Date and Time Functions

Date and Time functions

Date and Time Functions :

How Dates Works in SAS
The SAS system stores Date values as the number of elapsed days
Since January 1, 1960

Ex:- January 03,1960 is stored as 2
       January 02,1960 is stored as 1
       January 01,1960 is stored as 0
      December 31,1959 is stored as -1
      December 30,1959 is stored as -2
      December 31,1960 is stored as 365
The SAS system stores Time values as the number of elapsed seconds since midnight of that particular day.
The SAS system stores Datetime values as the number of elapsed seconds since midnight January 1, 1960 12:00 am
And SAS system stores Date variables as the number of days since midnight January 1, 1960
Dates before January 01, 1960 are negative integers, after January 01, 1960 are positive integers
SAS Dates are valid from A.D. 1582 to A.D. 19,900.
How SAS Converts Calendar Dates to SAS Date Values

screenshot 2025 09 14 115452

DATE
Returns the current date as a SAS date value
Returns today’s date as a SAS date value
Syntax: – DATE()
Data ds1;
date1=date();
Run;
Data ds1a;
date1=date();
Format date1 date9. ;
Run;

TODAY
Returns the current date as a SAS date value
Syntax:-TODAY()
Data ds2;
Day=today();
Format day date9.;
Run;

DATETIME
Returns the current date and time of day as a SAS datetime value
Syntax:-DATETIME()
Data ds3;
a=datetime();
Format a datetime20.;
Run;

TIME
Returns the current time of day
Syntax:-TIME()
SAS assigns current system time as a SAS time value corresponding to 15:32:00 if the following statements are executed exactly at 3:32 PM:
Its gives 24 hour format
Data ds4;
Time=time();
Format time time8. ;
Run;

DAY
Returns the day of the month from a SAS date value
Syntax:-DAY()
Data ds5;
a=’29Jan2010’d;
Day=day(a);
Run;
Data ds5a;
a=date();
b= day(a);
Format a date9.;
Run;

WEEK
Returns the week-number value
Syntax:-WEEK (<SAS_Date>, <descriptor>)
Data ds6;
X=week(’29Jan2010’d);
Y= week(’10Feb2010’d);
Z= week(’31Dec2010’d);
Run;
Data ds6a;
X=date();
Y=week(x);
Format x date9. ;
Run;

WEEKDAY
Returns the day of the week from a SAS date value
For example 17Oct1991 Returns 5 because 17Oct1991 was Thursday so it’s 5
Syntax:-WEEKDAY(date)
Data ds7;
week1=weekday(’16Mar1997’d);
Run;
Data ds7a;
a=date();
week1=weekday(a);
Run;

MONTH
Returns the month from a SAS date value
Syntax:-MONTH (date)
Data ds8;
a=’29Jan2010’d;
Mon=month(a);
Run;
Data ds8a;
a=today();
Mon=month(a);
Run;

QTR
Returns the quarter of the year from a SAS date value
Syntax:-QTR(date)
Data ds9;
a=’29Jan2010’d;
Quarter=qtr(a);
Run;
Data ds9a;
a=’15Nov2010’d;
b=today();
Quarter1=qtr(a);
Quarter2=qtr(b);
Run;

YEAR
Returns the year from a SAS date value
Gives four-digit numeric value that represents the year
Syntax:-YEAR(date)
Data ds10;
Date=’25dec97’d;
y=year(date);
Run;

DHMS
Returns a SAS datetime value from date, hour, minute, and second
Syntax: – DHMS (date, hour, minute, second)
Data ds11;
a=dhms(’15Nov2010’d,10,02,15);
Format a datetime. ;
Run;
Data ds11a;
a=dhms(’15Nov2010’d,10,02,61);
b=dhms(’15Nov2010’d,10,02,61);
Format a datetime. ;
Format b datetime20. ;
Run;
Data ds11b;
a=dhms(’15Nov2010’d,10,.2,11);
Format a datetime.;
Run;

HMS
Returns a SAS time value from hour, minute, and second values
Syntax: – HMS (hour, minute, second)
Data ds12;
a=HMS(10,02,15);
Format a time.;
Run;

Data ds12;
a=HMS(10,02,15);
b=HMS(10,02,15);
c=HMS(10,02,15);
Format a time.;
Format b time5.;
Format c time8.;
Run;

HOUR
Returns the hour from a SAS time or datetime value
Syntax: – HOUR (<time | datetime>)
Data ds13;
a=hour(’10:30’t);
Run;
Data ds13a;
a=’10:30:05’t;
b=hour(a);
Format a time8. ;
Run;
MINUTE
Returns the minutes from a SAS time or datetime value
Syntax: – Minute (<time | datetime>)
Data ds14;
a=’10:30:05’t;
b=MINUTE(a);
Format a time5.;
Run;

SECOND
Returns the seconds from a SAS time or datetime value
Syntax: – Second (<time | datetime>)
Data ds14a;
a=’10:30:05’t;
b=second(a);
Format a time. ;
Run;

DATEJUL
Converts a Julian date to a SAS date value
Syntax: – DATEJUL(Julian-date)
Julian-date
Specifies a SAS numeric expression that represents a Julian date A Julian date in SAS is a date in the form yyddd or yyyyddd, Where yy or yyyy is a two-digit or four-digit integer that represents the year and ddd is the number of the day of the year
The value of ddd must be between 1 and 365 (or 366 for a leap year).
Data ds15;
a=Datejul(10001);
Format a date9.;
Run;
Data ds15a;
a=Datejul(10365);
Format a date9.;
Run;

JULDATE
Returns the Julian date from a SAS date value
Syntax: – JULDATE (date)
The JULDATE function converts a SAS date value to a five- or seven-digit Julian date If date falls within the 100-year span defined by the system option YEARCUTOFF=, the result has five digits:
The first two digits represent the year, and the next three digits represent the day of the year (1 to 365, or 1 to 366 for leap years)
Otherwise, the result has seven digits: the first four digits represent the year, and the next three digits represent the day of the year. For example, if YEARCUTOFF=1920, JULDATE would return 97001 for January 1, 1997,
and return 1878365 for December 31, 1878.
Data ds16;
a=juldate(’01Jan2010’d);
Run;
Data ds16a;
a=date();
b=juldate(a);
Format a date9.;
Run;

MDY
Returns a SAS date value from month, day, and year values
Syntax: – MDY (month,day,year)
Month
Specifies a numeric expression that represents an integer from 1 through 12.
Day
Specifies a numeric expression that represents an integer from 1 through 31.
Year
Specifies a two-digit or four-digit integer that represents the year
The YEARCUTOFF= system option defines the year value for two-digit dates
Data ds17;
x_birthday=mdy(8,27,90);
y_birthday=mdy(05,30,2009);
Format x_birthday worddate20. ;
Format y_birthday weekdate30. ;
Run;
YYQ
Returns a SAS date value from the year and quarter year
Year
Specifies a two-digit or four-digit integer that represents the year
The YEARCUTOFF= system option defines the year value for two-digit dates
Quarter
Specifies the quarter of the year (1, 2, 3, or 4)
Syntax: – YYQ(year, quarter)
Data ds18;
DateValue1=yyq(2001,3);
DateValue2=yyq(09,2);
Format DateValue1 date7.;
Format DateValue2 date7.;
Run;

TIMEPART
Extracts a time value from a SAS datetime value
Syntax: – TIMEPART (datetime)
Data ds19;
x=datetime();
y=timepart(x);
Format X datetime. Y time. ;
Run;

DATEPART
Extracts the date from a SAS datetime value
Syntax: – DATEPART(datetime)
Data ds20;
X=datetime();
Y=datepart(x);
Format x datetime. y ddmmyy10.;
Run;
Data ds20a;
x=datepart (’01Jan2010:05:30:26’dt);
Format x ddmmyy8.;
Run;
Data ds1;
Input id$ fname$ lname$ sal dob datetime.;
Format dob datetime. date date9. time time8.;
Date=datepart(dob);
Time=timepart(dob);
Datalines;
001 mohan arisela 60000 10jan1983:10:30:15
002 padma narni 45000 22feb1983:20:23:52
003 varma maddina 50000 30mar1983:06:55:25
;
Run;

INTCK
Returns the integer count of the number of interval boundaries between two dates, two times, or two datetime values
Syntax: – INTCK(interval, from, to)
Interval
Specifies a character constant, a variable, or an expression that contains a time interval such as SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QTR, SEMIYEAR and YEAR
DATA ds21;
BDATE=’10SEP2008’D;
EDATE=’14SEP2010’D;
ACTDATE=INTCK(‘DAYS’, BDATE, EDATE);
RUN;
DATA ds21a;
BDATE=’10SEP2008’D;
EDATE=’14SEP2010’D;
ACTDATE=INTCK(‘months’, BDATE, EDATE);
RUN;
DATA ds21b;
BDATE=’10SEP2008’D;
EDATE=’14SEP2010’D;
ACTDATE=INTCK(‘Semiyear’, BDATE, EDATE);
RUN;
DATA ds21c;
y=trim(‘year ‘);
date1=’1sep1991’d + 300;
date2=’1sep2001’d – 300;
Years= INTCK(y,date1,date2);
RUN;

YRDIF
Returns the difference in years between two dates
Syntax: – YRDIF (sdate,edate,basis)
sdate Specifies a SAS date value that identifies the starting date
edate Specifies a SAS date value that identifies the ending date
basis
Identifies a character constant or variable that describes how SAS calculates the date difference. The following character string is valid: ’30/360′ – Specifies a 30-day month and a 360-day year in calculating the number of years. Each month is considered to have 30 days, and each year 360 days, regardless of the actual number of days in each month or year
DATA ds22;
BDATE=’10SEP2000’D;
EDATE=’14SEP2010’D;
ACTYEARS=YRDIF(BDATE, EDATE, ‘ACTUAL’);
Format BDATE date9. EDATE date9. ;
RUN;
DATA ds22a;
Sdate=’16Oct1998’d;
Edate=’16Feb2003’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’);
Run;
DATA ds22b;
Sdate=’16Oct1998’d;
Edate=’16Feb2003’d;
YRDIFF=yrdif(sdate, edate, ’30/360′);
DAYDIFF=yrdif(sdate, edate, ‘ACT/365’);
Run;

INTNX
Increments a date, time, or datetime value by a given interval or intervals, and returns a date, time, or datetime value
Syntax: –
INTNX (interval<multiple><.shift-index>, start-from, increment<, alignment>)
Interval
Specifies a character constant, a variable, or an expression that contains a time interval such as SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QTR, SEMIYEAR and YEAR
Data ds23;
Yr=intnx(‘year’,’05feb94’d,3);
Format yr date7. ;
Run;
Data ds23a;
Next=intnx(‘semiyear’,’01jan97’d,1);
Format next date9.;
Run;
Data ds23b;
X1=’month ‘;
X2=trim(x1);
Date=’1jun1990’d – 100;
Next_month=intnx(x2,date,1);
Format Next_month date9.;
Run;
DATA DS23c;
TODAY1=TODAY(); FORMAT TODAY1 DATE9.;
CDATE=PUT (INTNX (‘MONTH’,TODAY1,0,’S’),DATE9.);
LMCDATE=PUT(INTNX(‘MONTH’,TODAY1,-1,’S’),DATE9.);
BCDATE=PUT(INTNX(‘DAY’,TODAY1,-1,’S’),DATE9.);
LMBCDATE=PUT(INTNX(‘MONTH’,(TODAY1-1),-1,’S’),DATE9.);
BDATE=PUT(INTNX(‘MONTH’,TODAY1,0,’B’),DATE9.);
EDATE=PUT(INTNX(‘MONTH’,TODAY1,0,’E’),DATE9.);
RUN;
HOLIDAY
Returns a SAS date value for the holiday and year specified
Valid values for holiday are ‘BOXING’, ‘CANADA’, ‘CANADAOBSERVED’, ‘CHRISTMAS’, ‘COLUMBUS’, ‘EASTER’, ‘FATHERS’, ‘HALLOWEEN’, ‘LABOR’, ‘MLK’, ‘MEMORIAL’, ‘MOTHERS’, ‘NEWYEAR’,’THANKSGIVING’, ‘THANKSGIVINGCANADA’,’USINDEPENDENCE’, ‘USPRESIDENTS’, ‘VALENTINES’, ‘VETERANS’, ‘VETERANSUSG’, ‘VETERANSUSPS’, and ‘VICTORIA’
For example: MOTHERS2011= HOLIDAY (’ MOTHERS’, 2011);
Syntax: – HOLIDAY (‘holiday’, year)
DATA DS24;
THANKSGIVING_2012=HOLIDAY (‘ THANKSGIVING ‘, 2012);
Format THANKSGIVING_2012 date9. ;
RUN;