Search code examples
if-statementsassas-macro

%if %then %do condition not resolved


I have inherited macro in which the below block does not seem to be resolved. The macro is supposed to conditionally select the logical operations based on the date values. If the %if conditions are satisfied, they should print the macro variables mentioned in the %put statement, which in-turn become part of the if condition. If the condition %if %length(&datein.)=10 OR %length(ST_&date.)=10 is satisfied, it would print the first %put statement else the second %put statement would be printed. Same logic applies to the second %if statement.

The macro is supposed to be run inside a data step. The logical operator AND is being considered as a variable and I'm getting a note NOTE: Variable AND is uninitialized.

I tried to balance the if condition by adding the open and closed parenthesis mentioned in below code. Tried to change the structure of the if condition to separate the first and the second condition, but doesn't seem to work. I assuming that the logical operator AND cannot be used as it is in the %if %then %else statement.

%macro test_date(date=, comp1=, comp2=, label=);
            if &datein. ne "" and ST_&date. ne "" then do;
                if (%if %length(&datein.)=10 OR %length(ST_&date.)=10 %then %put ST_&date._10 &comp1. datein_10; %else %put ST_&date._19 &comp1. datein_19;
                AND %if %length(&datein.)=10 OR %length(ED_&date.)=10 %then %put datein_10 &comp2. ED_&date._10; %else %put datein_19 &comp2. ED_&date._19;)
                then EPOCH=&label.;
            end;
%mend test_date;

%test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');

Solution

  • You will probably need to adapt this a bit, but it should be helpful in clearifying the logic of it. The code below checks the length of macro variables and inserts data step code based on this length. It will also print some values to the log, but that doesn't affect results. I've included some simple data step examples to illustrate how it works.

    %macro test_date(date=, comp1=, comp2=, label=);
        %if &datein. ne  and &&ST_&date. ne  %then %do;
    
            %if %length(&datein.)=10 OR %length(&&ST_&date.)=10 %then %do;
                /*  Insert data step code */
                if variable1=1 then epoch="First condition";
                else epoch="firstelse";
                /*  Print to log*/
                %put &&ST_&date. &comp1. &datein.; 
            %end;
            %else %if %length(&datein.)=10 OR %length(&&ED_&date.)=10 %then %do;
                if variable1=0 then epoch="Second condition";
                else epoch="secondelse";
            %end;
    
        %end;
    %mend test_date;
    
    /*  Macro variables given values of length 10. Outer if and first condition in do block holds.*/
    %let datein=0123456789;
    %let st_screen=0123456789;
    %let ed_screen=0123456789;
    
    data test1;
        variable1=1;
        %test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
    run;
    
    
    data test2;
        variable1=0;
        %test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
    run;
    
    /*  Macro variables given values of length 9 and 10. Outer if and else condition in do block holds.*/
    %let datein=012345678;
    %let st_screen=012345678;
    %let ed_screen=0123456789;
    
    
    data test3;
        variable1=1;
        %test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
    run;
    
    data test4;
        variable1=0;
        %test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
    run;
    
    /*  Macro variables are empty. Outer if is false. The macro does nothing.  */
    %let datein=;
    %let st_screen=;
    %let ed_screen=;
    
    
    data test5;
        variable1=1;
        %test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
    run;
    
    data test6;
        variable1=0;
        %test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
    run;