## array and do loop

Solved
Frequent Contributor
Posts: 114

# array and do loop

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;

Accepted Solutions
Solution
‎11-03-2017 11:12 AM
Super User
Posts: 10,544

## Re: array and do loop

Array and do loop will do it:

``````array mems{5} mem1-mem5;
do i = rv_mon to 5;
mems{i} = 0;
end;
drop i;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
PROC Star
Posts: 1,400

## Re: array and do loop

[ Edited ]

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;``````
Solution
‎11-03-2017 11:12 AM
Super User
Posts: 10,544

## Re: array and do loop

Array and do loop will do it:

``````array mems{5} mem1-mem5;
do i = rv_mon to 5;
mems{i} = 0;
end;
drop i;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.