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

Hello all,

 

 How do I create a new variable for each month between two dates?

 

IDStartdateEnddate
101JAN201606JUN2016

 

What I want is:

 

IDStartdateEnddateNewdate
101JAN201606JUN2016JAN2016
101JAN201606JUN2016FEB2016
101JAN201606JUN2016MAR2016
101JAN201606JUN2016APR2016
101JAN201606JUN2016MAY2016
101JAN201606JUN2016JUN2016

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @raajdesaii  Your WANT seems to suggest you want the monthly sequences between start and end in monyy7. format. I guess there's nothing more. Or perhaps, it's just a matter of ways to write a loop is the question with dates being mere integers-


data have;
input ID	(Startdate	Enddate) (:date9.);
format Startdate	Enddate date9.;
cards;
1	01JAN2016	06JUN2016
;

data want;
 set have;
 new_date=startdate;
 do while(new_date<Enddate);
  output;
  new_date=intnx('mon',new_date,1);
 end;
 format new_date monyy7.;
run;

View solution in original post

5 REPLIES 5
singhsahab
Lapis Lazuli | Level 10
data want;
set have;
st_month=month(startdate);
en_month=month(Enddate);
do i=st_month-1 to en_month-1;
 new_date=cat(put(st_month+i,z2.),' ',put(year(startdate),z4.));
output;
end;
drop i st_month en_month;
run;

you have to apply a user define format to convert month number in character format.

PhilC
Rhodochrosite | Level 12

I agree with @RichardDeVen and @PGStats , and  recommend using the INTNX function.  See our answers to your other post:

Re: How do I count unique observation by creating ... - SAS Support Communities

novinosrin
Tourmaline | Level 20

Hi @raajdesaii  Your WANT seems to suggest you want the monthly sequences between start and end in monyy7. format. I guess there's nothing more. Or perhaps, it's just a matter of ways to write a loop is the question with dates being mere integers-


data have;
input ID	(Startdate	Enddate) (:date9.);
format Startdate	Enddate date9.;
cards;
1	01JAN2016	06JUN2016
;

data want;
 set have;
 new_date=startdate;
 do while(new_date<Enddate);
  output;
  new_date=intnx('mon',new_date,1);
 end;
 format new_date monyy7.;
run;
raajdesaii
Fluorite | Level 6

Thank you. This worked!

 

Ksharp
Super User

data have;
input ID	(Startdate	Enddate) (:date9.);
format Startdate	Enddate date9.;
cards;
1	01JAN2016	06JUN2016
;
data want;
 set have;
 do date= Startdate	to Enddate;
  if month ne month(date) then do;new_date=date;month=month(date);output;end;
 end;
 format new_date monyy.;
 drop month date;
run;

Just for some fun.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 8602 views
  • 2 likes
  • 5 in conversation