BookmarkSubscribeRSS Feed
farwa
Calcite | Level 5

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.

Thanks

3 REPLIES 3
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller
Kurt_Bremser
Super User

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;
PaigeMiller
Diamond | Level 26

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 691 views
  • 2 likes
  • 3 in conversation