I have two different values of body-weight for each individuals in a dataset (Dataset 1) and I would like to create a new dataset (Dataset 2) that will have only the highest of the two values. I was wondering how do I write a SAS code for that. My current dataset is:
Dataset 1
-------------
--------------------------
ID Weight
--- -----------
A101W1 140 lb
A101W2 142 lb
A102W1 145 lb
A102W2 144 lb
A103W1 139lb
A103W2 140lb
A104W1 144lb
A104W2 145lb
----------- ----------
and so on and so on
I would like to create a SAS code for creating a new dataset (Dataset 2) that will contain the highest weight value out of the two from each individual in Dataset 1.
Dataset 2
-------------
ID Weight
---- -------------
A101W2 142 lb
A102W1 145 lb
A103W2 140 lb
A104W2 145 lb
and so on and so on
Any help will be highly appreciated. And thanks in advance.
You're close but you need to format ID properly. Proc summary will do it all but you do need to remove the format from ID to see the Wn part again.
You could try data step 'look ahead' or 'look behind' technique, but for me it is easier to do the following:
data have;
input ID$ Weight;
/* grp+mod(_n_,2)=1;*/
length grp $ 4;
grp=id;
cards;
A101W1 140 lb
A101W2 142 lb
A102W1 145 lb
A102W2 144 lb
A103W1 139 lb
A103W2 140 lb
A104W1 144 lb
A104W2 145 lb
;
proc sql;
create table want as
select * from have group by grp having weight=max(weight);
quit;
Thank you. This is helpful.
OR
proc summary data=have nway;
class id;
var weight;
output out=want(drop=_type_ _freq_) max=;
run;
You're close but you need to format ID properly. Proc summary will do it all but you do need to remove the format from ID to see the Wn part again.
Another way ...
data have;
input ID$ Weight;
uniqid = SUBSTR(ID,1,4);
cards;
A101W1 140 lb
A101W2 142 lb
A102W1 145 lb
A102W2 144 lb
A103W1 139 lb
A103W2 140 lb
A104W1 144 lb
A104W2 145 lb
;;;;
run;
PROC SORT; BY uniqid weight;
DATA have;
SET have;
BY uniqid;
IF LAST.uniqid;
DROP uniqid;
PROC PRINT;
RUN;
Thanks very much.
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.