BookmarkSubscribeRSS Feed
christinagting0
Quartz | Level 8

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?

 

 

 

 

 

 

 

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

jklaverstijn
Rhodochrosite | Level 12

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.

ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 470 views
  • 0 likes
  • 4 in conversation