SQL is a good idea,
data have;
input PATIENT Res $;
datalines;
101 78
101 72
102 80
102 90
;
proc sql;
create table want as
select *, input(res,best.) as New_res
from have
union all
select PATIENT,' ' as Res,mean( input(res,best.)) as New_res
from have
group by PATIENT
order by patient,res desc;
quit;
So New_res should always be the same as Res? And is New_res allowed to be numeric?
This doesn't make much sense to me. However, if the Res variables should stau character (even though you request it to hold a numeric mean value), then do something like this
data have;
input PATIENT Res $;
datalines;
101 78
101 72
102 80
102 90
;
data want(drop=s);
do _N_=1 by 1 until (last.PATIENT);
set have;
by PATIENT;
New_Res=input(Res, 8.);
s+Res;
output;
end;
New_Res=.;
Res=put(s/_N_, 8. -l);
output;
run;
Result:
PATIENT Res NewREs 101 78 78 101 72 72 101 75 . 102 80 80 102 90 90 102 160 .
SQL is a good idea,
data have;
input PATIENT Res $;
datalines;
101 78
101 72
102 80
102 90
;
proc sql;
create table want as
select *, input(res,best.) as New_res
from have
union all
select PATIENT,' ' as Res,mean( input(res,best.)) as New_res
from have
group by PATIENT
order by patient,res desc;
quit;
Hi @sasuser123123 Honestly, it was your idea thanks to your thought process. My thought process didn't think of SQL. All I did was merely wrote the syntax which anybody can 🙂 Have a good one!
Hi @sasuser123123 Do you mean this?
data have;
input (Patient TP Test) ($) Results;
cards;
101 BS1 Sysbp 90
101 BS1 Diabp 80
101 BS1 RR 18
101 BS2 Sysbp 100
101 BS2 Diabp 60
101 BS2 RR 18
102 BS1 Sysbp 100
102 BS1 Diabp 60
102 BS1 RR 18
102 BS2 Sysbp 109
102 BS2 Diabp 50
102 BS2 RR 18
;
proc sql;
create table want as
select patient,test,mean(Results) as Avg
from have
group by patient,test;
quit;
I saw your thread. If it's an ODS related question, I recommend asking Cynthia SAS, Reeza etc. They are experts and can guide you appropriately.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.