DATA Step, Macro, Functions and more

Arrays Using Macro Variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

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
Respected Advisor
Posts: 3,069

Re: Arrays Using Macro Variables

[ Edited ]
Posted in reply to SASaholic629

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


All Replies
Solution
a month ago
Respected Advisor
Posts: 3,069

Re: Arrays Using Macro Variables

[ Edited ]
Posted in reply to SASaholic629

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

Posted in reply to PaigeMiller

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

Regular Contributor
Posts: 217

Re: Arrays Using Macro Variables

Posted in reply to PaigeMiller

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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