BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Junyong
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

3 REPLIES 3
Ksharp
Super User
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 .

Rick_SAS
SAS Super FREQ

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;
Ksharp
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 1730 views
  • 3 likes
  • 3 in conversation