Sample 43303: Using the equivalent of CONTAINS and LIKE in an IF statement
Both the IF and WHERE statements can be used to subset data. The LIKE operator in a WHERE clause matches patterns in words. To get the equivalent result in an IF statement, the '=:' operator can be used. This matches patterns that occur at the beginning of a string. For example:
data test;
input name $;
datalines;
John
Diana
Diane
Sally
Doug
David
DIANNA
;
run;
data test;
set test;
if name =: 'D';
/* the syntax to select observations that */
/* do not match the pattern is below */
*if name not =: 'D';
/* equivalent WHERE clause */
*where name like 'D%';
run;
proc print;
run;
The CONTAINS operator in a WHERE clause checks for a character string within a value. To get the equivalent result in an IF statement, the FIND function can be used with the 'i' argument to ignore case. Another alternative is to use the INDEX and UPCASE functions. For example:
data test;
set test;
if find(name,'ian','i') ge 1;
/* equivalent WHERE clause */
*where upcase(name) contains 'IAN';
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Comparison to LIKE operator
Obs name
1 Diana
2 Diane
3 Doug
4 David
5 DIANNA
Comparison to CONTAINS operator
Obs name
1 Diana
2 Diane
3 DIANNA
The CONTAINS and LIKE operators are valid only in a WHERE clause. Other operators and functions can be used to get the equivalent behavior for an IF statement.
Type: | Sample |
Topic: | SAS Reference ==> Statements ==> Control ==> IF-THEN/ELSE
|
Date Modified: | 2011-05-20 15:42:15 |
Date Created: | 2011-05-18 11:47:57 |
Operating System and Release Information
SAS System | Base SAS | z/OS | | |
Z64 | | |
OpenVMS VAX | | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | | |
Microsoft Windows XP 64-bit Edition | | |
Microsoft® Windows® for x64 | | |
OS/2 | | |
Microsoft Windows 95/98 | | |
Microsoft Windows 2000 Advanced Server | | |
Microsoft Windows 2000 Datacenter Server | | |
Microsoft Windows 2000 Server | | |
Microsoft Windows 2000 Professional | | |
Microsoft Windows NT Workstation | | |
Microsoft Windows Server 2003 Datacenter Edition | | |
Microsoft Windows Server 2003 Enterprise Edition | | |
Microsoft Windows Server 2003 Standard Edition | | |
Microsoft Windows Server 2003 for x64 | | |
Microsoft Windows Server 2008 | | |
Microsoft Windows Server 2008 for x64 | | |
Microsoft Windows XP Professional | | |
Windows 7 Enterprise 32 bit | | |
Windows 7 Enterprise x64 | | |
Windows 7 Home Premium 32 bit | | |
Windows 7 Home Premium x64 | | |
Windows 7 Professional 32 bit | | |
Windows 7 Professional x64 | | |
Windows 7 Ultimate 32 bit | | |
Windows 7 Ultimate x64 | | |
Windows Millennium Edition (Me) | | |
Windows Vista | | |
Windows Vista for x64 | | |
64-bit Enabled AIX | | |
64-bit Enabled HP-UX | | |
64-bit Enabled Solaris | | |
ABI+ for Intel Architecture | | |
AIX | | |
HP-UX | | |
HP-UX IPF | | |
IRIX | | |
Linux | | |
Linux for x64 | | |
Linux on Itanium | | |
OpenVMS Alpha | | |
OpenVMS on HP Integrity | | |
Solaris | | |
Solaris for x64 | | |
Tru64 UNIX | | |