BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ursula
Pyrite | Level 9

Hi there,

I wonder how to code the proc means to calculate the avarage of the values by id and visit.

example:

id visit value avarage
a 1 10 20
a 1 20
a 1 30
a 2 50 40
a 2 30
b 1 10 15
b 1 20

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
ursula
Pyrite | Level 9

Thank you so much!! Astounding!!

This is what exactly i want.

 

Happy New year!!

View solution in original post

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16

Try proc sql

 

data have;
input id $	visit 	value ;
cards;
a 	1 	10 
a 	1 	20
a 	1 	30
a 	2 	50 
a 	2 	30
b 	1 	10 	
b 	1 	20
;

proc sql;
create table test as select *, mean(value) as average from have group by id ,visit;
quit;
Thanks,
Jag
ursula
Pyrite | Level 9

Thank you very much for your prompt reply.

It works!

Jagadishkatam
Amethyst | Level 16

we could also get the same via data step

 

data want;
do until(last.visit);
set have;
by id visit;
if first.visit then do;
s=value;
c=1;
end;
else do;
s+value;
c+1;
end;
average=s/c;
end;
do until(last.visit);
set have;
by id visit;
output;
end;
drop s c;
run;
Thanks,
Jag
Astounding
PROC Star

Since you originally asked about PROC MEANS, here is what it would look like:

 

proc means data=have nway noprint;

var value;

class id visit;

output out=want (drop=_type_ _freq_) mean=average;

run;

ursula
Pyrite | Level 9

Thank you so much!! Astounding!!

This is what exactly i want.

 

Happy New year!!

pritam26
Fluorite | Level 6


proc means data=have nway noprint ;
var value;
output out=test(drop=_TYPE_ _FREQ_)
mean= average;
class id visit ;
run;

 

Hope this help

ursula
Pyrite | Level 9

Thank you, pritam

the solution is exactly the same as Astounding's post.

 

I love it.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1091 views
  • 6 likes
  • 4 in conversation