BookmarkSubscribeRSS Feed
bignate1030
Fluorite | Level 6

I am trying to calculate Min and Max in multiple capacities with a data set.

Here is an example of the data I am working with:

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 who have multiple rows within the data set but each observation will have a unique event id.

For var2 - var6 I am trying to calculate an overall max and min for each person_id and an individualized max and min for each event_id.

Here is my desired output:

 

person_id    event id   var2      var3     var4     var5    var6   maxPERSON minPERSON maxEVENT minEVENT

1                  1              2          3          1         3         5         6                     1                    5                  1  

1                  2              6          2          2         4         3         6                     1                    6                  2

2                  3              3          3          5         2         4         5                     2                    5                  2

1 REPLY 1
Kc2
Quartz | Level 8 Kc2
Quartz | Level 8

Hi,

 

the values under var2 through var6 in the datalines and in the output do not match.

In the datalines suject1 var3=1 but in the output var3=2. Assuming the datalines are correct try this code:

 

 

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

run;

 

Proc sort data= have; by person_id event_id ; run;

proc transpose data=have out=tr_have;
by person_id event_id ;
var var2 var3 var4 var5 var6 ;
run;

 

proc sql;


create table wantp as
select distinct(person_id), min(col1) as minp, max(col1) as maxp
from tr_have
group by person_id;

 

create table wante as
select distinct(event_id),person_id, min(col1) as mine, max(col1) as maxe
from tr_have as a
group by event_id;

 

create table want as
select a.*,b.mine,b.maxe
from wantp as a left join wante as b
on a.person_id=b.person_id;


quit;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 742 views
  • 0 likes
  • 2 in conversation