DATA Step, Macro, Functions and more

array and do loop

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 114
Accepted Solution

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

Posted in reply to sophia_SAS

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

View solution in original post


All Replies
PROC Star
Posts: 1,400

Re: array and do loop

[ Edited ]
Posted in reply to sophia_SAS

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

Posted in reply to sophia_SAS

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 144 views
  • 2 likes
  • 3 in conversation