BookmarkSubscribeRSS Feed
bignate1030
Fluorite | Level 6

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! 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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;
    
--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

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;
bignate1030
Fluorite | Level 6
Instead of using var2-var6 I now want to use only 3 variables:

Column6M Column1Y Column100D

I know I can specify

Array X{3} Column6M Column1Y Column100D

But is there a way that I can specify the range Column6M-Column100D as in
the first example? When I try that syntax it gives me an error of
"Alphabetic prefixes for enumerated variables are different.
PeterClemmensen
Tourmaline | Level 20

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 839 views
  • 4 likes
  • 3 in conversation