BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
momin
Calcite | Level 5

How do you extract or subset observations for a specific year  from variable date time from a data set ?

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

It will not work unless you replace date with hiredate

Data A;

set Shareef.emp;

if year(datepart(hiredate))=1982;

run;

View solution in original post

14 REPLIES 14
stat_sas
Ammonite | Level 13

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;

momin
Calcite | Level 5

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.

stat_sas
Ammonite | Level 13

It will not work unless you replace date with hiredate

Data A;

set Shareef.emp;

if year(datepart(hiredate))=1982;

run;

Patrick
Opal | Level 21

Post your SAS code and the SAS log which shows us what exactly is not working.

Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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.   

Patrick
Opal | Level 21

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

Tom
Super User Tom
Super User

Don't know. It is in the view SASHELP.VFUNC. Not sure if it requires SAS/Access to be licensed.

momin
Calcite | Level 5

Hi Tom,

Is that the way to do it?

Thanks

Mohammed

Data B;

set Shareef.emp;

dtextyear(1982);

run;

Tom
Super User Tom
Super User

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 ;

momin
Calcite | Level 5

SORRY, BOTH WAYS DON'T WORK.

THANKS ANYWAYS

MOHAMMED

momin
Calcite | Level 5

tried again.it worked.Thanks for everyone's help.Regards

Mohammed

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 2392 views
  • 0 likes
  • 4 in conversation