Monday, April 22, 2013

SAS Date and Time functions(Part 2)


SAS Date and Time functions(Part 2)
A quick reference

Objective : This post is the second in the series of posts on functions and Second of two posts on Date Time functions. The first post was on SAS character functions. This post highlights frequently used SAS Date and time functions and provides a quick reference list and is invaluable for revision before interviews. Also this posts aim to strengthen the your function knowledge by providing interesting examples.

Date time functions:

MDY  Function:

This function takes month, day and year as numeric values and returns a SAS date value created from them.
Syntax:
MDY(month,day,year)
E.g.
data _null_;
      sasdt1=MDY(01,31,2009);
      format sasdt1 date9.;
      put sasdt1=;
run;

LOG:
sasdt1=31JAN2009

YEAR Function:


This function extracts the year from a SAS date value.

Syntax:
YEAR(date)

Date – Any SAS date value

E.g.
data _null_;
      ye=YEAR('31Jan2011'd);
      put ye=;
run;

LOG:
ye=2011

QTR Function:

This function determines which quarter of the year, the date in the argument lies into and return the number of that Quarter.

Syntax:
QTR(date)
Date – Any SAS date value

E.g.
data _null_;
      qtr=QTR('31Jan2011'd);
      put qtr=;
run;

LOG:
qtr=1

MONTH Function:

This function extracts and returns the Month value from  a SAS Date value.

Syntax:
MONTH(date)
Date : Any SAS date value

E.g.
data _null_;
  x=MONTH('04APR2013'd);
  put x=;
run;

LOG:
x=4

WEEKDAY Function:

The weekday function returns and integer corresponding to the day of the week which is on the date passed as argument. With Sunday denoted as 1, Monday as 2 and so on.

Syntax:
WEEKDAY(date)
Date : Any SAS date value
E.g.
data _null_;
  x=WEEKDAY('04APR2013'd);
  put x=;
run;

LOG:
x=5

DAY Function:

The day function returns and integer donating the day of the month which is in the date passed as argument.

Syntax:
DAY(date)
Date : Any SAS date value
E.g.
data _null_;
  x=day('04APR2013'd);
  put x=;
run;

LOG:
x=4

HOUR Function:

The HOUR function accepts a SAS Date value or Datetime value as an argument and extracts and returns the hour value .

Syntax:
HOUR(<time | datetime>)
Time | Datetime – Any SAS Time or Datetime value

E.g.
data _null_;
      hr=HOUR('31Jan2011:10:26:56'dt);
      put hr=;
run;

LOG:
hr=10

MINUTE Function:

This function extracts the Minute value from  a SAS Time or Datetime value.

Syntax:
MINUTE(time | datetime)
Time | Datetime – Any SAS Time or Datetime value

E.g.
data _null_;
      min=MINUTE('31Jan2011:00:26:56'dt);
      put min=;
run;

LOG:
min=26

SECOND Function:

This function extracts the Second value from  a SAS Time or Datetime value.

Syntax:
SECOND(time | datetime)
Time | Datetime – Any SAS Time or Datetime value

E.g.
data _null_;
      sec=MINUTE('31Jan2011:00:26:56'dt);
      put sec=;
run;

LOG:
sec=56

INTCK Function:

This function is one of the most important date functions and is also asked heavily in SAS interviews.
Intck function calculates the number of interval(Year, Month,day etc.) boundaries between two dates, the from and To dates.

Syntax:
INTCK('interval',from,to)
Interval -

The intervals that can be used are :
DAY,  WEEK, WEEKDAY, TENDAY, SEMIMONTH, MONTH, QTR, SEMIYEAR, YEAR, HOUR, MINUTE, SECOND.
From – The SAS start date value
To – The SAS end date value.
e.g

data _null_;
      date1='1oct1993'd;
      date2='1oct2003'd;
      newyears=intck('year',date1,date2);
      put newyears=;
run;

LOG:
newyears=10

INTNX Function:

This function is one of the most important date functions and is also asked heavily in SAS interviews.
Intnx function increments the strat-date by number of intervals(Year, Month,day etc.) specified by increment.

Syntax:
INTNX('interval',start-date,increment<,'alignment'>)

Interval -
The intervals that can be used are :
DAY,  WEEK, WEEKDAY, TENDAY, SEMIMONTH, MONTH, QTR, SEMIYEAR, YEAR, HOUR, MINUTE, SECOND.
start-date – The SAS start date value for the increment
increment – The number of increments for which the date has to be incrmented.
Alignment - controls the position of incremented date within the interval. Alignment can take the following values:

BEGINNING(Alias: B) : This specifies that the date that is returned  is aligned to the beginning of the interval.
MIDDLE(Alias: M): This specifies that the date that is returned  is aligned to the midpoint of the interval, which is the average of the beginning and ending alignment values.
END(Alias: E): This specifies that the date that is returned is aligned to the end of the interval.
SAME(Alias: S):  This specifies that the date that is returned has the same alignment as the input date.
e.g

data _null_;
      date1='1oct1993'd;
      final_date=intnx('year',date1,10,'B');
      format final_date date9.;
      put final_date =;
run;

LOG:
final_date=01JAN2003

The year has been incremented by 10 Years and the date is aligned to the beginning of the YEAR interval which is 1st Jan.

Conclusion: I hope this post will help you to improve your knowledge on functions and also will serve as a quick reference while preparing for interviews.

Will be back with some more SAS magic. Goodbye till then.


Saurabh Singh  Chauhan
(er.chauhansaurabh@gmail.com)         
Note: Comments and suggestions are always welcome.



Disclaimer :
SAS® and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc.in the USA and other countries. ® Indicates USA registration.
Other brand and product names are registered trademarks or trademarks of their respective companies. 
The contents of this post are the works of the author(s)and do not necessarily represent the opinions,recommendations, or practices of any organization whatsoever.


1 comment:

  1. it's really a piece of new information to me, I learn more from this blog, I want to learn more from this blog, keep on it doing, I eagerly waiting for your updates, Thankyou. other anyone wants to learn SAS course s contact here- +91-9311002620 or visit website- https://www.htsindia.com/Courses/business-analytics/sas-training-institute-in-delhi

    ReplyDelete