Statistical programming, matrix languages, and more

Proc IML: If value in vector /matrix

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Proc IML: If value in vector /matrix

[ Edited ]

Hello,

I'm trying to figure out a efficient way to see if a value, numerical or character, is in a vector.

 

The optimal way of doing it would be if A in B.. But i havent managed to get this to work, nor find any information about it under documentation.

a simple solution to the problem is:

A={1 2 3 4 5 6 7 8 9 10};
B={11 12 13 14 15 16 17 43 33 3};
do i=1 to ncol(B);

     if any(A=B[i]) then print "yes";
end;

This is however quite messy. Is there an easy solution to my problem such as in datastep: where X in (' ', ' ')?

 

*EDIT* i may add that i have SAS/IML 9.21_M.


Accepted Solutions
Solution
‎12-01-2017 08:01 AM
SAS Super FREQ
Posts: 4,023

Re: Proc IML: If value in vector /matrix

I looked through my archives and found this SAS/IML module that emulates the ELEMENT function:

 

proc iml;

start ElementFunc(x, y);
   if ncol(x)=0 then return(_NULL_);
   b = j(nrow(x), ncol(x), 0);
   if ncol(y)=0 then return(b);
   
   u = xsect(x, y);
   if ncol(u)=0 then return(b);
   
   do i = 1 to ncol(u);
      b = bor(b, (x=u[i]));
   end;
   return (b);
finish;

x = {0, 0.5, 1, 1.5, 2, 2.5, 3, 0.5, 1.5, 3, 3, 1};
set = {0, 1, 3};
check = {1,0,1,0,0,0,1,0,0,1,1,1};  /* the correct result */
a = ElementFunc(x, set);  /* compare with  b = element(x, set); */
print set, x a check;

View solution in original post


All Replies
Regular Contributor
Posts: 162

Re: Proc IML: If value in vector /matrix

[ Edited ]

Have a look at the set functions XSECT and ELEMENT.

 

x = xsect(A, B);

 

will give a row vector with the values that are common to both A and B.

 

idx = loc(element(B, x));

 

will give a vector with the values of the loop variable i that correspond to a "yes".

SAS Super FREQ
Posts: 4,023

Re: Proc IML: If value in vector /matrix

[ Edited ]
Posted in reply to IanWakeling
Contributor
Posts: 26

Re: Proc IML: If value in vector /matrix

Hi Rick_SAS, Please see my response to Ksharp, seeing your solution uses the element function.
Super User
Posts: 10,511

Re: Proc IML: If value in vector /matrix

proc iml;
A={1 2 3 4 5 6 7 8 9 10};
B={11 12 13 14 15 16 17 43 33 3};
     if any(element(A,B)) then print "yes";
	  else print 'no';
quit;
Super User
Posts: 10,511

Re: Proc IML: If value in vector /matrix

proc iml;
A={1 2 3 4 5 6 7 8 9 10};
B={11 12 13 14 15 16 17 43 33 3};
     if any(element(A,B)) then print "yes";
	  else print 'no';
quit;
Contributor
Posts: 26

Re: Proc IML: If value in vector /matrix

Hi, when i try to run the code u posted i get the following message. Is it due to me having an too old SAS client?

 

 

124 proc iml;
NOTE: IML Ready

141 A={1 2 3 4 5 6 7 8 9 10};
142 B={11 12 13 14 15 16 17 43 33 3};
143 if any(element(A,B)) then print "yes";
144 else print 'no';
ERROR: Invocation of unresolved module ELEMENT.

statement : IF at line 143 column 6
145 quit;
NOTE: Exiting IML.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE IML used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

SAS Super FREQ
Posts: 4,023

Re: Proc IML: If value in vector /matrix

The ELEMENT function was introduced in SAS 9.3, which was shipped in 2011. There have been seven releases of SAS since then. The most current release is the SAS 9.4M5. The "M5" means it is the fifth release of SAS 9.4.

Contributor
Posts: 26

Re: Proc IML: If value in vector /matrix

I suspected that. Do you know of an alternative solution for SAS/IML 9.21_M?
Solution
‎12-01-2017 08:01 AM
SAS Super FREQ
Posts: 4,023

Re: Proc IML: If value in vector /matrix

I looked through my archives and found this SAS/IML module that emulates the ELEMENT function:

 

proc iml;

start ElementFunc(x, y);
   if ncol(x)=0 then return(_NULL_);
   b = j(nrow(x), ncol(x), 0);
   if ncol(y)=0 then return(b);
   
   u = xsect(x, y);
   if ncol(u)=0 then return(b);
   
   do i = 1 to ncol(u);
      b = bor(b, (x=u[i]));
   end;
   return (b);
finish;

x = {0, 0.5, 1, 1.5, 2, 2.5, 3, 0.5, 1.5, 3, 3, 1};
set = {0, 1, 3};
check = {1,0,1,0,0,0,1,0,0,1,1,1};  /* the correct result */
a = ElementFunc(x, set);  /* compare with  b = element(x, set); */
print set, x a check;
Contributor
Posts: 26

Re: Proc IML: If value in vector /matrix

Thank you very much!
Super User
Posts: 10,511

Re: Proc IML: If value in vector /matrix

How about this one ?

 

proc iml;
A={1 2 3 4 5 6 7 8 9 10};
B={11 12 13 14 15 16 17 43 33 3};
if ncol(xsect(A,B))=0 then print 'No' ; 
 else print 'Yes';
quit;
☑ This topic is solved.

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

Discussion stats
  • 11 replies
  • 438 views
  • 4 likes
  • 4 in conversation