Statistical programming, matrix languages, and more

Using Data Set Inside Do Loops

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Using Data Set Inside Do Loops

Hi, I want to ask if this is possible.

data DataSet;
	call streaminit(1);
	do Set=1 to 3;
		do Number=1 to 10;
			Variable=rand("normal");
			output;
		end;
	end;
run;

proc iml;
	do IMLSet=1 to 3;
		use DataSet(where=(Set=IMLSet));
			read all var{Variable};
		close DataSet;
	end;
quit;

In this case, DataSet contains Variable, which is indexed by both Set and Number. In IML, I intended to load DataSet Set by Set so used a loop with an IML variable IMLSet, but this does not work since the "where" only uses the variables in DataSet and it does not have IMLSet there. Alternatives are welcome.


Accepted Solutions
Solution
‎12-10-2017 02:48 PM
SAS Super FREQ
Posts: 3,831

Re: Using Data Set Inside Do Loops

KSharp's solution is good. To read more about how to use the WHERE clause with dynamic values that change in a loop. See

"The WHERE clause in SAS/IML"

 

The only improvement I'd suggest to KSharp's code is to put the USE and CLOSE statements outside the DO loop. There is no need to open/close the data set multiple times. You can apply his solution to the OP's original attempt, as follows:

 

proc iml;
use DataSet;
	do IMLSet=1 to 3;
		read all var{Variable} where(Set=(IMLSet));
      print Variable;
	end;
close DataSet;
quit;

View solution in original post


All Replies
Super User
Posts: 10,194

Re: Using Data Set Inside Do Loops

data DataSet;
	call streaminit(1);
	do Set=1 to 3;
		do Number=1 to 10;
			Variable=rand("normal");
			output;
		end;
	end;
run;

proc iml;
use dataset;
read all var{set};
close;
level=unique(set);
do i=1 to ncol(level);
  use dataset;
  read all var _all_ into x where (set=(level[i]));
  print x;
  close;
end;
quit;

P.S. If you really want learn IML code, I really suggest you to read Rick's blog .You can find most answer of IML in it .

Solution
‎12-10-2017 02:48 PM
SAS Super FREQ
Posts: 3,831

Re: Using Data Set Inside Do Loops

KSharp's solution is good. To read more about how to use the WHERE clause with dynamic values that change in a loop. See

"The WHERE clause in SAS/IML"

 

The only improvement I'd suggest to KSharp's code is to put the USE and CLOSE statements outside the DO loop. There is no need to open/close the data set multiple times. You can apply his solution to the OP's original attempt, as follows:

 

proc iml;
use DataSet;
	do IMLSet=1 to 3;
		read all var{Variable} where(Set=(IMLSet));
      print Variable;
	end;
close DataSet;
quit;
Super User
Posts: 10,194

Re: Using Data Set Inside Do Loops

Yeah. I know what Rick mean. So here is the code revisited.

 

 

data DataSet;
	call streaminit(1);
	do Set=1 to 3;
		do Number=1 to 10;
			Variable=rand("normal");
			output;
		end;
	end;
run;

proc iml;
use dataset;

read all var{set};
level=unique(set);
do i=1 to ncol(level);
  read all var _all_ into x where (set=(level[i]));
  print x;
end;

close;
quit;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 284 views
  • 3 likes
  • 3 in conversation