DATA Step, Macro, Functions and more

Date from wide format to Long

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

Date from wide format to Long

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 


Accepted Solutions
Solution
‎04-22-2014 10:58 AM
Respected Advisor
Posts: 3,156

Re: Date from wide format to Long

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


All Replies
Solution
‎04-22-2014 10:58 AM
Respected Advisor
Posts: 3,156

Re: Date from wide format to Long

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

Super User
Posts: 19,772

Re: Date from wide format to Long

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;

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 215 views
  • 0 likes
  • 3 in conversation