Checking the existence of a variable in SAS code

624 views Asked by At

I am not sure if I get any answer on a saturday evening, but I will give a shout :)

I am trying to automize a SAS-code which does comparison in a viariable between 2-3 companies. In each task, I will probably work with different companies, so I guess I can only enter the company names manually.

I describe macro-variables for each company, for example:

%let C1=CompanyNo1; %let C2=CompanyNo2; %let C3=CompanyNo3;

and then I only put &C1 in the remaining code. If I work for another company no later, then I only change the CompanyNo1 in the code.

However, the problem is that I may need to do the comparison between different number of companies later. If I write the code for 4 companies, and if I need to do the comparison between 2 companies in the next run, then I have to deactivate some part of the code.

So I want to write an existince check in the code like;

data _null_; if &C3 is true then continue the run; else if quit the code. run;

Someone told me that I can do that with %Macro statement(he said maybe, he wasn't sure). But I am not sure how to achieve this with %Macro.

Thanks for any contribution in advance...

2

There are 2 answers

0
user3714330 On BEST ANSWER

Finally I found an effective solution.

%Macro checking;
  %let C3=CompanyNo3;
  %if &SYSERR > 0 %then %do;
     %goto exit;
  %end;

  'other data processes here';

  %exit:   /* Be careful, it is not semicolon  */
 %Mend;

So if there is no CompanyNo3, then SAS gives only a warning that CompanyNo3 is not attended. Then it goes to exit, it doesn't do other data processes. It finalizes the process without error message.

0
Stig Eide On

You can check for existence of macro variables in your code with the %SYMEXIST macro function and SYMEXIST data step function.