Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 11-28-2017 11:17 AM
(1739 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

11 REPLIES 11

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

To add to what Ian said, see this recent question:

and the blog post

"Finding observations that satisfy multiple conditions: The LOC-ELEMENT technique"

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi Rick_SAS, Please see my response to Ksharp, seeing your solution uses the element function.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

```
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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

```
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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I suspected that. Do you know of an alternative solution for SAS/IML 9.21_M?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you very much!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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 open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.