DATA Step, Macro, Functions and more

proc means with by variables

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

proc means with by variables

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.


Accepted Solutions
Solution
‎01-05-2017 06:40 PM
Contributor
Posts: 67

Re: proc means with by variables

[ Edited ]
Posted in reply to Astounding

Thank you so much!! Astounding!!

This is what exactly i want.

 

Happy New year!!

View solution in original post


All Replies
Trusted Advisor
Posts: 1,137

Re: proc means with by variables

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
Contributor
Posts: 67

Re: proc means with by variables

Posted in reply to Jagadishkatam

Thank you very much for your prompt reply.

It works!

Trusted Advisor
Posts: 1,137

Re: proc means with by variables

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
Super User
Posts: 5,516

Re: proc means with by variables

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;

Solution
‎01-05-2017 06:40 PM
Contributor
Posts: 67

Re: proc means with by variables

[ Edited ]
Posted in reply to Astounding

Thank you so much!! Astounding!!

This is what exactly i want.

 

Happy New year!!

New Contributor
Posts: 3

Re: proc means with by variables


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

 

Hope this help

Contributor
Posts: 67

Re: proc means with by variables

Thank you, pritam

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

 

I love it.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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