BookmarkSubscribeRSS Feed
CatM
Obsidian | Level 7

I have a group variable that has 3 values. I want to convert it into a variable that has 9 values. I want to know that those in oldvar=1 became newvar=1,2,3, oldvar=2 became newvar=4-6, and oldvar=3 became newvar=7-9. However, within oldvar=1, the observations that take newvar=1, 2, or 3 can happen randomly or can happen where the first third become newvar= 1, the second third become newvar=2, and the last third become 3. 

thanks!

1 REPLY 1
PaigeMiller
Diamond | Level 26

One solution

 

data want;
    set have;
    if oldvar=1 then newvar=mod(_n_-1,3)+1;
    else if oldvar=2 then newvar=mod(_n_-1,3)+4;
    else if oldvar=3 then newvar=mod(_n_-1,3)+7;
run;

which can be simplified to

 

data want;
    set have;
    newvar=mod(_n_-1,3)+(oldvar-1)*3+1;
run;

 

--
Paige Miller

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