How to break up a series of values in SAS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

How to break up a series of values in SAS

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.


Accepted Solutions
Solution
‎09-01-2017 12:20 PM
Contributor
Posts: 32

Re: How to break up a series of values in SAS

Posted in reply to jswinford
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


All Replies
PROC Star
Posts: 311

Re: How to break up a series of values in SAS

Posted in reply to jswinford

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.

Solution
‎09-01-2017 12:20 PM
Contributor
Posts: 32

Re: How to break up a series of values in SAS

Posted in reply to jswinford
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;
Super User
Posts: 23,337

Re: How to break up a series of values in SAS

Posted in reply to jswinford

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;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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