DATA Step, Macro, Functions and more

Adding statistics to a data set

Reply
Contributor
Posts: 33

Adding statistics to a data set

[ Edited ]

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.

PROC Star
Posts: 288

Re: SAS

[ Edited ]

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;

 

Ask a Question
Discussion stats
  • 1 reply
  • 139 views
  • 0 likes
  • 2 in conversation