I am trying to calculate min and max in multiple instances for my data.
Here is an example of my data set.
person_id event id var2 var3 var4 var5 var6
1 1 2 3 1 3 5
1 2 6 1 2 4 1
2 3 3 1 4 2 5
So there are people with multiple rows in my data however each event_id will be unique.
I am trying to calculate an overall min and max for each person and an individual min and max for each event.
Here is what I am looking for:
person_id event id var2 var3 var4 var5 var6 maxALL minALL maxONE minONE
1 1 2 3 1 3 5 6 1 5 1
1 2 6 3 2 4 3 6 1 6 2
2 3 3 2 4 2 5 5 2 5 2
Thank you in advance!
UNTESTED CODE
data two;
set one;
maxone=max(of var2-var6);
minone=min(of var2-var6);
run;
proc summary data=two nway;
class person_id;
var maxone minone;
output out=minmax min=minALL max=maxALL;
run;
data final
merge two minmax;
by personid;
run;
He is a Double DoW approach
data have;
input person_id event_id var2 var3 var4 var5 var6;
datalines;
1 1 2 3 1 3 5
1 2 6 1 2 4 1
2 3 3 1 4 2 5
;
data want;
do until (last.person_id);
set have;
array _{5} var2-var6;
by person_id;
maxALL=max(maxALL, max(of _[*]));
minALL=min(minALL, min(of _[*]));
end;
do until (last.person_id);
set have;
by person_id;
maxONE=max(of _[*]);
minONE=min(of _[*]);
output;
end;
run;
Provided that these variables are next to each other, you can use -- instead of - like this
data have;
input person_id event_id Column6M Column1Y Column100D;
datalines;
1 1 2 3 1
1 2 6 1 2
2 3 3 1 4
;
data want;
do until (last.person_id);
set have;
array _{*} Column6M--Column100D;
by person_id;
maxALL=max(maxALL, max(of _[*]));
minALL=min(minALL, min(of _[*]));
end;
do until (last.person_id);
set have;
by person_id;
maxONE=max(of _[*]);
minONE=min(of _[*]);
output;
end;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.