Programming the statistical procedures from SAS

Group Means using Arrays

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Group Means using Arrays

Hi. I am new to SAS and have just started using arrays. I have been trying to calculate a group mean using arrays but when I try to use a function like group by or by it doesn't seem to work. Here is the code I have so far

DATA work.QUERY_FOR_CONCORDIATPT_CLEANED;

SET work.QUERY_FOR_CONCORDIATPT_CLEANED;

array computedgroupfairnessidxtime[3] computedgroupfairnessidxtime1-computedgroupfairnessidxtime3;

array avggroupfairnessidxtime[3];

do i=1 to 3;

  avggroupfairnessidxtime= mean(computedgroupfairnessidxtime) ;

group by cycle;

end;

run;


So basically I want it to give me the mean of all the fairness indexes in each cycle (1-6). I hope this makes sense and if anybody can help I'd really appreciate it.


Thanks!


-Christina


Accepted Solutions
Solution
‎09-13-2013 12:31 PM
Super User
Posts: 18,563

Re: Group Means using Arrays

Your "GROUP BY" syntax is incorrect, also, arrays work across rows, so your average is calculating the average of each row, which in your case is the identical value.

Your best bet is to post an example of the data you have and what you need.

This will "WORK" but I don't think it's what you want:

DATA work.QUERY_FOR_CONCORDIATPT_CLEANED;

SET work.QUERY_FOR_CONCORDIATPT_CLEANED;

BY CYCLE;

array computedgroupfairnessidxtime[3] computedgroupfairnessidxtime1-computedgroupfairnessidxtime3;

array avggroupfairnessidxtime[3];

do i=1 to 3;

  avggroupfairnessidxtime= mean(computedgroupfairnessidxtime) ;

end;

run;


I think you actually want proc means instead :


proc means data=QUERY_FOR_CONCORDIATPT_CLEANED;

class cycle;

var computedgroupfairnessidxtime1-computedgroupfairnessidxtime3;

output out=QUERY_FOR_CONCORDIATPT_CLEANED mean= /autoname;

run;

View solution in original post


All Replies
Solution
‎09-13-2013 12:31 PM
Super User
Posts: 18,563

Re: Group Means using Arrays

Your "GROUP BY" syntax is incorrect, also, arrays work across rows, so your average is calculating the average of each row, which in your case is the identical value.

Your best bet is to post an example of the data you have and what you need.

This will "WORK" but I don't think it's what you want:

DATA work.QUERY_FOR_CONCORDIATPT_CLEANED;

SET work.QUERY_FOR_CONCORDIATPT_CLEANED;

BY CYCLE;

array computedgroupfairnessidxtime[3] computedgroupfairnessidxtime1-computedgroupfairnessidxtime3;

array avggroupfairnessidxtime[3];

do i=1 to 3;

  avggroupfairnessidxtime= mean(computedgroupfairnessidxtime) ;

end;

run;


I think you actually want proc means instead :


proc means data=QUERY_FOR_CONCORDIATPT_CLEANED;

class cycle;

var computedgroupfairnessidxtime1-computedgroupfairnessidxtime3;

output out=QUERY_FOR_CONCORDIATPT_CLEANED mean= /autoname;

run;

Super Contributor
Posts: 333

Re: Group Means using Arrays

I agree with based on your description you want to average across your observations grouped by the 6 cycles that are indicated by the cycle variable. I think may have misspoken above, arrays and the mean function both work across variables (ie columns). Proc SQL mean function does work across rows and with a group by statement can provide the same results as the proc means code that provided (Might want to change the output to a different table name so you dont over write your input data).

Proc sql;

     create table  AVG_CONCORDIATPT as

     select cycle,

          mean(computedgroupfairnessidxtime1) as avg_computedgroupfairnessidxtime1,

          mean(computedgroupfairnessidxtime2) as avg_computedgroupfairnessidxtime2,

          mean(computedgroupfairnessidxtime3) as avg_computedgroupfairnessidxtime3

     from QUERY_FOR_CONCORDIATPT_CLEANED

     group by cycle

     ;

quit;

Super User
Posts: 18,563

Re: Group Means using Arrays

I was thinking in "English" rather than "Programming" Smiley Happy

Across the rows meant horizontal across, versus down rows, vertically "across".

Anyways, You're also using EG, so you can also use Tasks>Describe>Summary Statistics to get what you need. Make sure to click the Save Stats to dataset option. You can also edit the statistics calculated to get only the mean. 

Occasional Contributor
Posts: 5

Re: Group Means using Arrays

Yes. Proc Means was what I was looking for. That did the trick perfectly. Thanks.

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 343 views
  • 2 likes
  • 3 in conversation