DATA Step, Macro, Functions and more

IF-ELSE Condition if a table exists

Reply
Contributor
Posts: 22

IF-ELSE Condition if a table exists

Hello. May I know how to convert my comment (/*         */) into SAS code? Thanks.

%macro loop;

proc sql;

select max(tnx_dt) into: today from TESTING;

quit;

%DO k= %sysevalf("&today"d) %TO  %eval(%sysfunc(today())-2);

proc sql;

select max(tnx_dt) into: today1 from TESTING;

select max(tnx_dt-1) into: yesterday from TESTING;

select max(tnx_dt+1) into: tomorrow from TESTING;

quit;

%let day = %substr(&today1,1,5);

%let dt1 = %substr(&yesterday,1,9);

%let dt2 = %substr(&tomorrow,1,9); 

            /*IF TABLE_RAW_DATA EXISTS, DO THE CODE BELOW */    

            proc sql;

            insert into TESTING values("&dt2"d);

            quit;

            /*ELSE IF TABLE_RAW_DATA DOES NOT EXISTS, DO THE CODE BELOW*/

            proc sql;

            insert into TESTING (tnx_dt)

            select tnx_dt from previous_data;

            quit;

%end;

%mend;

%loop;

Super User
Super User
Posts: 7,942

Re: IF-ELSE Condition if a table exists

Posted in reply to 01SASUser

Probably the easiest way is to use the exist() function, e.g:

%if %sysfunc(exist(WORK.TABLE_RAW_DATA)) %then %do;

You could also query the sashelp.vtable dataset.

Alternatively,, drop the whole macro concept and generate the SAS code from a data _null_; call execute.

Contributor
Posts: 22

Re: IF-ELSE Condition if a table exists

Hello. Thank you for the response.

What if the conditions are:

- IF TABLE_RAW_DATA1 AND TABLE_RAW_DATA2 AND TABLE_RAW_DATA3 EXISTS

- ELSE IF ((TABLE_RAW_DATA1 AND TABLE_RAW_DATA2 EXISTS) AND (TABLE_RAW_DATA3 DOES NOT EXISTS))

May I know how to convert it into SAS code?

Super User
Posts: 7,762

Re: IF-ELSE Condition if a table exists

Posted in reply to 01SASUser

Once your conditions get complicated, I recommend doing them in a data _null_ step and put the result into a macro variable with call symput('cond_var','Y') or call symput('cond_var','N'), respectively. That way you can easily check if it works by simply doing a %put &cond_var after the data _null_ step.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 3 replies
  • 3215 views
  • 7 likes
  • 3 in conversation