How can I extract the year from a sas date variable?

Reply
Frequent Contributor
Posts: 88

How can I extract the year from a sas date variable?

I have a variable that is in SAS date formate and I want to extract just the year from this into a new variable.

 

I tried this code but I'm not sure what I'm doing wrong

 

data orpwork;
tx_year=year(orp_tx_start);
run;

How do I extract the year from my date variable into a new variable?

 

 

 

 

 

 

 

Super User
Super User
Posts: 7,392

Re: How can I extract the year from a sas date variable?

You are missing a set statement to set an input dataset.  That step has no data:

data orpwork;
set <yourdataset>; tx_year=year(orp_tx_start); run;

Also note that orp_tx_start needs to be a numeric SAS datetime. 

Super Contributor
Posts: 408

Re: How can I extract the year from a sas date variable?

The short answer here is: use the year() function. But you did that and still you have this question. Did something go wrong? Please provide us your logs for additionsal info.

 

What I can see in your code is that the datastep lacks an input. We would expect a SET or an INFILE statement. That inoput should provide variable orp_tx_start.

 

Regards,

- Jan.

Super User
Posts: 10,460

Re: How can I extract the year from a sas date variable?

Search your online documentation for SAS functions involving Date and time. There are many functions to extract and manipulate the data. And sometimes you don't need to "extract" at all.

If you data is a date then you can display and use only the year value for most analyis by assigning a format

 

proc print data =yourdatasetname;

   var orp_tx_start;

   format orp_tx_start year4.;

run;

 

will display the value as a 4 digit year.

 

Other formats can display week of the year, year and quarter, year and month, day of the week, calendar quarter, month.

Ask a Question
Discussion stats
  • 3 replies
  • 270 views
  • 0 likes
  • 4 in conversation