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)
|
2018 |
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 work.nov17;
set cert.temp;
if year(startdate)=2017 and month(startdate)=11;
run;
proc print data=work.nov17; format startdate enddate birthdate date9.; run;
year(startdate)
2016
are
placed in the new data set. data work.temp16; set cert.temp; if year(startdate)=2016; run;
data work.temp16; set cert.temp; where year(startdate)=2016; run; proc print data=work.temp16; format startdate enddate birthdate 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 cert.sch; if 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 work.datestemp;
set cert.dates;
Date=mdy(month,day,year);
run;
proc print data=work.datestemp;
format date mmddyy10.;
run;
data work.datestemp;
set cert.dates;
DateCons=mdy(6,17,2018);
run;
proc print data=work.datestemp;
format DateCons mmddyy10.;
run;
data work.review2018 (drop=Day);
set cert.review2018;
ReviewDate=mdy(12,day,2018);
run;
proc print data=work.review2018;
format ReviewDate mmddyy10.;
run;
data work.review2018 (drop=Day);
set cert.review2018;
ReviewDate=mdy(15,day,2018);
run;
proc print data=work.review2018;
format ReviewDate mmddyy10.;
run;
Syntax, DATE function:
DATE ()
|
Syntax, TODAY function:
TODAY ()
|
EditDate=date();
data work.tempdate; set cert.dates; EditDate=date(); run; proc print data=work.tempdate; format EditDate date9.; run;
Syntax, INTCK function:
INTCK (‘interval’
, from, to )
Note: The type of interval (date,
time, or datetime) must match the type of value in from.
|
Example Code
|
Value
|
---|---|
Weeks=intck('week','31dec2017'd,'01jan2018'd); |
0
|
Months=intck('month','31dec2017'd,'01jan2018'd); |
1
|
Years=intck('year','31dec2017'd,'01jan2018'd); |
1
|
2
. The INTCK
function determines that two years have elapsed between June 15, 2016,
and June 15, 2018. Years=intck('year','15jun2016'd,'15jun2018'd);
Months=intck('month','15jun2016'd,'15jun2018'd);
Years=intck('year','01jan2018'd,'31dec2018'd);
data work.anniversary; set cert.mechanics(keep=id lastname firstname hired); Years=intck('year',hired,today()); if years=20 and month(hired)=month(today()); run; proc print data=work.anniversary; title '20-Year Anniversaries'; 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.
|
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
|
---|---|
data _null_;
x=yrdif('16feb2016'd,'16jun2018'd,'30/360');
put x;
run; |
2.3333333333 |
data _null_;
x=yrdif('16feb2016'd, '16jun2018'd, 'ACT/ACT');
put x;
run; |
2.3291114604 |
data _null_;
x=yrdif('16feb2016'd, '16jun2018'd, 'ACT/360');
put x;
run; |
2.3638888889 |
data _null_;
x=yrdif('16feb2016'd, '16jun2018'd, 'ACT/365');
put x;
run; |
2.3315068493 |