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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.