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
Onyx | Level 15
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
Onyx | Level 15
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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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