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
SAS Innovate 2025: Register Now
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9. Sign up by Dec. 31 to get the 2024 rate of just $495. Register now!