Hi, I wanted to ask that I am trying to take mean of different observations done on same date. I have data for 6 months and there are many observations of every date. Now I want to take mean of every date so that data is minimized. If anyone can tell me the code.
proc summary data=have nway;
class date; /* Your date variable name goes here */
var x; /* The names of the variables from which you want to compute means */
output out=want mean=;
run;
Just for completeness (using MEANS/SUMMARY is recommended):
DATA step:
proc sort data=have;
by date;
run;
data want;
set have (rename=(x=_x));
by date;
if first.date
then do;
x = _x;
count = 1;
end;
else do;
x + _x;
count + 1;
end;
if last.date;
x = x / count;
drop _x count;
run;
(the two SUM statements in the ELSE cause an implied RETAIN of the variables)
SQL:
proc sql;
create table want as
select
date,
mean(x) as x
from have
group by date
;
quit;
I agree that PROC SUMMARY/PROC MEANS is the preferred approach, for many reasons. One of those reasons: if you have multiple variables for which means are needed, it is also much less typing than the other approaches.
The data step code, as provided, gives the wrong answer if there are missing values in the data.
-- Paige Miller
Special offer for SAS Communities members
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.