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

Hi

When using PROC SQL, there is operator for 'contains' in addition to equals. Is there a way to use 'contains' functionality in a data step? I have the below data step that I apparently need to use something like 'contains' as there are data values that are not returned, but look exactly like those that are returned when using equals (possible spaces after 'Issue Joined', maybe).

Paul

data work.issuejoinedevents;

set sasD.court_events;

if event_detail_1 = 'Issue Joined';

run;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Use the INDEX function

If Index function returns a number > 0, then it "contains" the desired text.

If Index function returns a zero, then it does not "contain" the desired text.

--
Paige Miller

View solution in original post

16 REPLIES 16
PaigeMiller
Diamond | Level 26

Use the INDEX function

If Index function returns a number > 0, then it "contains" the desired text.

If Index function returns a zero, then it does not "contain" the desired text.

--
Paige Miller
Paul_NYS
Obsidian | Level 7

I think that did it, thank you.

Paul

Astounding
PROC Star

The suggestion about INDEX is spot on.  Just to help you understand, though, trailing blanks would not cause a problem.  Other situations would, such as (1) leading blanks, (2) spelling/capitalization differences, or (3) other trailing characters such as carriage returns/line feeds if they appear in the data.

Goood luck.

art297
Opal | Level 21

Paul,

You might find the findw function to have some additional features you might want/need.  Take a look at: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002978282.htm

PaigeMiller
Diamond | Level 26

INDEX is more general than FINDW, which as I understand it searches for words, while INDEX can search for arbitrary character strings

--
Paige Miller
art297
Opal | Level 21

: I think that findw can be configured to function the same way, but allow one to do such things as ignore case, etc.  E.g.:

data want;

  set sashelp.class;

  x=findw(name,'an',,'ks');

run;

PaigeMiller
Diamond | Level 26

INDEX can be configure to ignore case as well

--
Paige Miller
art297
Opal | Level 21

: Are you sure?  I thought it could only do things like ignore case if one builds the statement to include additional functions.

PaigeMiller
Diamond | Level 26

Yes, that's what I meant

--
Paige Miller
art297
Opal | Level 21

Paige,  I sit corrected!  I just did a comparison and, to my surprize, combining functions using the index function ran twice as fast as using the findw function.

I ran:

data class;

  set sashelp.class;

  do _n_=1 to 100000;

    output;

  end;

run;

data want;

  set class;

  x=findw(name,'AN',,'iks');

run;

data want;

  set class;

  x=index(upcase(name),'AN');

run;

PaigeMiller
Diamond | Level 26

Good to know. I wasn't even claiming a speed advantage, I just think INDEX is easier to program and easier to read than FINDW in this application.

--
Paige Miller
Ksharp
Super User

A more recently function is FIND() which is better than INDEX() , bescause it has more arguments than INDEX() ,has more power .

So if you want , you can use FIND() replace INDEX().

Ksharp

PaigeMiller
Diamond | Level 26

Thanks,

Sometimes I wish I would spend more time learning the new features of each SAS release, because I didn't know about FIND() ... I did know about FINDW()

--
Paige Miller
art297
Opal | Level 21

: FYI, the find() function appears to process faster than any of the other alternatives, with or without using any of its options.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 16 replies
  • 3307 views
  • 0 likes
  • 6 in conversation