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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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