BookmarkSubscribeRSS Feed
CynthiaWei
Obsidian | Level 7

Hi SAS Pros,

 

I have a dataset that each ID has many observations. I want to create two variables. One is called combination with the values from a1 to a5. The other is called type, which is assigned with different value for different combination and same value for same combination. But the combinations assigned with a same number between different IDs don't necessarily have to be the same. For example, the combination of 'axoo1' for ID=1 with a value of 1 in type is different with the combination of 'esjl4' for ID=2 who also has a value of 1 in type. The value for type within each ID should be continuous.


data Have; length a1 $1 a2 $1 a3 $1 a4 $1 a5 ; infile datalines; input ID a1 a2 a3 a4 a5 ; return; datalines; 1 a x o o 1 1 b x o p 2 1 c y q s 6 1 c y q s 6 2 e s j l 4 2 h e w p 5 2 k k u u 9 2 k k u u 9 2 q q s i 7 ; run;

What I want:

ID a1 a2 a3 a4 a5 type combination

1 a x o o 1              1    axoo1

1 b x o p 2              2    bxop2

1 c y q s 6              3    cyqs6

1 c y q s 6              3    cyqs6

2 e s j l 4              1    esjl4

2 h e w p 5              2    hewp5

2 k k u u 9              3    kkuu9

2 k k u u 9              3    kkuu9

2 q q s i 7              4    qqsi7

 

Thank you so much for any help!

Best regards,

C

1 REPLY 1
Shmuel
Garnet | Level 18

data want;

 set have;

   by ID;

      length comb combination $5;

      retain comb type;

      combination = cats(a1, a2, a3, a4, a5);   

      ***(or try combination = cats(of a1-a5);

      if first.id then do;

          type=1;

          output;

          comb=combination;

      end;

      else do;

          if combination ne comb then type+1;

          output;

          comb=combination;

      end;

run;

       

     

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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