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

I have this data set:

 

Obs.     Indate     

1          1/7/2011

2          3/7/2012

3          5/4/2014

4          1/9/2011

5          8/9/2013

6          1/1/2014

 

How do I create a new variable (year) which looks like this:

 

Obs.     Year

1          2011

2          2012

3          2014

4          2011

5          2013

6          2014

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just to add, if its a character variable, then you might be better off with:

year=scan(datevar,3,"/");  

If you input it to date and then take a year, you will need to handle the possiblity of missing or invalida dates.  The scan will take the third piece of text delimited by /.

View solution in original post

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hi @Gothardt,

 

If variable INDATE is numeric, you can define

Year=year(indate);

Otherwise, the definition could read

Year=year(input(indate, mmddyy10.));

provided that INDATE is in MM/DD/YYYY format. If it's DD/MM/YYYY format, please replace mmddyy10. by ddmmyy10. in the above code.

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just to add, if its a character variable, then you might be better off with:

year=scan(datevar,3,"/");  

If you input it to date and then take a year, you will need to handle the possiblity of missing or invalida dates.  The scan will take the third piece of text delimited by /.

FreelanceReinh
Jade | Level 19

Good point from @RW9. The SCAN technique would also be more robust in case of leading blanks
("    1/7/2011").

 

Edit: By default, this would result in a character variable YEAR, whereas the approach using the YEAR function yields a numeric variable.

ballardw
Super User

And depending on what you are doing, if the value is currently a SAS date value use the Year format to display as needed.

 

Example:

 

data example;

   x='05APR2016'd;

run;

proc print data=have;

   var x;

   format x year4.;

run;

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2207 views
  • 5 likes
  • 4 in conversation