SAS Tip: Use the CONTAINS Operator as an Alternative to the INDEX Function (Daily tip for 2022-Oct-31)


The CONTAINS operator can be used to look for a user-specified string in a character variable.

Like the LIKE operator, the CONTAINS operator is case-sensitive, and can only be used within a WHERE clause.

While the CONTAINS operator is similar to the INDEX function, it returns a Boolean value rather than the position of the character string.

The two steps below are equivalent.

The CONTAINS keyword can be replaced with the shortcut ? (question mark). 

 

data CHECK;
  set SASHELP.ZIPCODE(where=(COUNTYNM contains 'z'));
run;

proc sql;
  create table CHECK as
  select * 
  from SASHELP.ZIPCODE
  where COUNTYNM ? 'z';
quit;

 See the documentation for more information on the WHERE operators.


Thanks to Otterm1 for sharing this tip on sasCommunity.org.

READ this complete tip   Visit a random SAS tip

Start a topic
About these Tips
A "SAS tip" is a basic unit of information exchange between SAS users. The tips on this board come from sasCommunity.org, from SAS user conferences, from SAS blogs, and from freelance contributions from SAS users like you. We hope that you're able to always learn something new here!


» How you can contribute


Tip-o-meter: 179 tips

Twitter Stream