tmtran99
tmtran99

Reputation: 37

Refer to a macro variable

Let's say I have a table with 10 variables: var1, var2... var10 and I want to create other 9 variables : coef1, coef2.... coef9 from the 10 initial variable like:

coef1 = var2 gt .,
coef2 = var3 gt .,
...
coef9 = var10 gt .,

I try the code like:

%macro mymacro;
%do i = 1 %to 9;
 data mydata;
  set mydata;
  coef&i = var&i+1 gt .,
 run;
%end;
%mend;

%mymacro;

But it doesn't work, I guess the problem is that SAS could not resolve the var&i+1. What am I supposed to solve it?

Upvotes: 0

Views: 134

Answers (2)

Llex
Llex

Reputation: 1770

You should use macro %do inside data step and %eval to calculate macro value:

%macro mymacro;

 data mydata;
  set mydata;
  %do i = 1 %to 9;
     coef&i = var%eval(&i+1) gt .;
  %end;
 run;

%mend;

%mymacro;

Upvotes: 1

Reeza
Reeza

Reputation: 21264

Use an array instead of macro for looping over variables. Or alternatively restructure your data to a long format and use that formatting.

data want;
set have;

array coef(*) coef:;
array _var(*) var2-var100;

do i=1 to 99;
  coef(i) = _var(i+1) gt .;
end;

run;

Upvotes: 3

Related Questions