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)
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)
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!
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.
Ready to level-up your skills? Choose your own adventure.