Dates, times, and date-times are commonly used variable types in data analysis. In SAS, dates and times are numeric variables.
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.
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
The functions that can be used to create date values “include”:
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.
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()= 22381Formated today=11/04/2021date()= 22381Formated today=11/04/2021mdy(7,25,2020)= 22121Formated mdy=25/07/2020datejul(2008095)= 17626Formated datejul=04/04/2008yyq(2008, 2)= 17623Formated yyq=01/04/2008time= 54973.863589Formated time="15":"16":14datetime()= 1933773373.9Formated datetime="11APR21":"15":"16":14dhms('10082020'd,10,12,12)= 1912727532Formated dhms="11AUG20":"01":"12":12hms(10,12,12)= 1912727532Formated hms= "1":"30":15
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.
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.
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=11AUG2020day=11weekday=3week=32month=8quarter=3year=2020firstSunday=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
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
The functions that can be used to calculate intervals “include”:
“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) |
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.6138888889yactact=1.6120218579yact360=1.6361111111yact365=1.6136986301
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
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=831days360=820
Learn more on INTNX and INTCK functions.
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=255date after 6 week = 18OCT2020
The DATEPART and TIMEPART functions extract date or time from a SAS DateTime value.
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=22138Formated datepart=11AUG2020timepart=74895Formated timepart="20":"48":15
data _null_;valentines=holiday('valentines', 2020);format valentines date9.;put valentines;run;
“Output”:
14FEB2020
To set a time zone, use the TIMEZONE= system “option”:
options timezone='asia/tokyo';
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":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.
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
