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

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

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

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


Accepted Solutions
Solution
‎08-17-2014 11:08 PM
Trusted Advisor
Posts: 1,228

Re: 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;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,228

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

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;

Occasional Contributor
Posts: 14

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

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.

Solution
‎08-17-2014 11:08 PM
Trusted Advisor
Posts: 1,228

Re: 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;

Occasional Contributor
Posts: 14

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

No,doesn't work!

Respected Advisor
Posts: 4,130

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

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

Super User
Super User
Posts: 6,842

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

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.

Super User
Super User
Posts: 6,842

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

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.   

Respected Advisor
Posts: 4,130

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

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

Super User
Super User
Posts: 6,842

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

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

Occasional Contributor
Posts: 14

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

Hi Tom,

Is that the way to do it?

Thanks

Mohammed

Data B;

set Shareef.emp;

dtextyear(1982);

run;

Super User
Super User
Posts: 6,842

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

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 ;

Occasional Contributor
Posts: 14

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

SORRY, BOTH WAYS DON'T WORK.

THANKS ANYWAYS

MOHAMMED

Respected Advisor
Posts: 4,130

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

What is not working? Post your code and log.

Occasional Contributor
Posts: 14

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

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

Mohammed

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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