Hi, SAS-Lers,
I have a macro that uses a %if statement to check the value of a macro variable against a *data step* variable. The problem is that the comparison seems to check the value of the macro variable (which resolves correctly) to the *name* of the data set variable (qnum in this case). Any ideas here ? I tried several approaches using call symput, call execute, %eval, - all without success.
Here is version 1 of the program:
option symbolgen mprint mlogic;
data one;
input qnum;
datalines;
1
2
3
;
proc print data = one;
title 'Test data set';
run;
%macro makevar;
%local x;
data new;
set one;
%do x = 1 %to 3;
%if &x=qunum %then %do; /* Should be TRUE for first obs of data set */
%put IF condition is true for &i ;
%end;
%end;
run;
%mend makevar;
%makevar
Part of the log for this program :
28 %makevar
MLOGIC(MAKEVAR): Beginning execution.
MLOGIC(MAKEVAR): %LOCAL X
MPRINT(MAKEVAR): data new;
MPRINT(MAKEVAR): set one;
MLOGIC(MAKEVAR): %DO loop beginning; index variable X; start value is 1; stop value is 3; by value is 1.
SYMBOLGEN: Macro variable X resolves to 1
MLOGIC(MAKEVAR): %IF condition &x=qunum is FALSE <-- HERE IS THE PROBLEM
MLOGIC(MAKEVAR): %DO loop index variable X is now 2; loop will iterate again.
SYMBOLGEN: Macro variable X resolves to 2
MLOGIC(MAKEVAR): %IF condition &x=qunum is FALSE
MLOGIC(MAKEVAR): %DO loop index variable X is now 3; loop will iterate again.
SYMBOLGEN: Macro variable X resolves to 3
MLOGIC(MAKEVAR): %IF condition &x=qunum is FALSE
MLOGIC(MAKEVAR): %DO loop index variable X is now 4; loop will not iterate again.
MPRINT(MAKEVAR): run;
So, it appears the comparison %if is making is the resolved value of &x (which is 1) to the name 'qnum'; hence, it is never true - and it should be on the first observation of the data set.
Second try using call symput and second macro variables &y:
%macro makevar;
%local x y;
data new;
set one;
%do x = 1 %to 3;
call symputx('y',qnum);
%if &x=&y %then %do; /* Should be TRUE for first obs of data set */
%put IF condition is true for &x ;
%end;
%end;
run;
%mend makevar;
%makevar
And here's the log from this program:
29 %makevar
MLOGIC(MAKEVAR): Beginning execution.
MLOGIC(MAKEVAR): %LOCAL X Y
MPRINT(MAKEVAR): data new;
MPRINT(MAKEVAR): set one;
MLOGIC(MAKEVAR): %DO loop beginning; index variable X; start value is 1; stop value is 3; by value is 1.
MPRINT(MAKEVAR): call symputx('y',qnum);
SYMBOLGEN: Macro variable X resolves to 1
SYMBOLGEN: Macro variable Y resolves to <-- HERE IS THE PROBLEM
MLOGIC(MAKEVAR): %IF condition &x=&y is FALSE
MLOGIC(MAKEVAR): %DO loop index variable X is now 2; loop will iterate again.
MPRINT(MAKEVAR): call symputx('y',qnum);
SYMBOLGEN: Macro variable X resolves to 2
SYMBOLGEN: Macro variable Y resolves to
MLOGIC(MAKEVAR): %IF condition &x=&y is FALSE
MLOGIC(MAKEVAR): %DO loop index variable X is now 3; loop will iterate again.
MPRINT(MAKEVAR): call symputx('y',qnum);
SYMBOLGEN: Macro variable X resolves to 3
SYMBOLGEN: Macro variable Y resolves to
MLOGIC(MAKEVAR): %IF condition &x=&y is FALSE
MLOGIC(MAKEVAR): %DO loop index variable X is now 4; loop will not iterate again.
MPRINT(MAKEVAR): run;
Do any of you macro experts have any idea why this is not working ? I have a feeling it involves the timing of the macro compilation vs. data set compilation.
Barry Walton
[email protected]