Problem Note 70279: The SUBSTRN and KSUBSTRN functions used in PROC SQL with a numeric first argument return an incorrect result
When used in an SQL procedure step and the first argument is a numeric column or expression, the SUBSTRN and KSUBSTRN functions incorrectly return a numeric result with a value of 0.
The SUBSTRN and KSUBSTRN functions are unique because they accept either a character or numeric argument as their first argument. When you use these functions in a DATA step, the following occurs:
- These functions automatically convert a numeric argument to a character string using the BEST32. format.
- They remove leading and trailing spaces.
- They perform the sub-string on that converted value, which results in a character value as output.
In this scenario, no messages are written to the SAS log to indicate that the automatic numeric-to-character conversion occurs.
However, when you use these functions in PROC SQL, the numeric result is a value of 0. No indication is given that the result is not what was expected.
Example
The code below demonstrates the different results when using SUBSTRN in a DATA step and PROC SQL. The expected result is a character variable with a value of 34:
data work.sample ;
argument = 123456 ;
DataStepExample = substrn(argument,3,2) ;
run ;
proc sql ;
create table work.results as
select argument,
DataStepExample,
substrn(argument,3,2) as ProcSqlExample
from sample ;
quit ;
proc print data=work.results ; run ;
ods select Position ;
proc contents data=work.results varnum ; run ;
Here's the output:
The CONTENTS Procedure
argument |
Num |
8 |
DataStepExample |
Char |
32767 |
ProcSqlExample |
Num |
8 |
Workaround
A workaround is to revert to the SUBSTR function, and explicitly convert the number to a character argument. You can modify the FORMAT length of 32 in this example to accommodate the length of the value and/or you can use the optional LENGTH specification to control the length of the result:
substr(strip(put(argument,best32.)),3,2) length=32767
Click the Hot Fix tab in this note to access the hot fix for this issue.
Operating System and Release Information
SAS System | Base SAS | z/OS | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows Server 2012 R2 Datacenter | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows Server 2012 Datacenter | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows Server 2008 for x64 | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows Server 2008 R2 | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows 10 | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows 8.1 Pro x64 | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows 8.1 Pro 32-bit | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows 8.1 Enterprise x64 | 9.4 | | 9.4 TS1M0 | |
z/OS 64-bit | 9.4 | | 9.4 TS1M0 | |
Microsoft® Windows® for x64 | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows 8 Enterprise x64 | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows 8 Pro x64 | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows 8.1 Enterprise 32-bit | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows Server 2012 R2 Std | 9.4 | | 9.4 TS1M0 | |
Microsoft Windows Server 2012 Std | 9.4 | | 9.4 TS1M0 | |
Windows 7 Enterprise x64 | 9.4 | | 9.4 TS1M0 | |
Windows 7 Professional x64 | 9.4 | | 9.4 TS1M0 | |
64-bit Enabled AIX | 9.4 | | 9.4 TS1M0 | |
64-bit Enabled Solaris | 9.4 | | 9.4 TS1M0 | |
HP-UX IPF | 9.4 | | 9.4 TS1M0 | |
Linux for x64 | 9.4 | | 9.4 TS1M0 | |
Solaris for x64 | 9.4 | | 9.4 TS1M0 | |
*
For software releases that are not yet generally available, the Fixed
Release is the software release in which the problem is planned to be
fixed.
The SUBSTRN and KSUBSTRN functions should return a character string, but instead they return an incorrect numeric result with a value of 0.
Type: | Problem Note |
Priority: | high |
Topic: | SAS Reference ==> Functions ==> Character ==> SUBSTRN SAS Reference ==> SQL
|
Date Modified: | 2023-09-28 12:44:26 |
Date Created: | 2023-07-27 03:21:27 |