BookmarkSubscribeRSS Feed
stevenyan0127
Fluorite | Level 6

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;

Screen Shot 2022-04-08 at 7.09.59 PM.png

 

7 REPLIES 7
HarrySnart
SAS Employee

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. 

 

kris8
Calcite | Level 5
Hi can you do this to repeat a year?
PaigeMiller
Diamond | Level 26
%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;
--
Paige Miller
Kurt_Bremser
Super User

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.

kris8
Calcite | Level 5
how would you do it for a year?
Tom
Super User Tom
Super User

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;

 

 

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
  • 7 replies
  • 1996 views
  • 0 likes
  • 6 in conversation