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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 1 reply
  • 582 views
  • 0 likes
  • 2 in conversation