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