BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Olime
Fluorite | Level 6

Does anyone please give advice to me how come the result is not A? 

Thank you very much! 

 

Here the sample code below:


%global result;


%macro addd(re);

   data _null_;
      if &re = "A" then do;
             call symputx('result','A');
    end;
    else do;
            call symputx('result','B');
    end;
  run;
%mend;

 

data readds;
  input posit $;
datalines;
 A
 ;
 run;

 

data _null_;
set readds;
  rc = dosubl('%addd('||posit||');');
  collectresult=symget('result');
  put collectresult=;
run;

 

 

 

RESULT:

collectresult=B

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The problem is with your macro. So let's ignore the complications of the extra data step and DOSUBL call and just call the macro directly to see what is happening. Since your macro is just generating SAS code we an turn on the MPRINT option and see what it does.  So here is program that can test that (I simplified the macro so that is not so many lines of code).

%global result;
%macro addd(re);
data _null_;
  if &re = "A" then call symputx('result','A');
  else call symputx('result','B');
run;
%mend addd;

options mprint;
%addd(A)
%put &=result;

And here is the results from the LOG.

53   %addd(A)
MPRINT(ADDD):   data _null_;
MPRINT(ADDD):   if A = "A" then call symputx('result','A');
MPRINT(ADDD):   else call symputx('result','B');
MPRINT(ADDD):   run;

NOTE: Variable A is uninitialized.
NOTE: DATA statement used (Total process time):
      real time           0.08 seconds
      cpu time            0.00 seconds


54   %put &=result;
RESULT=B

As you can see SAS could not find a variable named A to compare to the string literal 'A' so the comparison was false. It will be false unless you call it with a string literal as the value of the parameter :  %ADDD(re='A') or %ADDD(re="A");

 

If you really wanted a line of data step code that would compare the value of the macro variable RE to the character A then you would want something like this. 

  if "&re" = "A" then call symputx('result','A'); 

 

View solution in original post

3 REPLIES 3
Astounding
PROC Star

When &RE is A, this line gets generated:

 

if A = "A" then do;

 

That comparison is never true, so the ELSE condition always kicks in.

Cynthia_sas
SAS Super FREQ
Hi:
I'm not sure what you are trying to do, but I am fairly sure that this is wrong:
if &re = "A" then do;

What notes do you see in the log? I would expect you to see:
NOTE: Variable A is uninitialized.

Because your data step IF statement is asking whether the macro variable value &re is equal to the quoted string "A". As far as I can tell, you do NOT have a data step variable called A -- you have a macro variable whose value is A and so this statement:
if &re = "A" then do;
resolves to
if A = "A" then do;

which has to be false, since variable A does not exist.

If you are trying to test the value of a macro variable, then you should use a %IF.

However, since it's still not clear to me what you want to do, I don't have any other suggestions except that you rethink your logic and investigate the correct way to refer to macro variables and the difference between DATA step IF and Macro %IF.

cynthia
Tom
Super User Tom
Super User

The problem is with your macro. So let's ignore the complications of the extra data step and DOSUBL call and just call the macro directly to see what is happening. Since your macro is just generating SAS code we an turn on the MPRINT option and see what it does.  So here is program that can test that (I simplified the macro so that is not so many lines of code).

%global result;
%macro addd(re);
data _null_;
  if &re = "A" then call symputx('result','A');
  else call symputx('result','B');
run;
%mend addd;

options mprint;
%addd(A)
%put &=result;

And here is the results from the LOG.

53   %addd(A)
MPRINT(ADDD):   data _null_;
MPRINT(ADDD):   if A = "A" then call symputx('result','A');
MPRINT(ADDD):   else call symputx('result','B');
MPRINT(ADDD):   run;

NOTE: Variable A is uninitialized.
NOTE: DATA statement used (Total process time):
      real time           0.08 seconds
      cpu time            0.00 seconds


54   %put &=result;
RESULT=B

As you can see SAS could not find a variable named A to compare to the string literal 'A' so the comparison was false. It will be false unless you call it with a string literal as the value of the parameter :  %ADDD(re='A') or %ADDD(re="A");

 

If you really wanted a line of data step code that would compare the value of the macro variable RE to the character A then you would want something like this. 

  if "&re" = "A" then call symputx('result','A'); 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 782 views
  • 4 likes
  • 4 in conversation