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

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 

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

2 REPLIES 2
Haikuo
Onyx | Level 15

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

Reeza
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 1238 views
  • 0 likes
  • 3 in conversation