Arrays Using Macro Variables

Solved
Occasional Contributor
Posts: 12

Arrays Using Macro Variables

[ Edited ]

- I have a data set with 50 variables R1-R50

- I have stored the average of each variable in macro variables called AvgR1-AvgR50

- I want to subtract the average from each variable (Z1 = R1 - &AvgR1.)

- How do I create these 50 new variables Z1-Z50?

This is the method I was going for, but I might have the completely wrong idea here:

``````DATA Want (DROP=R: Avg: i);
ARRAY Z{50};
ARRAY R{50};
ARRAY AvgR{50};
SET Have;
DO i = 1 to 50;
Z{i} = R{i} - %eval("&AvgR{i}.");
END;
RUN;``````

Accepted Solutions
Solution
a month ago
Posts: 3,069

Re: Arrays Using Macro Variables

[ Edited ]

No macro needed

PROC STANDARD or PROC STDIZE will do this

Or in a data step

``````proc summary data=have;
var r1-r50;
output out=means mean=avgr1-avgr50;
run;
data want;
array z{50};
array r{50};
array AvgR{50};
if _n_=1 then set means;
set have;
do i = 1 to 50;
z{i} = r{i} - avgr{i};
end;
drop i;
run;``````

But really ... use PROC STANDARD or PROC STDIZE ... don't write your own code to do a calculation that SAS has already programmed.

--
Paige Miller

All Replies
Solution
a month ago
Posts: 3,069

Re: Arrays Using Macro Variables

[ Edited ]

No macro needed

PROC STANDARD or PROC STDIZE will do this

Or in a data step

``````proc summary data=have;
var r1-r50;
output out=means mean=avgr1-avgr50;
run;
data want;
array z{50};
array r{50};
array AvgR{50};
if _n_=1 then set means;
set have;
do i = 1 to 50;
z{i} = r{i} - avgr{i};
end;
drop i;
run;``````

But really ... use PROC STANDARD or PROC STDIZE ... don't write your own code to do a calculation that SAS has already programmed.

--
Paige Miller
Occasional Contributor
Posts: 12

Re: Arrays Using Macro Variables

Hadn't heard of PROC STANDARD before - that's sooooo much easier. Thanks!

Regular Contributor
Posts: 217