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
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;
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;
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;
I was thinking in "English" rather than "Programming"
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.
Yes. Proc Means was what I was looking for. That did the trick perfectly. Thanks.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.