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

Hello: Any help with extracting the year from a SAS character variable 

 

Latest_date_Date Char 11 $11.

example 08-mar-2001

End result wanted is a new latest_year = 2001.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16
latest_year=year(input(Latest_date_Date, date11.));
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

6 REPLIES 6
yabwon
Amethyst | Level 16
latest_year=year(input(Latest_date_Date, date11.));
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ama220
Obsidian | Level 7
Thanks for your help!!
Tom
Super User Tom
Super User

If all of the values follow that pattern then just take the 3rd (or last) word when using - as the delimiter.

latest_year = scan(latest_date_date,-1,'-');

Note that will create another character variable.

 

If you want a number you could use INPUT() function to convert that string into a number.

But in the case perhaps you should first use INPUT() to convert the date string into a date.  Then you could use the YEAR() function to take the year part of the date.

ama220
Obsidian | Level 7
Thank you, this also worked. I saw the top one first. I appreciate your help!
Kurt_Bremser
Super User

Make your data intelligent by using SAS date values, then it's a breeze:

data want;
set have (rename=(latest_date_date=_date));
latest_date_date = input(_date,date11.);
drop _date;
format latest_date_date date11.;
latest_year = year(latest_date_date);
run;
ama220
Obsidian | Level 7
Thanks for the detailed response! This worked as well, but I saw the top one first and accepted as the solution.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 6 replies
  • 1417 views
  • 4 likes
  • 4 in conversation