Sample 40344: Determine if a variable contains any character besides standard alphanumeric characters
Determine if a value contains any characters besides standard alphanumeric characters. Output to the data set observations with only alphanumeric characters.
The ANYALPHA and ANYDIGIT functions evaluate each byte individually and if both return a 0 value, it means the single byte is something other than an alphanumeric character. The observation is deleted and the remainder of the value doesn't have to be searched.
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.
The LENGTH function obtains the length of Y and uses the value as the upper boundary of the DO loop. Within the loop, the SUBSTR function pulls off each byte of Y and evaluates it with ANYALPHA and ANYDIGIT. When both are false, a non-alphanumeric character is in the value. The observation is deleted and execution returns to the top of the DATA step.
The newly formed data set contains observations containing only standard alphanumeric characters.
/* sample data set for testing purposes*/
data one;
y='1@1abcd';output;
y='abc123';output;
y='0D0A'x; output;
run;
data b;
set one;
len=length(y);
do i=1 to len;
str=substr(y,i,1);
if anyalpha(str) = 0 and anydigit(str) = 0 then do;
delete;
end;
end;
run;
proc print data=b;
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.
Obs y len i str
1 abc123 6 7 3
| Type: | Sample |
| Topic: | SAS Reference ==> Functions ==> Character ==> ANYALPHA SAS Reference ==> Functions ==> Character ==> ANYDIGIT
|
| Date Modified: | 2010-07-29 10:17:42 |
| Date Created: | 2010-07-14 14:52:59 |
Operating System and Release Information
| SAS System | Base SAS | z/OS | 9 TS M0 | |
| Microsoft® Windows® for 64-Bit Itanium-based Systems | 9 TS M0 | |
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9 TS M0 | |
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9 TS M0 | |
| Microsoft Windows 2000 Advanced Server | 9 TS M0 | |
| Microsoft Windows 2000 Datacenter Server | 9 TS M0 | |
| Microsoft Windows 2000 Server | 9 TS M0 | |
| Microsoft Windows 2000 Professional | 9 TS M0 | |
| Microsoft Windows NT Workstation | 9 TS M0 | |
| Microsoft Windows Server 2003 Datacenter Edition | 9 TS M0 | |
| Microsoft Windows Server 2003 Enterprise Edition | 9 TS M0 | |
| Microsoft Windows Server 2003 Standard Edition | 9 TS M0 | |
| Microsoft Windows XP Professional | 9 TS M0 | |
| 64-bit Enabled AIX | 9 TS M0 | |
| 64-bit Enabled HP-UX | 9 TS M0 | |
| 64-bit Enabled Solaris | 9 TS M0 | |
| HP-UX IPF | 9 TS M0 | |
| Linux | 9 TS M0 | |
| OpenVMS Alpha | 9 TS M0 | |
| Tru64 UNIX | 9 TS M0 | |