Nimit_ZZ
Nimit_ZZ

Reputation: 495

Controlling program flow - sas

Below is the code to execute a set of data-steps based on the value of the increment variable "i". Since I have assigned the value of i to 1 (numeric and not character value). Ideally the first data-step block need to execute, but in the below case the second data-step block is executing.

%put &i. ; prints 1 in to the log window.

%macro DSN;
%let i = 1 ;
data new_DSN;
run;
%if i = 1 %then %do;    
    data Dummy ;
    run;
    data DUMMY_ ;
        set DUMMY new_DSN ;
    run;
%end;
%else %if i ^= 1 %then %do ;
    data DUMMY_ ;
       set DUMMY_ new_DSN ;
    run;
%end;
%mend DSN;

%DSN;

Upvotes: 1

Views: 102

Answers (1)

Vasilij Nevlev
Vasilij Nevlev

Reputation: 1449

Your IF statement is not calling &I macro variable, but simply comparing string I to 1. This also explains why your second loop running because technically speaking string "I" is not equal to "1". You just need to put ampersand in front of I in both %IF statements. I also put two %PUT statements to easier see where code is running. See below:

%macro DSN;
%let i = 1 ;
data new_DSN;
run;
%if &i = 1 %then %do; 
    %PUT First Loop Run;  
    data Dummy ;
    run;
    data DUMMY_ ;
        set DUMMY new_DSN ;
    run;
%end;
%else %if &i ^= 1 %then %do ;
    %PUT Second Loop Run; 
    data DUMMY_ ;
       set DUMMY_ new_DSN ;
    run;
%end;
%mend DSN;

%DSN;

enter image description here

Upvotes: 5

Related Questions