Dear SAS community-
I'm struggling to write a more elegant code for my current brute method below. I believe the below code can be done with a simple array and do loop, but I cannot seem to figure out the right combination. Here's a sample of my current code. Any advice?
Thanks!
if rv_mon=1 then do mem1=0; mem2=0; mem3=0; mem4=0; mem5=0; end;
else if rv_mon=2 then do mem2=0; mem3=0; mem4=0; mem5=0; end;
else if rv_mon=3 then do mem3=0; mem4=0; mem5=0; end;
else if rv_mon=4 then do mem4=0; mem5=0; end;
else if rv_mon=5 then do mem5=0; end;
Array and do loop will do it:
array mems{5} mem1-mem5;
do i = rv_mon to 5;
mems{i} = 0;
end;
drop i;
Code not tested, but perhaps something like this
data want(drop=i);
set yourdata;
array memVars{*} mem1-mem5;
do i=rv_mon to dim(memVars);
memVars[i]=0;
end;
run;
Array and do loop will do it:
array mems{5} mem1-mem5;
do i = rv_mon to 5;
mems{i} = 0;
end;
drop i;
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.