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

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