title: sas macros for incrementing date
body: <p>My codes are:</p>
  
  <pre><code>libname " Cp/mydata"
  options ;
  %let yyyymmdd=20050210;
  %let offset=0;
  %let startrange=0;
  %let endrange=0;
  
  /* MACRO FOR INCREMENTING THE DATE */
  %macro base(yyyymmdd=, offset=);
  %local date x ds; /* declare macro variables with local scope */
  %let date=%sysfunc(mdy(%substr(&amp;yyyymmdd,5,2)
                     ,%substr(&amp;yyyymmdd,7,2)
                     ,%substr(&amp;yyyymmdd,1,4))); /* convert yyyymmdd to SAS date */
  %let loopout=100;/* hardcoded - number of times to check whether ds exists */
  %do x=&amp;offset %to &amp;loopout; /* begin loop */
     /* convert &amp;date to yyyymmdd format */
     %let ds=AQ.CO_%sysfunc(intnx(day,&amp;date,&amp;offset),yymmddn8.); 
     %if %sysfunc(exist( &amp;ds )) %then %do;
        %put &amp;ds exists!;
        &amp;ds /* write out the dataset, if it exists */
        %let x=&amp;loopout; /* exit loop */
     %end;
     %else %do;
        %put &amp;ds does not exist - checking subsequent day;
        %let date=&amp;date+1;
     %end;
  %end;
  %mend;
  
  %macro loop(yyyymmdd=, startrange=, endrange=);
  %local date x ds;
  %let date=%sysfunc(mdy(%substr(&amp;yyyymmdd,5,2)
                        ,%substr(&amp;yyyymmdd,7,2)
                        ,%substr(&amp;yyyymmdd,1,4)));
  data x;
  set set %base(yyyymmdd=&amp;yyyymmdd, offset=0)
  /* loop through each specific dataset, checking first whether it exists.. */
  %do x=&amp;startrange %to &amp;endrange;
     %let ds=AQ.CO_%sysfunc(intnx(day,&amp;date,&amp;x),yymmddn8.);
     %if %sysfunc(exist( &amp;ds )) %then %do;
        &amp;ds
     %end;
  %end;
  ;
  run;
  %mend; 
  </code></pre>
  
  <p>This was the error generated when I tried to run this macro.</p>
  
  <p>data temp;</p>
  
  <p>58         set %loop(yyyymmdd=&amp;yyyymmdd, startrange=&amp;startrange,
  58       ! endrange=&amp;endrange);   </p>
  
  <p>ERROR: File WORK.DATA.DATA does not exist.</p>
  
  <p>ERROR: File WORK.X.DATA does not exist.</p>
  
  <p>AQ.CO_20050210 does not exist - checking subsequent day</p>
  
  <p>AQ.CO_20050211 does not exist - checking subsequent day</p>
  
  <p>AQ.CO_20050212 exists!</p>
  
  <p>NOTE: The system stopped processing this step because of errors.</p>
  
  <p>I want help on two things:</p>
  
  <p>1) Here, I'm trying to increment my date by 1 or 2 or so on if that date is not there in my original dataset. Please help to make this macro work fine.</p>
  
  <p>2)I would like to have another column ie work.date in my data that will have 0 or 1(1 if the specified date yyyymmdd exist in our original data and 0 if I'm incrementing). Please make the specified changes in my macro.
  Thanks in advance!!</p>