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;

 

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1084 views
  • 0 likes
  • 6 in conversation