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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1 reply
  • 406 views
  • 0 likes
  • 2 in conversation