data test;
set hrd.temp;
TotDay=enddate-startdate;
run;
proc print data=hrd.temp; format startdate enddate date9.; run;
Function
|
Example Code
|
Result
|
---|---|---|
MDY
|
date=mdy(mon,day,yr); |
SAS date
|
TODAYDATE
|
now=today(); now=date(); |
today's date as a SAS
date
|
TIME
|
curtime=time(); |
current time as a SAS
time
|
Function
|
Example Code
|
Result
|
---|---|---|
DAY
|
day=day(date); |
day of month (1-31)
|
QTR
|
quarter=qtr(date); |
quarter (1-4)
|
WEEKDAY
|
wkday=weekday(date); |
day of week (1-7)
|
MONTH
|
month=month(date); |
month (1-12)
|
YEAR
|
yr=year(date); |
year (4 digits)
|
INTCK
|
x=intck('day',d1,d2); x=intck('week',d1,d2); x=intck('month',d1,d2); x=intck('qtr',d1,d2); x=intck('year',d1,d2); |
days from D1 to D2
weeks from D1 to D2
months from D1 to D2
quarters from D1 to
D2
years from D1 to D2
|
INTNX
|
x=intnx('interval', start-from,increment); |
date, time, or datetime
value
|
DATDIF
YRDIF
|
x=datdif(date1,date2,'ACT/ACT'); x=yrdif(date1,date2,'ACT/ACT'); |
days between date1 and
date2
years between date1
and date2
|
Syntax, YEAR, QTR, MONTH,
and DAY functions:
YEAR(date)
QTR(date)
MONTH(date)
DAY(date)
|
Function
|
Description
|
Form
|
Sample Value
|
---|---|---|---|
YEAR
|
Extracts the year value
from a SAS date value.
|
YEAR(date)
|
2002 |
QTR
|
Extracts the quarter
value from a SAS date value
|
QTR(date)
|
1 |
MONTH
|
Extracts the month value
from a SAS date value.
|
MONTH(date)
|
12 |
DAY
|
Extracts the day value
from a SAS date value
|
DAY(date)
|
5 |
year(startdate)
month(startdate)
data hrd.nov99; set hrd.temp; where year(startdate)=1999 and month(startdate)=11; run;
data hrd.nov99; set hrd.temp; where year(startdate)=1999 and month(startdate)=11; proc print data=hrd.nov99; format startdate enddate date9.; run;
year(startdate)
1998
are
placed in the new data set. data hrd.temp98; set hrd.temp; where year(startdate)=1998; run;
data hrd.temp98; set hrd.temp; where year(startdate)=1998; run; proc print data=hrd.temp98; format startdate enddate date9.; run;
Syntax, WEEKDAY function:
WEEKDAY(date)
|
Value
|
Equals
|
Day of the Week
|
---|---|---|
1
|
=
|
Sunday
|
2
|
=
|
Monday
|
3
|
=
|
Tuesday
|
4
|
=
|
Wednesday
|
5
|
=
|
Thursday
|
6
|
=
|
Friday
|
7
|
=
|
Saturday
|
data work.schwkend; set sasuser.sch; where weekday(airdate)in(1,7); run; proc print data=work.schwkend; run;
if weekday(airdate) in (1,7);
is the same as if
weekday(airdate)=7 or weekday(airdate)=1;
Syntax, MDY function:
MDY (month, day, year)
|
mdy(month,day,year)
data hrd.newtemp(drop=month day year);
set hrd.temp;
Date=mdy(month,day,year);
run;
proc print data=hrd.newtemp; format date date9.; run;
data hrd.newtemp;
set hrd.temp;
DateCons=mdy(6,17,2002);
proc print data=hrd.newtemp;
format datecons date9.;
run;
MDY(5,10,20)
=
May 10, 1920
MDY(5,10,2020)
=
May 10, 2020
data sasuser.review2010; set sasuser.review; ReviewDate=mdy(12,day,2010); run; proc print data=sasuser.review2010 noobs; run;
data dec.review2010;
set dec.review;
ReviewDate=mdy(15,day,2010);
run;
Syntax, DATE function:
DATE ()
|
Syntax, TODAY function:
TODAY ()
|
EditDate=date();
data hrd.newtemp; set hrd.temp; EditDate=date(); run;
proc print data=hrd.newtemp; format editdate date9.; run;
Syntax, INTCK function:
INTCK (interval <multiple> <.shift-index>, start-date, end-date, <‘method’>)
INTCK(custom-interval, start-date, end-date, <‘method’>)
|
Example Code
|
Value
|
---|---|
Weeks=intck('week','31dec2000'd,'01jan2001'd); |
0
|
Months=intck('month','31dec2000'd,'01jan2001'd); |
1
|
Years=intck('year','31dec2000'd,'01jan2001'd); |
1
|
2
. The INTCK
function determines that two years have elapsed between June 15, 1999,
and June 15, 2001. Years=intck('year','15jun1999'd,'15jun2001'd);
Months=intck('month','15jun1999'd,'15jun2001'd);
Years=intck('year','01jan2002'd,'31dec2002'd);
data work.anniv30; set flights.mechanics(keep=id lastname firstname hired); Years=intck('year',hired,today()); if years=30 and month(hired)=month(today()); run; proc print data=work.anniv30; title '30-Year Anniversaries This Month'; run;
Syntax, INTNX function:
INTNX('interval',start-from,increment<,'alignment'>)
Note: The type of interval (date,
time, or datetime) must match the type of value in start-from and increment.
|
13515
, which corresponds to
January 1, 1997. TargetYear=intnx('year','05feb94'd,3);
TargetMonth=intnx('semiyear','01jan2001'd,1);
Example Code
|
Date Value
|
---|---|
MonthX=intnx('month','01jan1995'd,5,'b'); |
12935 (June
1, 1995)
|
MonthX=intnx('month','01jan1995'd,5,'m'); |
12949 (June
15, 1995)
|
MonthX=intnx('month','01jan1995'd,5,'e'); |
12964 (June
30, 1995)
|
Syntax, DATDIF, and
YRDIF functions:
DATDIF(start_date,end_date,basis))
YRDIF(start_date,end_date,basis))
|
Character String
|
Meaning
|
Valid in DATDIF
|
Valid in YRDIF
|
---|---|---|---|
'30/360' |
specifies a 30-day month
and a 360-day year
|
yes
|
yes
|
'ACT/ACT' |
uses the actual number
of days or years between dates
|
yes
|
yes
|
'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)
|
no
|
yes
|
'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)
|
no
|
yes
|
Example Code
|
Returned Value
|
---|---|
yrdif('16oct1998'd,'16feb2003'd,'30/360')
|
4.333333333 |
yrdif('16oct1998'd,'16feb2003'd,'ACT/ACT') |
4.3369863014 |
yrdif('16oct1998'd,'16feb2003'd,'ACT/360') |
4.4 |
yrdif('16oct1998'd,'16feb2003'd,'ACT/365') |
4.3397260274 |