DATA Step, Macro, Functions and more

Data step with 'contains' logic

Accepted Solution Solved
Reply
Regular Contributor
Posts: 216
Accepted Solution

Data step with 'contains' logic

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;


Accepted Solutions
Solution
‎11-07-2012 08:44 AM
Trusted Advisor
Posts: 1,931

Re: Data step with 'contains' logic

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.

View solution in original post


All Replies
Solution
‎11-07-2012 08:44 AM
Trusted Advisor
Posts: 1,931

Re: Data step with 'contains' logic

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.

Regular Contributor
Posts: 216

Re: Data step with 'contains' logic

Posted in reply to PaigeMiller

I think that did it, thank you.

Paul

Super User
Posts: 5,513

Re: Data step with 'contains' logic

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.

PROC Star
Posts: 7,480

Re: Data step with 'contains' logic

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

Trusted Advisor
Posts: 1,931

Re: Data step with 'contains' logic

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

PROC Star
Posts: 7,480

Re: Data step with 'contains' logic

Posted in reply to PaigeMiller

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

Trusted Advisor
Posts: 1,931

Re: Data step with 'contains' logic

INDEX can be configure to ignore case as well

PROC Star
Posts: 7,480

Re: Data step with 'contains' logic

Posted in reply to PaigeMiller

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

Trusted Advisor
Posts: 1,931

Re: Data step with 'contains' logic

Yes, that's what I meant

PROC Star
Posts: 7,480

Re: Data step with 'contains' logic

Posted in reply to PaigeMiller

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;

Trusted Advisor
Posts: 1,931

Re: Data step with 'contains' logic

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.

Super User
Posts: 10,035

Re: Data step with 'contains' logic

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

Trusted Advisor
Posts: 1,931

Re: Data step with 'contains' logic

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()

PROC Star
Posts: 7,480

Re: Data step with 'contains' logic

Posted in reply to PaigeMiller

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

🔒 This topic is solved and locked.

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

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