date functions in sas

Date Functions in SAS – The Definitive Guide

  • Post author:
  • Post category:Base SAS
  • Post comments:0 Comments
  • Reading time:17 mins read

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

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 are 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.

 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.457266
Formated time= 9:12:16
date()= 22138
Formated today=11/08/2020
datetime()= 1912756336.5
Formated datetime=11AUG20:09:12:16

Functions to create date and time values

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

  • DATE() returns today’s date as a SAS date value.
  • TODAY() 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 date 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 two or four 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.

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;

Output:

-- Used DDMMYY10. FORMAT --
today()= 22381
Formated today=11/04/2021

date()= 22381
Formated today=11/04/2021

mdy(7,25,2020)= 22121
Formated mdy=25/07/2020

datejul(2008095)= 17626
Formated datejul=04/04/2008

yyq(2008, 2)= 17623
Formated yyq=01/04/2008

time= 54973.863589
Formated time=15:16:14

datetime()= 1933773373.9
Formated datetime=11APR21:15:16:14

dhms('10082020'd,10,12,12)= 1912727532
Formated dhms=11AUG20:01:12:12

hms(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) – The 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 the 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) – The 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) – The 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 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 a SAS date constant. Otherwise, reasonably self-explanatory! Let’s take a look at an example.

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;

Output:

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. 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.

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;

Output:

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.

Arguments Meaning
‘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)

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;

Output:

y30360=1.6138888889
yactact=1.6120218579
yact360=1.6361111111
yact365=1.6136986301

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;

Output:

age=22 years

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;

Output:

actual=831
days360=820
  • 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.

Calculating date After the specified date

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;

Output:

days=255
date after 6 week = 18OCT2020

Functions to extract date or time from SAS Datetime values

The DATEPART and TIMEPART functions extract date or time from a SAS DateTime value.

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

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;

Output:

datepart=22138
Formated datepart=11AUG2020
timepart=74895
Formated timepart=20:48:15

Functions to compute Holidays

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

Holiday function example

data _null_;
    valentines=holiday('valentines', 2020);
    format valentines date9.;
    put valentines;
run;

Output:

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.

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;

Output:

tzname=GMT+05:30
Time 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.

Finding the difference between two time zones

data _null_;
    diff=abs(tzoneoff('america/new_york') - tzoneoff('asia/Kolkata'));
    put diff time.;
run;

Output:

9:30:00

We hope this article helped you to understand the Date functions that are available in SAS.

You may also want to see our article on SAS Date Formats: How To Display Dates Correctly? and Date Interval Functions – INTNX And INTCK In SAS

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

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