- 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;
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.
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.
Hadn't heard of PROC STANDARD before - that's sooooo much easier. Thanks!
Just curious: You suggest using standard or stdize but then you use summary in your code...?
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: