How do you extract or subset observations for a specific year from variable date time from a data set ?
It will not work unless you replace date with hiredate
Data A;
set Shareef.emp;
if year(datepart(hiredate))=1982;
run;
data have;
input date anydtdtm.;
format date datetime20.;
cards;
28-05-2013:11:21:20
27-05-2013:15:22:26
27-05-2013:11:36:33
27-05-2013:11:14:55
28-05-2014:10:21:20
27-05-2014:14:22:26
27-05-2014:11:36:33
27-05-2014:17:14:55
27-05-2014:10:34:56
27-05-2014:19:03:48
27-05-2014:14:30:56
;
run;
data want;
set have;
if year(datepart(date))=2013;
run;
proc print data=want;
run;
Data A;
set Shareef.emp;
if year(datepart(date))=1982;
run;
This is not working.returns zero observations. I know for sure that there is year 1982 in the following format 23jan198200.00.00 and the name of the variable is hiredate.
Thanks for the help.
It will not work unless you replace date with hiredate
Data A;
set Shareef.emp;
if year(datepart(hiredate))=1982;
run;
No,doesn't work!
Post your SAS code and the SAS log which shows us what exactly is not working.
Are you sure that HIREDATE is not a string instead of a proper datetime value?
Or perhaps it has already been converted from datetime to date. If so then just use the YEAR() function.
If you have SAS 9.2 or newer you can use the DTEXTYEAR() function.
DT for Date Time
EXT for Extract
There are also DAY , MONTH, and WEEKDAY versions.
Hi Tom
I can find DTEXTYEAR() as a supported function by Access engines - but I can't find the function as such documented under "functions" SAS(R) 9.4 Functions and CALL Routines: Reference, Third Edition
Do you know where I could find the documentation and why this one is not documented where one would expect it?
Thanks
Patrick
Don't know. It is in the view SASHELP.VFUNC. Not sure if it requires SAS/Access to be licensed.
Hi Tom,
Is that the way to do it?
Thanks
Mohammed
Data B;
set Shareef.emp;
dtextyear(1982);
run;
To subset data you need to use a WHERE statement ( or possibly an IF statement). A WHERE statement requires an expression that can be evaluated to be true or false. So if the variable in your dataset that has date time values is named DT then you might do something like.
data want ;
set have ;
where year(datepart(DT)) = 1982;
run;
Or you could use other forms:
where dtextyear(DT) = 1982 ;
where '01JAN1982:00:00'dt <= DT < '01JAN1983:00:00'dt ;
SORRY, BOTH WAYS DON'T WORK.
THANKS ANYWAYS
MOHAMMED
What is not working? Post your code and log.
tried again.it worked.Thanks for everyone's help.Regards
Mohammed
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.