HomeBASE SAS

Date Functions in SAS – The Definitive Guide9 min read

Like Tweet Pin it Share Share Email

Table of Contents Example 1: Demonstrating actual and Formatted SAS date values1 Functions to create date and time valuesExample 2: Creating SAS date, DateTime and time values2 Functions to extract part of datesExample 3: Extracting parts of SAS date values3 Functions to extract hours, minutes and seconds from SAS date-time valuesExample 4: Extracting hours, minutes and seconds from SAS date-time values.4 Using functions to calculate intervalsExample 5: YRDIF and its arguments exampleExample 6: Calculating a Person’s Age using YRDIF functionExample 7: DATDIF exampleExample 8:5 Functions to extract date or time from SAS Datetime valuesExample 9: DATEPART and TIMEPART example6 Functions to compute HolidaysExample 10: Holiday function example7 Functions to work with timezonesExample 11: Zone ID or Time Zone Name and Timezone offset ExampleExample 12:  Finding the difference between two time zones

Dates, times, and date-times are commonly used variable types in data analysis. In SAS, dates and times are numeric variables.

  • Create date, time or DateTime values
  • Extract part of a date
  • Computing interval between two dates

You may wish to review SAS date formats SAS stores date, time and DateTime variables as integers. This helps in calculating the differences between dates and many other numerical calculations.SAS time values are stored as the number of seconds between midnight of the current day and another time value.SAS DateTime values stored internally as the number of seconds between midnight, January 1, 1960, and the specified date and time.

SAS date values are stored internally as the number of days between January 1, 1960, and a specified date.Dates after January 1, 1960, are stored as positive numbers and dates before January 1, 1960, are stored as negative numbers.

Example 1: Demonstrating actual and Formatted SAS date values

data _NULL_;time=time();date=date();datetime=datetime();put ‘time= ‘ time;put ‘Formated time=’ time time.;put ‘date()= ‘ date;put ‘Formated today=’ date ddmmyy10.;put ‘datetime()= ‘ datetime;put ‘Formated datetime=’ datetime datetime.;run;

Note the actual value stored and the formatted output in the SAS log.

time= 33136.457266Formated time= 9:12:16date()= 22138Formated today=11/08/2020datetime()= 1912756336.5Formated datetime=11AUG20:09:12:16

Functions to create date and time values

The functions that can be used to create date values include:

  • DATE() – It returns today’s date as a SAS date value.
  • TODAY() – It returns today’s date as a SAS date value.
  • MDY(m,d,y) – It returns a SAS date value from the given month (m), day (d), and year (y) values.
  • DATEJUL(juldate) – converts a Julian date (juldate) to a SAS date value
  • YYQ(y, q) – returns a SAS date value from the given year (y) and a quarter (q) 1, 2, 3, or 4
  • TIME() – Returns the time of the day when the program was run.
  • DATETIME() – Returns DateTime value for the current data and time.
  • DHMS(date,hour,minutes,seconds) – Returns a SAS datetime value for given date,hour,minutes and seconds
  • HMS(h,m,s) – Returns SAS time value from hours,minutes and seconds.

The date( ) and today( ) functions returns the current date.

A Julian date is represented in the form of yyddd or yyyyddd

yy or yyyy represents the year for 2 digits or 4 digits and ddd is the number of the day of the year. The value  ddd is between 001 and 365 (or 366 for a leap year). For instance, the SAS Julian date for January 11, 2020, is 2020011.

Example 2: Creating SAS date, DateTime and time values

data _NULL_;today=today();date=date();mdy=mdy(7, 25, 2020);datejul=datejul(2008095);yyq=yyq(2008, 2);time=time();datetime=datetime();dhms=dhms(’10AUG2020’d,25,12,12);hms=hms(1,30,15);put ‘ — Used DDMMYY10. FORMAT –‘;put ‘today()= ‘ today;put ‘Formated today=’ today ddmmyy10.;put ‘date()= ‘ date;put ‘Formated today=’ date ddmmyy10.;put ‘mdy(7,25,2020)= ‘ mdy;put ‘Formated mdy=’ mdy ddmmyy10.;put ‘datejul(2008095)= ‘ datejul;put ‘Formated datejul=’ datejul ddmmyy10.;put ‘yyq(2008, 2)= ‘ yyq;put ‘Formated yyq=’ yyq ddmmyy10.;put ‘time= ‘ time; put ‘Formated time=’ time time.; put ‘datetime()= ‘ datetime; put ‘Formated datetime=’ datetime datetime.; put “dhms(‘10082020’d,10,12,12)= ” dhms;put ‘Formated dhms=’ dhms datetime.;put “hms(10,12,12)= ” dhms;put ‘Formated hms=’ hms time.;run;— Used DDMMYY10. FORMAT –today() = 22138 Formated today = 11/08/2020date() = 22138 Formated today = 11/08/2020mdy(7,25,2020) = 22121 Formated mdy = 25/07/2020datejul(2008095) = 17626 Formated datejul =0 4/04/2008yyq(2008, 2)= 17623 Formated yyq = 01/04/2008time() = 34398.129141 Formated time = 9:33:18datetime() = 1912757598.1 Formated datetime = 11AUG20:09:33:18dhms(‘10082020’d,10,12,12) = 1912727532 Formated dhms = 11AUG20:01:12:12hms(10,12,12) = 1912727532 Formated hms = 1:30:15

Functions to extract part of dates

The functions that can be used to take apart date values include:

  • DAY(date) – Day function returns the day of the month from a given SAS date value.
  • WEEKDAY(date) – To extract the day of the week from a SAS date. (1=Sunday,2=Monday and so on.)
  • WEEK(date,modifiers) – To extract week number of the year from a SAS date. The week number value is between 0-53 or 1-53 based on the modifiers(u,v,w). For details on the modifier refer to the SAS documentation.
  • MONTH(date) – Month function returns the month(1-January,2-February and so on) from a SAS date.
  • QTR(date) –  It extracts the Quarter(January-March =1, April- June =2,etc)
  • YEAR(date) – Year function returns the year from a SAS date value.
  • NWKDOM(n, weekday, month, year) – Returns the date for the nth occurrence of a weekday for the specified month and year.
    • n is the numeric week in the range of 1-5 of the month that contains the specified day.N=5 indicates that the specified day occurs in the last week of that month.
    • weekday – It specifies week number in the range of 1- 7. Sunday is considered the first day of the week and has a weekday value of 1.
    • month – specifies the month number in the range 1-12
    • year – specifies a four-digit calendar year.

The date can be specified either as a variable name or as a SAS date constant. Otherwise, fairly self-explanatory! Let’s take a look at an example.

Example 3: Extracting parts of SAS date values

data _null_;date=today();day=day(date);weekday=weekday(date);week=week(date);month=month(date);quarter=qtr(date);year=year(date);firstSunday=nwkdom(1,1, 1, 2020);format date date9. firstSunday weekdatx.;put _all_;run;date=11AUG2020 day=11 weekday=3 week=32 month=8 quarter=3 year=2020 firstSunday=Sunday, 5 January 2020

You can use the NWKDOM function to find dates like the last Saturday in June. You can use the following statement to calculate the date: date = nwkdom(4, 7, 6, 2020) which is 27JUN2020

Functions to extract hours, minutes and seconds from SAS date-time values

  • HOUR – It extracts the hour from a SAS DateTime or time value.
  • MINUTE – It extracts the minutes from a SAS DateTime or time value.
  • SECOND – It extracts the hour from a SAS DateTime or time value.

Example 4: Extracting hours, minutes and seconds from SAS date-time values.

data _null_;datetime=datetime();hours=hour(datetime);minutes=minute(datetime);seconds=second(datetime);format datetime datetime.;put _ALL_;run;datetime=11AUG20:10:03:16 hours=10 minutes=3 seconds=15.943666935

Using functions to calculate intervals

The functions that can be used to calculate intervals include:

  • YRDIF(startdate, enddate, ‘method’)-  It returns the difference in years between two SAS date values (startdate, enddate) using one of four methods (‘method’)
  • DATDIF(startdate, enddate, ‘method’)  – It returns the difference in days between two SAS date values (startdate, enddate) using one of four methods (‘method’)

Note: When counting the number of days in a month, DATDIF always includes the starting date and excludes the ending date.

Table 1: Valid arguments in DATDIF and YRDIF functions.

ArgumentsMeaning
‘act/act’uses the actual number of days and years between two dates
’30/360′specifies a 30-day month and a 360-day year
‘act/360’uses the actual number of days between dates in calculating the number of years (calculated by the number of days divided by 360)
‘act/365’uses the actual number of days between dates in calculating the number of years (calculated by the number of days divided by 365)

Example 5: YRDIF and its arguments example

data _null_; sdate=’1jan2019’d; edate=’12aug2020’d; y30360=yrdif(sdate, edate, ’30/360′); yactact=yrdif(sdate, edate, ‘ACT/ACT’); yact360=yrdif(sdate, edate, ‘ACT/360’); yact365=yrdif(sdate, edate, ‘ACT/365’); put y30360= / yactact= / yact360= / yact365= ;run;y30360=1.6138888889yactact=1.6120218579yact360=1.6361111111yact365=1.6136986301

Example 6: Calculating a Person’s Age using YRDIF function

With the YRDIF function, you can also calculate a person’s age. To calculate a person’s age enter the start date and end date arguments followed by a third argument ‘AGE’. Age calculation will automatically take care of any leap years.

data _null_; sdate=’10may1998’d; edate= today(); age=int(yrdif(sdate, edate, ‘AGE’)); put age= ‘years’;run;age=22 years

Example 7: DATDIF example

data _null_; sdate=’1jan2019’d; edate=today(); actual=datdif(sdate, edate, ‘act/act’); days360=datdif(sdate, edate, ’30/360′); put actual=; put days360=;run;actual=589days360=581

  • INTCK – It returns the number of time intervals (‘interval’) that occur between two dates (fromdate, to date)
  • INTNX – applies multiples (increment) of a given interval (‘interval’) to a date value (date) and returns the resulting value, and hence can be used to identify past or future days, weeks, months, and so on

Learn more on INTNX and INTCK functions.

Example 8:

data _null_; days=intck(‘day’, ’01jan2020’d,’12sep2020’d); date=intnx(‘week’,’12SEP2020’D, 6); put days=; put ‘date after 6 week = ‘ date date9.;run;days=255date after 6 week = 18OCT2020

Functions to extract date or time from SAS Datetime values

The DATEPART and TIMEPART function is used to extract date or time from a SAS DateTime value.

  • DATEPART(date) –  It returns a SAS date from a DateTime value.
  • TIMEPART(date) – It extracts time part from a SAS DateTime value.

Example 9: DATEPART and TIMEPART example

data _null_;datepart = DATEPART(’11AUG2020:20:48:15’DT);timepart = timepart(’11AUG2020:20:48:15’DT);put ‘datepart=’ datepart;put ‘Formated datepart=’ datepart date9.;put ‘timepart=’ timepart;put ‘Formated timepart=’ timepart time.;run;datepart=22138Formated datepart=11AUG2020timepart=74895Formated timepart=20:48:15

Functions to compute Holidays

  • HOLIDAY(‘ holiday’, year) – The HOLIDAY function returns a SAS date value of a specified holiday for a specified year. The standard holiday values are mentioned found in the SAS documentation.

Example 10: Holiday function example

data _null_;valentines = holiday(‘valentines’, 2020);format valentines date9.;put valentines;run;14FEB2020

Functions to work with timezones

To set a time zone, use the TIMEZONE= system option:

options timezone=’asia/tokyo’;

  • TZONENAME() – It Returns the current standard or daylight savings time, time zone name.
  • TZONEOFF() – The TZONEOFF( ) function returns the time zone offset for the current time zone.

Example 11: Zone ID or Time Zone Name and Timezone offset Example

data _null_;tzname=tzonename() ;timezoneIndia=tzonename(‘asia/Kolkata’);put tzname =;put ‘Time zone for India: ‘ timezoneIndia;tzone=tzoneoff(‘asia/Kolkata’);put ‘tzone=’ tzone time.;tzid=tzoneid() ;put tzid=;run;tzname=GMT+05:30Time zone for India: ISTtzone= 5:30:00tzid=GMT+05:30

For time zone names and time zone IDs, see Time Zone IDs and Time Zone Names.

Example 12:  Finding the difference between two time zones

data _null_;diff=abs(tzoneoff(‘america/new_york’) – tzoneoff(‘asia/Kolkata’));put diff time.;run;9:30:00

 

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.