BookmarkSubscribeRSS Feed
Khaladdin
Quartz | Level 8

I have a question and want to ask question by using example. My data-set is:

Group Value
1         10
1         8
1        12
2        13
2        11
2        7
I want to add two columns to this data-set. First column should consist of maximum value of second column by group. Second column should consist of minimum value of second column by group. So, the result should be look:

Group Value Max Min
1          10     12   8
1          8       12   8
1         12      12   8
2         13      13   7
2        11       13   7
2         7        13   7

12 - because there are 3 numbers (10,8,12) in group number 1 and 12 is maximum among these values.
13 - because there are 3 numbers (13,11,7) in group number 2 and 13 is maximum among these values.
8 - because there are 3 numbers (10,8,12) in group number 1 and 8 is minimum among these values.
7 - because there are 3 numbers (13,11,7) in group number 2 and 7 is minimum among these values.
I hope, i can explain it..
Many thanks in advance.

1 REPLY 1
collinelliot
Barite | Level 11

You can get what you want easily with SQL: 

 

proc sql;
    CREATE TABLE want AS
    SELECT *, 
                  min(value) AS min
                  max(value) AS max
    FROM have
    GROUP BY group;
quit;

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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
  • 1138 views
  • 0 likes
  • 2 in conversation