data Hivdata;
merge learn.demogdemo (keep=patient_id male death arvstart) learn.visitdemo (keep=patient_id weight apptdate);
by patient_id;
run;
*identify min weight prior to ARTstart by seperating records where appt date is less than or equal to arvstart to find min weight by id;
proc means data=work.Hivdata min noprint;
class patient_id;
var weight male death;
where apptdate le arvstart;
output out=minweight
min=minWt;
run;
This code is outputing the new var minWt but the other variables like male and death are not being keep. I also need to do this with max weight and compare difference.
Split into 2 summaries (before and after arvstart)
Merge and compare min_wt and max_wt.
proc means nway missing noprint data=have(where=(apptdate le arvstart));
class patient_id male death;
output out=before(drop=_type_ _freq_)
min(weight)=min_wt;
run;
proc means nway missing noprint data=have(where=(apptdate gt arvstart));
class patient_id male death;
output out=after(drop=_type_ _freq_)
max(weight)=max_wt;
run;
data want;
merge before(in=a) after(in=b);
by patient_id male death;
if a and b;
if max_wt - min_wt gt 10;
run;
-unison
If you want your other variables you need to add them into the class statement - be careful though as this can change the result of the minimum just based on patientid. What you can do is merge the result of proc means back to your original set.
-unison
@Flexluthorella wrote:
data Hivdata; merge learn.demogdemo (keep=patient_id male death arvstart) learn.visitdemo (keep=patient_id weight apptdate); by patient_id; run; *identify min weight prior to ARTstart by seperating records where appt date is less than or equal to arvstart to find min weight by id; proc means data=work.Hivdata min noprint; class patient_id; var weight male death; where apptdate le arvstart; output out=minweight min=minWt; run;
This code is outputing the new var minWt but the other variables like male and death are not being keep. I also need to do this with max weight and compare difference.
If I am understanding the question properly (which I may not be, can you clarify or explain in more detail what you want) you need ask for statistics for variables MALE and DEATH in the OUTPUT statement as well.
Example:
output out=minweight min(weight)=minweight mean(male death)=;
@Flexluthorella wrote:
I need the min weight per patient_id. the var male and death are numeric. male is gender and values include 1 for male 0 for female. Death values include 1 for deceased 0 for not deceased. I do not need the min or any stats for these.
So it sounds like those will be constant values across all observations for a particular person.
You can either include them in the CLASS statement instead of the VAR statement. (make sure to add NWAY option to the PROC statement).
Or if you include them in the VAR statement then calculate a stat on them. Like MIN or MAX.
Hi @Flexluthorella Good morning, A sample of your input dataset and the expected output for the input will help determine what's the best approach i would think. Your description gives me leaning towards proc sql unless your variable male and death are actually analysis variables.
ok @Flexluthorella Thanks. May I/we(on behalf of othersw too) request you to post the values of what you have?
Is it something like the below?
Patientid Gender Death Weight
A01 M 0 40
A02 M 1 60
A03 F 0 65
A04 F 1 55
See attached for initial data set. there are several wt values for each patient_id. I need the min and the max for each pat ID. The original file was too large to upload.
How about?
proc sql;
select patientid, male, deceased ,min(weight) as min_wt
from your_input
group by patientid
having weight= min_wt;
quit;
yes you can if you want something like the following this to get two records i.e 1 for min 2 for max in the having filter
proc sql;
select patientid, male, deceased ,min(weight) as min_wt,max(weight) as max_wt
from your_input
group by patientid
having weight= min_wt or weight=max_wt;
quit;
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!
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.
Ready to level-up your skills? Choose your own adventure.