BookmarkSubscribeRSS Feed
BhargavDesai
Calcite | Level 5

How do I output this to a dataset?

 

This example shows how to use macro logic and a macro %DO loop to loop through a starting and ending date.



%macro date_loop(start,end);
/*converts the dates to SAS dates*/
%let start=%sysfunc(inputn(&start,anydtdte9.));
%let end=%sysfunc(inputn(&end,anydtdte9.));
/*determines the number of months between the two dates*/
%let dif=%sysfunc(intck(month,&start,&end));
%do i=0 %to &dif;
/*advances the date i months from the start date and applys the DATE9. format*/
  %let date=%sysfunc(putn(%sysfunc(intnx(month,&start,&i,b)),date9.));
  %put &date;
%end;
%mend;

%date_loop(01jun2008,01sep2009)
2 REPLIES 2
Reeza
Super User

You would do that in a data step, not in macro logic. I've left the macro variables but that isn't needed either.

 

%let start="01Jan2018"d;
%let end="01Dec2018"d;

data want;
n_intervals = intck('month', &start, &end);

do i=0 to n_intervals;
   date = intnx('month', &start, i, 'b');
    output;
end;

run;

@BhargavDesai wrote:

How do I output this to a dataset?

 

This example shows how to use macro logic and a macro %DO loop to loop through a starting and ending date.



%macro date_loop(start,end);
/*converts the dates to SAS dates*/
%let start=%sysfunc(inputn(&start,anydtdte9.));
%let end=%sysfunc(inputn(&end,anydtdte9.));
/*determines the number of months between the two dates*/
%let dif=%sysfunc(intck(month,&start,&end));
%do i=0 %to &dif;
/*advances the date i months from the start date and applys the DATE9. format*/
  %let date=%sysfunc(putn(%sysfunc(intnx(month,&start,&i,b)),date9.));
  %put &date;
%end;
%mend;

%date_loop(01jun2008,01sep2009)



BhargavDesai
Calcite | Level 5
Thanks.

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
  • 1048 views
  • 0 likes
  • 2 in conversation