prathyu
prathyu

Reputation: 47

SAS conditional logic to execute another sas program based on condition

I have a dataset naming error_table as follows. All the variables are character

Errorno   Error     Resolution  
001     login       check 
002     datacheck   check

I wanted a logic that executes a sas program If the Errorno is not in 001 and 002. Else stop execution and display the error_table.

I tried the following

%macro test();
   proc sql;
      select trim(Error_No) into: num from error_table;
   quit;

   %if &num. not in ("001","002") %then %do;
       %include "/path/dev/program.sas";
   %end;
   %else %do;
      proc print data = error_table;
      run;
   %end;
%mend;
%test;

But, it is throwing an error.

Can anyone please correct the logic.

Upvotes: 1

Views: 1411

Answers (2)

fl0r3k
fl0r3k

Reputation: 629

I would sugest moving condition with error codes to proc sql.

proc sql;
   select count(*) into :num_errors
   from error_table
   where Errorno in ("001", "002");
quit;

Then in macrovariable you have number of errors that are 001 or 002. Next step is to check macro-condition:

%if &num_errors. > 0 %then %do;
   %include "/path/dev/program.sas";
%end;
%else %do;
   proc print data = error_table;
   run;
%end;
%mend;

Upvotes: 0

Tom
Tom

Reputation: 51566

You need to watch out for the case when the SELECT returns zero rows. You should set a default value to the macro variable NUM.

Is your dataset variable numeric or character? Use the TRIMMED or SEPARATED BY clause instead of the TRIM() function to prevent spaces in the macro variable that is generated by the INTO clause.

%let num=NONE;
select Error_No into: num trimmed from error_table;

Remember that to the macro processor everything is a string so don't but quotes around the values you are trying to match unless they are actually part of the value.

%if NOT (&num. in (001,002)) %then %do;

Also to use the IN operator in macro code you need to make sure you have set the MINDELIMITER option.

Upvotes: 0

Related Questions