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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

11 REPLIES 11
IanWakeling
Barite | Level 11

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".

Shawnty
Obsidian | Level 7
Hi Rick_SAS, Please see my response to Ksharp, seeing your solution uses the element function.
Ksharp
Super User
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;
Ksharp
Super User
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;
Shawnty
Obsidian | Level 7

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

Rick_SAS
SAS Super FREQ

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.

Shawnty
Obsidian | Level 7
I suspected that. Do you know of an alternative solution for SAS/IML 9.21_M?
Rick_SAS
SAS Super FREQ

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;
Shawnty
Obsidian | Level 7
Thank you very much!
Ksharp
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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