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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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