简体   繁体   中英

SAS: Calculate an average excluding the current observation

I am searching for an elegant way (or, failing that, an inelegant way) to calculate an average which does not include the current record. So, if I have 30 observations I would end up with 30 different averages. Each would be the average of the other 29 values.

From this made-up data, I would want to create 5 new observations with the averages of A, B, and C not including their own data.

        A   B   C
Albert  12  4   6
Bob     14  7   12
Clyde   6   7   11
Dennis  9   11  7
Earl    8   8   6

I have a vague idea that this will involve proc sql inside a loop. Other ideas or approaches are appreciated.

No loop needed. Use SQL to get the totals for each variable. The average without the current observation is (total sum - value)/(n-1)

data test;
input NAME $ A   B   C;
datalines;
Albert  12  4   6
Bob     14  7   12
Clyde   6   7   11
Dennis  9   11  7
Earl    8   8   6
;
run;

proc sql noprint;
select count(*),
       sum(A),
       sum(B),
       sum(C)
    into :n,
         :a,
         :b,
         :c
    from test;
quit;

data test2;
set test;
Ave_A = (&a - a)/(&n-1);
Ave_B = (&b - b)/(&n-1);
Ave_C = (&c - c)/(&n-1);
run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM