Hi, The current code generates 10 observations and 10 dates. I would like to repeat this sequence of dates 10 times so that there will 100 observations and the dates should be in that order during the repetition. for example obs 1-10, 11-20, 21-30, etc will all have the same set of dates. Any help would be appreciated. Thanks!
%let start_date=01Jan2008;
%let end_date=10Jan2008;
data Dates;
date="&start_date"d;
do while (date<="&end_date"d);
output;
date=intnx('day', date, 1, 's');
end;
format date date9.;
run;
Hi @stevenyan0127 , is this what you're after?
%macro dateGenerator(start,end,multi,out);
*define date range;
%let start_date=&start;
%let end_date=&end;
*generate list of dates;
data Dates;
date="&start_date"d;
do while (date<="&end_date"d);
output;
date=intnx('day', date, 1, 's');
end;
format date date9.;
run;
*initialise base table;
data dates_out;
set dates;
run;
*Set counter from desired multiplier;
%let counter=%SYSEVALF(&multi - 1);
*loop through multiplier adding dates table;
%do i=1 %to &counter;
data dates_out;
set dates_out dates;
run;
%end;
*create output table with rownumber;
data &out;
set dates_out;
rownum+1;
run;
%mend;
%dateGenerator(01Jan2008,10Jan2008,10,myDates);
I've used your original code and effectively put it into a loop which multiplies the number of records.
%let start_date=01Jan2008;
%let end_date=10Jan2008;
data Dates;
date="&start_date"d;
count=0;
do while (date<="&end_date"d);
count=count+1;
do i=1 to 10;
n=(i-1)*10+count;
output;
end;
date=date+1;
end;
format date date9.;
drop count i;
run;
proc sort data=dates;
by n;
run;
Nested DO loop:
%let start_date=01Jan2008;
%let end_date=10Jan2008;
data Dates;
do i = 1 to 10;
do date = "&start_date."d to "&end_date."d;
output;
end;
end;
format date date9.;
drop i;
run;
Since dates are counts of days, it is not necessary to use the INTNX function to increment daily. INTNX is mainly needed for larger, irregular intervals like months, quarters or years.
Just set the start and end dates accordingly, and the loop will work through all 365 (or 366 in a leap year) days of the year.
Why use a DO WHILE() when you are just iterating? Why not just a normal old regular DO loop?
Add another loop to generate the 10 groups.
data Dates;
do group=1 to 10;
do date="&start_date"d to "&end_date"d ;
output;
end;
end;
format date date9.;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.