BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jswinford
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ebowen
Quartz | Level 8
Probably the easiest way would be to use a substr() function combined with the input() function. It would look something like this:

data out;
set in;
year=input(substr(date,1,4),4.0);
month=input(substr(date,5,2),2.0);
day=input(substr(date,7,2),2.0);
run;

You could also use an informat to turn the character variable into a date variable:

data test1;
format date mmddyy8.;
input
date : yymmdd8.
;
datalines;
20170816
20160421
20150725
;
run;

View solution in original post

3 REPLIES 3
collinelliot
Barite | Level 11

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.

ebowen
Quartz | Level 8
Probably the easiest way would be to use a substr() function combined with the input() function. It would look something like this:

data out;
set in;
year=input(substr(date,1,4),4.0);
month=input(substr(date,5,2),2.0);
day=input(substr(date,7,2),2.0);
run;

You could also use an informat to turn the character variable into a date variable:

data test1;
format date mmddyy8.;
input
date : yymmdd8.
;
datalines;
20170816
20160421
20150725
;
run;
Reeza
Super User

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;

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1111 views
  • 0 likes
  • 4 in conversation