Help using Base SAS procedures

Conditional Macros

Reply
Regular Contributor
Posts: 155

Conditional Macros

Hi,

I am getting an error when doing the below logic

%MACRO LOOP ;

data cat;

set rpt;

if var = 'x' THEN CALL SYMPUT("var0",trim(name0)); else

                            if var = 'y' THEN CALL SYMPUT("var1",trim(NAME1)); else

                            if var = 'z' THEN CALL SYMPUT("var2",trim(NAME2)); else

                            if var = 'l' THEN CALL SYMPUT("var3",trim(NAME3)); else

                            if var = 'k' THEN CALL SYMPUT("var4",trim(NAME4));

RUN;

   %IF "&var0" NE "" %THEN %FTP_LOOP(&var0);
   %IF "&var1" NE "" %THEN %LOOP1(&var1);
   %IF "&var2" NE "" %THEN %LOOP1(&var2);
   %IF "&var3" NE "" %THEN %LOOP1(&var3);
   %IF "&var4" NE "" %THEN %LOOP1(&var4);
  

%mend loop;

%macro loop1;

''''

'''

%mend loop1;

I am calling loop1 macro only when there are values in "var" variables,but when i am executing if any one of the var variable is not resolved it is taking the condition as true and running it.

This is causing the job to abend.

warning &var1 not resolved

%IF &var1  NE " " is true.


How to overcome this situation?

Trusted Advisor
Posts: 1,131

Re: Conditional Macros

could you please show us the sample data in the rpt dataset.

Thanks,

Jagadish

Thanks,
Jag
Super Contributor
Posts: 282

Re: Conditional Macros

Hi,

Generally speaking, macro code is executed before data step code, so this means the macro code:

%IF "&var0" NE "" %THEN %FTP_LOOP(&var0);

%IF "&var1" NE "" %THEN %LOOP1(&var1);

%IF "&var2" NE "" %THEN %LOOP1(&var2);

%IF "&var3" NE "" %THEN %LOOP1(&var3);

%IF "&var4" NE "" %THEN %LOOP1(&var4);

would be executed *before* your data step. As your var variables have *not* been set when the macro code is executing, the var variables will not resolve, making the %if test compare an unresolved macro variable name with a blank, which evaluates to true, which is why LOOP1 gets executed.

An example might help demonstrate:

%macro test;

  %if "&var1" ne "" %then

    %put var1=*1*&var1*1*;

  %let var1=;

  %if "&var1" ne "" %then

    %put var1=*2*&var1*2*;

  %let var1=value1;

  %if "&var1" ne "" %then

    %put var1=*3*&var1*3*;

%mend test;

%test;

gives:

WARNING: Apparent symbolic reference VAR1 not resolved.

WARNING: Apparent symbolic reference VAR1 not resolved.

var1=*1*&var1*1*

var1=*3*value1*3*

Only the middle %if test condition evaluates to false, as the first test resolves to:

"&var1" ne ""

because &var1 could not be resolved as it has not been set up, and the 3rd test is:

"value1" ne ""

which also evaluates to true.

Hope that helps.

Regards,

Amir.

Super User
Posts: 5,085

Re: Conditional Macros

Add this line as the first statement within %LOOP:

%local var0 var1 var2 var3 var4;

Otherwise, these variables won't exist.  Attempts to resolve them will generate a message, and will generate the original characters (such as '&var1') instead of a resolved value.

Super User
Super User
Posts: 6,502

Re: Conditional Macros

You are probably getting errors because you did not force the existence of the macro variables. So when you try to use the %IF statements it cannot resolve the macro variable reference.  When this happens SAS just uses the original string including the ampersand so you end up with the string "&VAR1" which is not equal to the string "".

A simple way to define the macro variables is to list them in a %LOCAL statement at the top of the macro.

Of course for this application you could just use CALL EXECUTE and eliminate the need for the macro variables or even for the macro %LOOP().

if var = 'x' then call execute(cats('%nrstr(%loop1()',name0,')'));

else ...

Ask a Question
Discussion stats
  • 4 replies
  • 255 views
  • 0 likes
  • 5 in conversation