BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasuser123123
Quartz | Level 8
Hello!!
I want to add a new record to the existing dataset which shows average of two records of the data,the data is


PATIENT Res
101 78
101 72
102 80
102 90

So I need the out put like

Patient Res New_res
101 78 78
101 72 72
101 75
102 80 80
102 90 90
102 85

Could you please help me out.
And how to do.
Note:the res variable is character (how to do with out changing it to the numeric).

Thank you!
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @sasuser123123 

 

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;

View solution in original post

14 REPLIES 14
PeterClemmensen
Tourmaline | Level 20

So New_res should always be the same as Res? And is New_res allowed to be numeric?

sasuser123123
Quartz | Level 8
Yes.
PeterClemmensen
Tourmaline | Level 20

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   .

 

sasuser123123
Quartz | Level 8
Thank you so much for your quick response

I'm extremely sorryyyyy..
The 75 and 85 values in my results are under New_res not under res.
And also I didn't get your progra. I'm trying it by using proc SQL but it shows an error 'The mean summary function requires a numeric argument'. Can we do it by using by proc SQL
novinosrin
Tourmaline | Level 20

Hi @sasuser123123 

 

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;
sasuser123123
Quartz | Level 8
Yeah it's perfectly working
Thank you so much for your assistance..
novinosrin
Tourmaline | Level 20

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!

sasuser123123
Quartz | Level 8
Thank you!
sasuser123123
Quartz | Level 8
Hello sir!
I've data
Patient TP Test Results
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
Now I'm trying to create one table which displays avg of results by TP and TEST per subject it means patient 101 has two records of SYSBP so I need avg of SYSBP...
I'm tried by using proc SQL but I didn't get output
Could you please solve this one

Thank you!


novinosrin
Tourmaline | Level 20

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;
sasuser123123
Quartz | Level 8
Thank you!
sasuser123123
Quartz | Level 8
Hello sir!

I've a problem while opening saved rtf file
I need your help..
novinosrin
Tourmaline | Level 20

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.

sasuser123123
Quartz | Level 8
Yeah sure!!
Than you

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 14 replies
  • 1566 views
  • 2 likes
  • 3 in conversation