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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
Reeza
Super User

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;

esjackso
Quartz | Level 8

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;

Reeza
Super User

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. 

cyanezIBR
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

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