I have a date column that I would like to break up into Year, Month, Day, however it is in a format that is not regularly condusive to usual SAS methods.
It is a character variable and set up YYYYMMDD. How would I break this up so it could be YYYY in one column, MM in another, and DD in a third?
Let me know if you need any other information.
substring function is one option. For example, year = substr(your_yyyymmdd_var, 1, 4);
For conversion to a numeric field, nest it in an input function:
year = input(substr(var, 1, 4), best.);
Do likewise for the three other columns.
Or, convert to a date and extract the year, month, and day:
year = year(input(var, yymmdd8.);
etc.
I would recommend converting it to a SAS date and then using the YEAR/MONTH/DAY functions.
Note that if you apply formats to your date they get grouped according to the format, so having a SAS date is advantageous.
Here's a quick example that shows the calculating of yearly statistics from a data set.
proc means data=sashelp.stocks min max mean;
class date;
format date year4.;
var open high low;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.