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

Hi Sas experts,

 

I am missing something in my code that enables me to write a do loop to iterate by month & year.

 

I have two dates stored in two different variables "Start_Date" and "End_Date" and I want to expand these dates by Month & Year as opposed to expanding every day in between.

 

The issue is my start date in the second do loop where it starts fine but doesn't start the next year correctly. But I am unsure how to modify this to make it work.

 

Any help would be greatly appreciated.

 

 

 

 

This is what I have in terms of code:

 

data want;
set have;

do year=year(Start_Date) to year(End_Date);

do month= month(Start_Date) to 12;
date =mdy(month,1,year);
format date Date9.;
output;
end;

end;

run;

 

 

Output is:

Var AStart_DateEnd_DateVar Byearmonthdate
1017/01/20141/01/203050003201471-Jul-14
1017/01/20141/01/203050003201481-Aug-14
1017/01/20141/01/203050003201491-Sep-14
1017/01/20141/01/2030500032014101-Oct-14
1017/01/20141/01/2030500032014111-Nov-14
1017/01/20141/01/2030500032014121-Dec-14
1017/01/20141/01/203050003201571-Jul-15
1017/01/20141/01/203050003201581-Aug-15
1017/01/20141/01/203050003201591-Sep-15
1017/01/20141/01/2030500032015101-Oct-15
1017/01/20141/01/2030500032015111-Nov-15
1017/01/20141/01/2030500032015121-Dec-15
1017/01/20141/01/203050003201671-Jul-16
….      
16432/01/20151/01/203020083201521-Feb-15
16432/01/20151/01/203020083201531-Mar-15
16432/01/20151/01/203020083201541-Apr-15
16432/01/20151/01/203020083201551-May-15
16432/01/20151/01/203020083201561-Jun-15
16432/01/20151/01/203020083201571-Jul-15
16432/01/20151/01/203020083201581-Aug-15
16432/01/20151/01/203020083201591-Sep-15
16432/01/20151/01/2030200832015101-Oct-15
16432/01/20151/01/2030200832015111-Nov-15
16432/01/20151/01/2030200832015121-Dec-15
16432/01/20151/01/203020083201621-Feb-16
16432/01/20151/01/203020083201631-Mar-16

 

Result Should be:

 

Var AStart_DateEnd_DateVar Byearmonthdate
1017/01/20141/01/203050003201471-Jul-14
1017/01/20141/01/203050003201481-Aug-14
1017/01/20141/01/203050003201491-Sep-14
1017/01/20141/01/2030500032014101-Oct-14
1017/01/20141/01/2030500032014111-Nov-14
1017/01/20141/01/2030500032014121-Dec-14
1017/01/20141/01/203050003201511-Jan-15
1017/01/20141/01/203050003201521-Feb-15
1017/01/20141/01/203050003201531-Mar-15
1017/01/20141/01/203050003201541-Apr-15
1017/01/20141/01/203050003201551-May-15
1017/01/20141/01/203050003201561-Jun-15
1017/01/20141/01/203050003201671-Jul-15
….      
16432/01/20151/01/203020083201521-Feb-15
16432/01/20151/01/203020083201531-Mar-15
16432/01/20151/01/203020083201541-Apr-15
16432/01/20151/01/203020083201551-May-15
16432/01/20151/01/203020083201561-Jun-15
16432/01/20151/01/203020083201571-Jul-15
16432/01/20151/01/203020083201581-Aug-15
16432/01/20151/01/203020083201591-Sep-15
16432/01/20151/01/2030200832015101-Oct-15
16432/01/20151/01/2030200832015111-Nov-15
16432/01/20151/01/2030200832015121-Dec-15
16432/01/20151/01/203020083201611-Jan-16
16432/01/20151/01/203020083201621-Feb-16
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Use functions INTNX and INTCK for date manipulations:

 

data want;
set have;
date = Start_Date;
do until(date > End_Date);
    month = month(date);
    output;
    date = intnx("MONTH", date, 1, "SAME");
    end;
format date Date9.;
run;
PG

View solution in original post

2 REPLIES 2
PGStats
Opal | Level 21

Use functions INTNX and INTCK for date manipulations:

 

data want;
set have;
date = Start_Date;
do until(date > End_Date);
    month = month(date);
    output;
    date = intnx("MONTH", date, 1, "SAME");
    end;
format date Date9.;
run;
PG
Go210
Obsidian | Level 7
Thanks PGStats! I will read up on those functions.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8629 views
  • 4 likes
  • 2 in conversation