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!
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.