Home > Enterprise >  How to make multiple conditional statements in SAS
How to make multiple conditional statements in SAS

Time:09-28

How do i make more conditional statements than two? this i my current code: It look for a table and if it does not exist another table is used. But if the second table doesn't exist either how can I extent the conditional statement to use yet another table?

PROC SQL;
%if(%sysfunc(exist(&str_YYMMN6_at1))) %then %do;

    CREATE TABLE Kunde_REA_UDL_2 AS 
    SELECT (input(t2.cpr_se,w.)) AS KundeNum, 
          t1.ExpoKat AS Z_ORDINATE
    FROM &str_YYMMN6_at1
    LEFT JOIN &str_PREV_YYMMN6_at1 ON (t1.F001_id_unique = t2.F001_id_unique);
%end;


%else %do;

    CREATE TABLE Kunde_REA_UDL_2 AS 
    SELECT (input(t2.cpr_se,w.)) AS KundeNum, 
          t1.ExpoKat AS Z_ORDINATE
    FROM &strYYMMN6_at2
    LEFT JOIN &str_PREV_YYMMN6_at2 ON (t1.F001_id_unique = t2.F001_id_unique);
%end;
QUIT;

I hope point me in a direction.

CodePudding user response:

@Negdo has already answered it. Just for clarity; below is the code:

PROC SQL;
%if(%sysfunc(exist(&str_YYMMN6_at1))) %then %do;

    CREATE TABLE Kunde_REA_UDL_2 AS 
    SELECT (input(t2.cpr_se,w.)) AS KundeNum, 
          t1.ExpoKat AS Z_ORDINATE
    FROM &str_YYMMN6_at1
    LEFT JOIN &str_PREV_YYMMN6_at1 ON (t1.F001_id_unique = t2.F001_id_unique);
%end;

%else %if (%sysfunc(exist(&str_YYMMN6_at2))) %then %do;

    CREATE TABLE Kunde_REA_UDL_2 AS 
    SELECT (input(t2.cpr_se,w.)) AS KundeNum, 
          t1.ExpoKat AS Z_ORDINATE
    FROM &strYYMMN6_at2
    LEFT JOIN &str_PREV_YYMMN6_at2 ON (t1.F001_id_unique = t2.F001_id_unique);
%end;

%else %if (%sysfunc(exist(&str_YYMMN6_at3))) %then %do;

    CREATE TABLE Kunde_REA_UDL_2 AS 
    SELECT (input(t2.cpr_se,w.)) AS KundeNum, 
          t1.ExpoKat AS Z_ORDINATE
    FROM &strYYMMN6_at2
    LEFT JOIN &str_PREV_YYMMN6_at3 ON (t1.F001_id_unique = t2.F001_id_unique);
%end;


%else %do;

    CREATE TABLE Kunde_REA_UDL_2 AS 
    SELECT (input(t2.cpr_se,w.)) AS KundeNum, 
          t1.ExpoKat AS Z_ORDINATE
    FROM &strYYMMN6_at2
    LEFT JOIN &str_PREV_YYMMN6_at4 ON (t1.F001_id_unique = t2.F001_id_unique);
%end;
QUIT;

This can go on and on as per your requirement.

CodePudding user response:

You can just add %else %if %then %do statement. In this case only first true if condition will execute.

Or if multiple if statements are allowed to execute (or are impossible to happen simultaneous), you can just use %if %then %do statement. In this case every true if condition will execute.

And you can have any number of those if statements.

Last %else %do happens if none of if statements executed. If you don't include it, then in case none if condition was true, nothing happens.

In this case SAS is not all that different than other programing languages.

  • Related