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
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');
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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.