0

I have the following dataset :

 ID   CODE  
 1     A
 1     B  
 2     A  
 2     A  
 2     B  
 3     A  
 3     B  

I would like to add a third column to this table which gives a sequence no. as given below :

 ID   CODE  SEQ
 1     A     1
 1     B     2
 2     A     1
 2     A     1
 2     B     2
 3     A     1
 3     B     2

How can I achieve this instead of coding A as 1 and B as 2 rather by a retain statement ?

0

1 Answer 1

2

You should look at by processing and first.. Something like this will work; basically, for each ID initialize seq to zero, and for each new code increment it by one.

data want;
  set have;
  by id code;
  if first.id then seq=0;
  if first.code then seq+1;
run;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.