sas variable difference with same id

265 views Asked by At

I appreciate a lot you guys, especially when i got problems about modulating with SAS.

I have a data set like a follows.

ID          key      score 
10002817 200207826243 0 
10002817 200207826271 0 
10002817 200208532180 0 
10002976 200301583978 0 
10003685 200302311690 0 
10006588 200401613047 0 
10006588 200502882618 0 
10009377 201007510866 1 
10009377 201111777969 0 
10011044 200801328219 2 
10011044 200803290654 3 
10011044 200803290728 1 
10011044 200803290905 1 
10011044 200803291161 0

Sometimes the id is repeated in the data or not. I want to see maximum difference in score according to ID. That is, a form like followings.

ID    key   score  diff_score
10002817 200207826243 0 0
10002817 200207826271 0 0
10002817 200208532180 0 0
10002976 200301583978 0 0
10003685 200302311690 0 0
10006588 200401613047 0 0
10006588 200502882618 0 0
10009377 201007510866 1 1
10009377 201111777969 0 1
10011044 200801328219 2 3
10011044 200803290654 3 3
10011044 200803290728 1 3
10011044 200803290905 1 3
10011044 200803291161 0 3

How can i make this with SAS?

It would be helpful if you help me. Thank you all.

1

There are 1 answers

1
Andrew Haynes On

You can do this using proc sql:

proc sql;
  create table want as 
  select ID, key, score, max(score)-min(score) as diff_score
  from have
  group by ID;
quit;

One of the advantages of using proc sql is your data doesn't need to be sorted for this to work.