Search code examples
sassas-macro

SAS %DO %UNTIL compare two variables


I have a dataset A1 as below.

ID COL1    COL2    COL3  COL4  COL5.....
A  04JAN21 05JAN21 16JAN21  01FEB21 06FEB21....
B  19DEC20  01JAN21  02JAN21  18JAN21 31MAR21....
C ........
D ........`

ID column is unique and COL1, COL2...are SAS Date9. format. I am trying to compare (COLi)+33 days and COL(i+3). If (COLi)+33 days>COL(i+3), it will count 1.

My SAS CODE is below:

DATA _NULL_;
    SET A1;
    CALL SYMPUT('NO',LEFT(PUT(_N_,6.)));
RUN;
%MACRO TEST();
DATA A2;
    SET A1;
    %DO i=1 %TO &NO.;
       z=0;
    %DO %UNTIL(%SYSFUNC(COL%eval(&j+3))=.);
   %IF %SYSFUNC(COL%eval(&j+3))<INTNX('day',COL&j,33) %THEN %DO;
   %LET z=%eval(z+1);
    %END;
   %END;
  %END;
RUN;
%MEND;
%TEST;

Above example, A case has case 1 & 4, case 2 & 5, counting 2 times. B case only has case 1 & 4.

Pls help see what syntax error I have. Many thanks.


Solution

  • Macro code is for generating code, not manipulating data.

    It is not clear what you are trying to count, but you probably have no need to generate code. Just loop over an array to perform your testing.

    DATA A2;
      SET A1;
      array col col1-col5 ;
      z=0;
      do j=1 to dim(col)-3;
        if col[j+3] < col[j]+33 then z=z+1;
      end;
    RUN;