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;
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!
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.