08-19-2016 09:14 AM
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?
08-19-2016 10:14 AM
You are missing a set statement to set an input dataset. That step has no data:
set <yourdataset>; tx_year=year(orp_tx_start); run;
Also note that orp_tx_start needs to be a numeric SAS datetime.
08-19-2016 10:26 AM
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.
08-19-2016 11:05 AM
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;
format orp_tx_start year4.;
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.