DATA Step, Macro, Functions and more

I cannot figure out this code is not working in proper way... please help

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

I cannot figure out this code is not working in proper way... please help

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


Accepted Solutions
Solution
‎02-04-2017 10:37 AM
Super User
Super User
Posts: 7,074

Re: I cannot figure out this code is not working in proper way... please help

[ Edited ]

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


All Replies
Super User
Posts: 5,516

Re: I cannot figure out this code is not working in proper way... please help

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.

SAS Super FREQ
Posts: 8,868

Re: I cannot figure out this code is not working in proper way... please help

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
Solution
‎02-04-2017 10:37 AM
Super User
Super User
Posts: 7,074

Re: I cannot figure out this code is not working in proper way... please help

[ Edited ]

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'); 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 176 views
  • 4 likes
  • 4 in conversation