BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Flexluthorella
Obsidian | Level 7
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.

1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10

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

-unison

View solution in original post

22 REPLIES 22
unison
Lapis Lazuli | Level 10

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

-unison
Flexluthorella
Obsidian | Level 7
How will it changes the results? I need the absolute min per patient ID
Flexluthorella
Obsidian | Level 7
when I ran the var in the class statement, there were missing data spots. What do you mean by adding proc means results to original data set? How would I do that
PaigeMiller
Diamond | Level 26

@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)=;
--
Paige Miller
Flexluthorella
Obsidian | Level 7
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.
Tom
Super User Tom
Super User

@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.

novinosrin
Tourmaline | Level 20

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.

Flexluthorella
Obsidian | Level 7
Male and death need to be in the final table but not analyzed. I am creating two datasets with minwt and maxwt then merging these two together. My final output should have the delta weight where delta is gt 10 lbs and the tables will have only the proportion of deceased (death=1 only) or males vs females. final table is most likely proc freq with cum percentage.
novinosrin
Tourmaline | Level 20

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

Flexluthorella
Obsidian | Level 7

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.

novinosrin
Tourmaline | Level 20

How about?

 

proc sql;
select patientid, male, deceased ,min(weight) as min_wt
from your_input
group by patientid
having weight= min_wt;
quit;
Flexluthorella
Obsidian | Level 7
Can I do the same for maxweight? Can I do this in one proc sql?
novinosrin
Tourmaline | Level 20

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;

 

Flexluthorella
Obsidian | Level 7
There is an error with this one as well. The query does not give me the output I need

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 22 replies
  • 832 views
  • 1 like
  • 6 in conversation