jeudi 15 décembre 2016

How to elegantly write multiple if-else conditions in SAS?

I have the following if-else conditions within a SAS macro:

%if &restart_flg = Y %then %do;
%if %sysfunc(exist(&library.f2)) %then %do;
proc sql;
Connect to xxxxx as dbcon (user=xx pw=xx server=xx port=xxx database=xxxxx);

select * into :prcs_flag_cnt    
from connection to dbcon  (
select count(1)::smallint
from &library.f2
where flag = 1 and pflag <> 'N' 
);
quit;

%put Process count flag: &prcs_flag_cnt;

%if &prcs_flag_cnt > 0 %then %do;
%let rflag = Y;
%end;

%else %do;
%let rflag  = N;
%end;

%end;

%else %do;
%let rflag = N;
%end;

%end;

%else %do;
%let rflag = N;
%end;

I am basically checking if a particular execution is a fresh execution or a restarted one and populate the rflag accordingly (Y for restart, N for fresh start). Firstly I am checking if restart_flg is Y, if so, I check the existence of a SAS dataset (flags2), and if it exists, I check if any of the records exist with "flag = 1 and pflag <> 'N'" condition and then populate Y or N for rflag accordingly. If the restart_flg is N in first place, the rflag is set to N.

I find the way multiple if conditions written in the code to be less elegant. Is there any better way of writing this if-else condition or accomplishing this functionality?

Thanks!

Aucun commentaire:

Enregistrer un commentaire