BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASaholic629
Fluorite | Level 6

- 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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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
SASaholic629
Fluorite | Level 6

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

tomrvincent
Rhodochrosite | Level 12

Just curious: You suggest using standard or stdize but then you use summary in your code...?

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 913 views
  • 0 likes
  • 3 in conversation