BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Anita_n
Pyrite | Level 9

Dear all,

I did ask this question previously about how ids with the same values the same subjid are assigned :

 

data have;
input id 2. subjid $6. var1 $2. var2 $2. ;
datalines;
1  pat_1 A B
14 pat_2 C D
14 pat_3 E F
14 pat_4 G H
2  pat_5 I J
;
run;

and got this reply as solution:

data want;
   set have(rename= (subjid = _subjid));
   by id notsorted;
   
   length subjid $ 6;
   retain subjid;
   
   if first.id then subjid = _subjid;
   
   drop _subjid;
run;

 

If I want the first id 14 to have a subjid pat_2_A

              the second id 14 to have a subjid pat_2_B

 

       and the  third id 14 to have a subjid pat_2_C

 

instead of just pat_2, how to I do that?

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

have a look at

data want;
   set have(rename= (subjid = _subjid));
   by id notsorted;
   
   length 
      subjid $ 8 
      o_subjid $ 6
      count 8
   ;
   
   retain o_subjid count;
   
   if first.id then do;
      o_subjid = _subjid;
      count = 0;
   end;

   if not (first.id and last.id) then do;
      subjid = catx('_', o_subjid, byte(65 + count));
      count + 1;
   end;
   else do;
      subjid = o_subjid;
   end;
   
   drop o_subjid _subjid count;
run;

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

Not clear, what you expect as value for subjid, if there is only one observation for the id. Please explain.

Anita_n
Pyrite | Level 9

Am expecting something like this:

id var1 var2 subjid
1    A  B  pat_1
14  C D  pat_2_A
14  E F   pat_2_B
14  G H  pat_2_C
2    I J    pat_5

andreas_lds
Jade | Level 19

have a look at

data want;
   set have(rename= (subjid = _subjid));
   by id notsorted;
   
   length 
      subjid $ 8 
      o_subjid $ 6
      count 8
   ;
   
   retain o_subjid count;
   
   if first.id then do;
      o_subjid = _subjid;
      count = 0;
   end;

   if not (first.id and last.id) then do;
      subjid = catx('_', o_subjid, byte(65 + count));
      count + 1;
   end;
   else do;
      subjid = o_subjid;
   end;
   
   drop o_subjid _subjid count;
run;
Anita_n
Pyrite | Level 9
perfect, thank you.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 873 views
  • 0 likes
  • 2 in conversation