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.
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;
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".
To add to what Ian said, see this recent question:
and the blog post
"Finding observations that satisfy multiple conditions: The LOC-ELEMENT technique"
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;
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;
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
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.
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;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.