Thursday, April 4, 2013

SAS Date and Time functions(Part 1)


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

Objective : This post is the second in the series of posts on functions and first 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.
All the SAS function posts can be found here:
SAS Character function Part 1
SAS Character function Part 2
Date time functions:

DATDIF Function:

This function outputs the number of days between two dates, the start and end date. The calculation is done based on the value of the third argument(basis).

Syntax : 
DATDIF(Start_date, End_date, basis)

Start_date : The  period start date.

End_date : The period end date.

Basis : Basic can take the following values :

  ACT/ACT – The actual number of days are calculated between two dates.
  30/360 – Calculations are based on 30 day month and 360 day year.
  ACT/360 – Calculations are based on ACT days in months but 360 days in a year.
  ACT/365 - Calculations are based on ACT days in months but 365 days in a year.

E.g.
data _null_;
   stdate='01jan10'd;
   eddate='15jul13'd;
   actual_days=datdif(stdate, eddate, 'act/act');
   days_360=datdif(stdate, eddate, '30/360');
   put actual_days= days_360=;
run;

Log: actual_days=1291 days_360=1274

YRDIF Function:

This function outputs the number of year elapsed between two dates, the start and end date. The calculation is done based on the value of the third argument(basis).

Syntax : 
YRDIF(Start_date, End_date, basis)

Start_date : The  period start date.

End_date : The period end date.

Basis : Basic can take the following values :

  ACT/ACT – The actual number of days are calculated between two dates.
  30/360 – Calculations are based on 30 day month and 360 day year.
  ACT/360 – Calculations are based on ACT days in months but 360 days in a year.
  ACT/365 - Calculations are based on ACT days in months but 365 days in a year.

E.g.
data _null_;
   stdate='01jan1930'd;
   eddate='15jul1999'd;
   actual_years =YRDIF(stdate, eddate, 'act/act');
   years_360    =YRDIF(stdate, eddate, '30/360' );
   put actual_years= years_360=;
run;

Log: actual_years=69.534246575 years_360=69.538888889

As you would expect, the number of years increased when we counted a 360 day Year.

DATE Function:

This function is equivalent to TODAY() function as it returns the current date value. This function is used for codes which are run daily to dynamically get the current date.

Syntax:
Date()

E.g.

data _null_;
  format x date9.;
  x=DATE();
  put x=;
run;

Log: x=03APR2013

TIME Function:

This function returns the current time value.

Syntax:
Time()

E.g.

data _null_;
  format x Time8.;
  x=TIME();
  put x=;
run;

Log: x=16:47:52

TODAY Function:

This function is equivalent to DATE() function as it returns the current date value. This function does not accept any argument. This function is used for codes which are run daily to dynamically get the current date.

Syntax:

Today()

E.g.

data _null_;
  format x date9.;
  x=today();
  put x=;
run;

Log: x=03APR2013

DATEPART Function:

This function extracts just the date and leaves out the Time part from a SAS Datetime value.

Syntax :

DATEPART(datetime)

Datetime : Any SAS datetime value

E.g.
data _null_;
      format x datetime20.;
      format y date9.;
      x=datetime();
      y=datepart(x);
      put x=;
      put y=;
run;

LOG:
x=03APR2013:23:10:23
y=03APR2013

TIMEPART  Function:

This function extracts just the time and leaves out the date part from a SAS Datetime value.

Syntax:
TIMEPART(datetime)

Datetime - Any SAS datetime value

E.g.
data _null_;
      format x datetime20.;
      format y date9.;
      x=datetime();
      y=timepart(x);
      put x=;
      put y=;
run;

LOG:
x=03APR2013:23:10:23
y=23:10:23

DATETIME Function:

This function returns the current date and time as a SAS Datetime value. It does not take any arguments.

Syntax:
DATETIME()

E.g.
data _null_;
      format x datetime20.;
      x=datetime();
      put x=;
run;

LOG:
x=03APR2013:23:24:11

DHMS Function:

This function is used to create a SAS Datetime value from a SAS date value and Hour, Min and Second values.

Syntax:
DHMS(date,hour,minute,second)

E.g.
data _null_;
      x=DHMS('04APR2013'd,12,10,45);
      format x datetime20.;
      put x=;
run;

LOG:
x=04APR2013:12:10:45

HMS Function:

This function takes Hour,Minute and Second numeric values as arguments and returns a SAS Time value.

Syntax:
HMS(hour,min,sec)

E.g.
data _null_;
      x=HMS(12,10,45);
      format x time8.;
      put x=;
run;

LOG:
x=12:10:45

JULDATE Function:

This function returns a Julian date value taking a SAS Date value as an argument.

Note : A Julian date is  notation where a year is represented as first 2 digits(If the date falls between the YEARCUTOFF option boundaries) or first 4 digits and next 3 digits as the number of days elapsed In an year.
 For e.g. 10Jan1911 will be represented as 1911010 and 10Jan1935 as 35010.

Syntax:
JULDATE(date)

Date- Any SAS date value

E.g.
data _null_;
      juldt1=juldate('30dec2099'd);
      juldt2=juldate('30dec2011'd);;
      put juldt1= juldt2=;
run;

LOG:
juldt1=2099364 juldt2=11364


JULDATE7 Function:

Same as JULDATE but always returns a 4 digit year. Recommended to use as output lengths do not vary with dates.

DATEJUL  Function:

This function is the opposite of JULDATE function. It takes a Julian date value as input and returns a SAS date value as output.

Syntax:
DATEJUL(Julian_date)
Julian_date – A Julian date value.

E.g.
data _null_;
      sasdt1=datejul(2099101);
      format sasdt1 date9.;
      put sasdt1=;
run;

LOG:
sasdt1=11APR2099

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.