简体   繁体   中英

Mean procedure with SAS

I want to find the mean of following datalines; the way I am trying, I am getting the mean based on no. of observation which in this case is 6. But I want it based on Day so it comes something like Mean = Timeread/(no. of day) which is 3

name    Day    Timeread
X        1        12
X        1        23
X        1        12
X        2        8
X        2        5
X        3        3 

This is the code I used

proc summary data = xyz nway missing;
  class Name;
  var timeread;
  output out = Average mean=;
run;
proc print data = Average;
run;

I'm not sure how to do this with proc mean but you can do this in SQL like so:

proc sql noprint;
  create table want as
  select name, 
         sum(timeread) / count(distinct day) as daily_mean
  from have
  group by name
  ;
quit;

This uses the HAVE dataset from @CarolinaJay65's answer.

If you are just wanting the mean of total timeread by total distinct days

Data HAVE;
Input name $   Day  Timeread ;
Datalines;
X        1        12
X        1        23
X        1        12
X        2        8
X        2        5
X        3        3 
;
Run;

Proc Sql;
    Create table WANT as
    Select Name, (select count(distinct(Day)) from HAVE) as DAYS
     , sum(timeread) as TIMEREAD_TOTAL
     , calculated timeread_total/calculated days as MEAN
    From HAVE
    Group by Name;
Quit;

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