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!
latest_year=year(input(Latest_date_Date, date11.));
latest_year=year(input(Latest_date_Date, date11.));
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.
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.