DATA Step, Macro, Functions and more

macro runs in one data step, not in another

Reply
N/A
Posts: 1

macro runs in one data step, not in another

Hi - first post on the forum, relatively new SAS user.

I wrote a macro to generate a bunch of variable declarations:

%macro gen(varlist);
%local k;
%local curvar;
%do k = 1 %to 120;
%let curvar = %scan(&varlist, &k);
&curvar.12 = &curvar.03 - &curvar.02;
&curvar.13 = &curvar.04 - &curvar.02;
&curvar.14 = &curvar.05 - &curvar.02;
&curvar.34 = &curvar.05 - &curvar.04;
%end;
%mend gen;

where the varlist input was this long (120 variables) list of variables in a random (but important) order.

At first I tried to call this macro in a data step after the INPUT section with all the variable names, because in the original file all of these declarations were just typed out, one by one, and this is where they were. It kept giving me an "apparent symbolic reference not resolved..." error. Then I made a second data step where I effectively just set the new dataset to the old dataset, and I was able to call the macro fine.

Are there rules about calling macros immediately after the INPUT part of a data step? Why would this work in the second data step but not the first?
Super Contributor
Super Contributor
Posts: 3,174

Re: macro runs in one data step, not in another

No restrictions, however you will be best served by providing an expanded SASLOG pasted in a forum post reply, so others can help guide you with the specific error condition and message text. Be sure to add:

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN;

Also, the above diagnostic command added to your SAS program may help with self-debugging.

One hint is that SAS data variables must start with an alpha character, so until we can see what macro invocation parameters you are supplying when invoking %gen, it's difficult to identify the problem cause. Suggest you first add the OPTIONS statement, rerun your SAS problem and review the SASLOG - then reply with more information if unable to determine the cause.

Scott Barry
SBBWorks, Inc.
Super Contributor
Super Contributor
Posts: 3,174

Re: macro runs in one data step, not in another

Your hardcode %DO operand 120 will have a negative impact - might I suggest the change below, also I took the opportunity to use your macro invocation in an example code snippet:

81 %macro gen(varlist);
82 %local k;
83 %local curvar;
84 %do k = 1 %to %sysfunc(countw(&varlist));
85 %let curvar = %scan(&varlist, &k);
86 &curvar.12 = &curvar.03 - &curvar.02;
87 &curvar.13 = &curvar.04 - &curvar.02;
88 &curvar.14 = &curvar.05 - &curvar.02;
89 &curvar.34 = &curvar.05 - &curvar.04;
90 %end;
91 %mend gen;
92 options source source2 macrogen symbolgen;
93
94 data;
95 input a02 a03 a04 a05;
96 %gen(a);
SYMBOLGEN: Macro variable VARLIST resolves to a
SYMBOLGEN: Macro variable VARLIST resolves to a
SYMBOLGEN: Macro variable K resolves to 1
SYMBOLGEN: Macro variable CURVAR resolves to a
SYMBOLGEN: Macro variable CURVAR resolves to a
SYMBOLGEN: Macro variable CURVAR resolves to a
MACROGEN(GEN): a12 = a03 - a02;
SYMBOLGEN: Macro variable CURVAR resolves to a
SYMBOLGEN: Macro variable CURVAR resolves to a
SYMBOLGEN: Macro variable CURVAR resolves to a
MACROGEN(GEN): a13 = a04 - a02;
SYMBOLGEN: Macro variable CURVAR resolves to a
SYMBOLGEN: Macro variable CURVAR resolves to a
SYMBOLGEN: Macro variable CURVAR resolves to a
MACROGEN(GEN): a14 = a05 - a02;
SYMBOLGEN: Macro variable CURVAR resolves to a
SYMBOLGEN: Macro variable CURVAR resolves to a
SYMBOLGEN: Macro variable CURVAR resolves to a
MACROGEN(GEN): a34 = a05 - a04;
97 putlog _all_;
98 datalines;

a02=1 a03=1 a04=1 a05=1 a12=0 a13=0 a14=0 a34=0 _ERROR_=0 _N_=1
NOTE: The data set WORK.DATA5 has 1 observations and 8 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


100 run;

Hope this info helps.


Scott Barry
SBBWorks, Inc.
PROC Star
Posts: 1,566

Re: macro runs in one data step, not in another

In case you declare/create/populate a macro variable in a data step, you cannot use it in the same data step, i.e.
data t;
a='1';
call symput('a',a);
put "&a";
run;
will not work.
Unsure this is an issue here, but it might be from your explanations.
Super Contributor
Posts: 474

Re: macro runs in one data step, not in another

Maybe you should provide us the full code (both cases) you are running, because beside the hardcoded %do (as Scott pointed), don't see anything wrong with your piece of code. The issue should be in the you are using it.

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.
Ask a Question
Discussion stats
  • 4 replies
  • 134 views
  • 0 likes
  • 4 in conversation