SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hope18
Fluorite | Level 6

I would like to extract the only year from the date as a new variable how do I write the program?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

It depends, but usually you would use a data step and the YEAR() function for starters. 

You can run the example below.

 

data year_demo;
set sashelp.stocks;
year = year(date);
run;

@Hope18 wrote:

I would like to extract the only year from the date as a new variable how do I write the program?


 

View solution in original post

4 REPLIES 4
Reeza
Super User

It depends, but usually you would use a data step and the YEAR() function for starters. 

You can run the example below.

 

data year_demo;
set sashelp.stocks;
year = year(date);
run;

@Hope18 wrote:

I would like to extract the only year from the date as a new variable how do I write the program?


 

Hope18
Fluorite | Level 6

Thank you guys this was helpful.

MarkWik
Quartz | Level 8

@Hope18 You could close the thread by marking @Reeza 's answer as accepted solution

ChrisBrooks
Ammonite | Level 13

It depends on how your date is stored - for example if it's a SAS datetime you can use the date part function http://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p0rttbu7w62xgzn1damccyuwpld8.htm&... or if it's a character then the substr function http://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n0n08xougp40i5n1xw7njpcy0a2b.htm&...

 

If you tell us how it's stored we can give you a concrete example.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 115837 views
  • 3 likes
  • 4 in conversation