BookmarkSubscribeRSS Feed
JasonNC
Quartz | Level 8

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?

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

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

Thanks,

Jagadish

Thanks,
Jag
Amir
PROC Star

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.

Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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 ...

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 964 views
  • 0 likes
  • 5 in conversation