I am a new user of SAS and have taken a fair number of classes. But SAS has so many procedures I get lost easily. I am looking to change a date range into individual observations by month. For the life of me i can not find anything on how to make this happen. I am likely searching the wrong terminology. I found the expand procedure that sounded close but I had a hard time understanding it.
Data in
UnitID StartDate EndDate
1234 1/1/2012 4/1/2012
1234 1/1/2014 2/1/2014
4321 10/1/2013 2/1/2014
Data Out
UnitID Month Year
1234 1 2012
1234 2 2012
1234 3 2012
1234 4 2012
1234 1 2013
4321 10 2013
4321 11 2013
4321 12 2013
4321 1 2014
There is a discrepancy in your output, you have included the end month for the first row of your raw data, but not for the rest. The following code choose to include the end month, it can be easily tweaked otherwise.
data have;
input UnitID $ (StartDate EndDate) (:mmddyy10.);
cards;
1234 1/1/2012 4/1/2012
1234 1/1/2014 2/1/2014
4321 10/1/2013 2/1/2014
;
data want;
set have;
do i=0 by 1 to intck('month',startdate,enddate);
month=month(intnx('month',startdate,i,'b'));
year=year(intnx('month',startdate,i,'b'));
output;
end;
keep unitid month year;
run;
Haikuo
There is a discrepancy in your output, you have included the end month for the first row of your raw data, but not for the rest. The following code choose to include the end month, it can be easily tweaked otherwise.
data have;
input UnitID $ (StartDate EndDate) (:mmddyy10.);
cards;
1234 1/1/2012 4/1/2012
1234 1/1/2014 2/1/2014
4321 10/1/2013 2/1/2014
;
data want;
set have;
do i=0 by 1 to intck('month',startdate,enddate);
month=month(intnx('month',startdate,i,'b'));
year=year(intnx('month',startdate,i,'b'));
output;
end;
keep unitid month year;
run;
Haikuo
This is a data step problem, where you can use the intnx to increment your date by a month and then a do while loop to loop until you reach the end of the period.
You should look into the intnx function to make sure you specify the appropriate options for your requirements.
data want;
set have;
date=startDate;
do while(date<endDate);
month=month(date);
year=year(date);
output;
date=intnx('month', date, 1, 'b');
end;
format month yearmon7.;
drop startdate enddate;
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.