SAS provides a function called VERIFY that can search through all characters in a character variable and return the position of the first character in the value that is not in the supplied excerpt.
To find an unprintable character, that is a character not in the standard ASCII code table or on the standard keyboard, create a character variable that contains all the standard characters for the ASCII code table (all values on a standard keyboard). This is done using the BYTE function inside a DO loop. Each character returned is concatenated together one after another to create the ASCII character string that will be used as the excerpt for the VERIFY function.
The ASCII characters are consecutive in the code table starting with the equivalent decimal value of 32 up through decimal value 127. In hexadecimal the values are hexadecimal 20 up through hexadecimal 7F. This makes it easy to use the BYTE function within a DO loop to pass in each decimal value and return each standard ASCII character, and concatenate them together in a new variable.
The sample program shown in the "Full Code" tab demonstrates how to create the ASCII character string using the BYTE function. A sample variable is created using hexadecimal notation to create a value of all ASCII characters except for one character. This non ASCII character is hexadecimal 96 which is the lower case letter o for EBCDIC data. The VERIFY function is used to return the position of the first character that is not in the ASCII string excerpt. In this case the position returned is 6.
Once you find the unwanted character you can then use a COMPRESS function or TRANSLATE function to either remove or replace the character. Use hexadecimal representation to specify the unwanted character for the function being used.
You can use this same technique to search EBCDIC character data to find any character that is unprintable and not on the standard EBCDIC code table. The only difference is that EBCDIC characters are not consecutive in the EBCDIC code table so you will use the BYTE function within a DO loop to create the string of ASCII characters (as shown in the sample program) and then convert the entire string from ASCII to EBCDIC. Just after the DO loop's END statement add the statement of: ebcdic_string=put(ascii_string,$ebcdic96.);. Then use the ebcdic_string variable in place of the ascii_string variable for the VERIFY function and name the resulting variable to not_ebcdic.
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 following sample shows how to find any character that is not a standard ASCII character.
If you need to find any character that is not a standard EBCDIC character then use the same sample program, and add the following statement after the DO loop's END statement:
ebcdic_string=put(ascii_string,$ebcdic96.);
This will convert the entire string from ASCII to EBCDIC. Change the variable named test_var to contain mostly EBCDIC data. Change the not_ascii variable to be named not_ebcdic, and then use the ebcdic_string variable in place of the ascii_string variable for the VERIFY function.
data test;
length ascii_string $ 96;
do x = 32 to 127;
ascii_string=trim(left(ascii_string))||byte(x);
end;
test_var="534153205396667477617265"x;
not_ascii=verify(test_var,ascii_string);
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.
Type: | Sample |
Topic: | SAS Reference ==> Functions ==> Character ==> BYTE SAS Reference ==> Functions ==> Character ==> COMPRESS SAS Reference ==> Functions ==> Character ==> TRANSLATE SAS Reference ==> Functions ==> Character ==> VERIFY Common Programming Tasks ==> Working with Character Data |
Date Modified: | 2010-12-24 10:56:43 |
Date Created: | 2010-12-22 09:23:32 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
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 |