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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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