Audience This paper is written for SAS Users and SAS Programmers of any experience level Abstract This presentation reviews the syntax, basic applications, and troubleshooting of the SUBSTR Data Step Function Trademarks and Copyright Notices The SAS Software System and its components are trademarks of the SAS Institute, Inc. in Cary, NC The information contained in this paper is copyright by SPIKEware, Inc. Permission to copy and distribute is granted provided no changes are made and the document is copied and distributed in its entirety. This material is provided "as is" by SPIKEware, Inc. There are no warranties, express or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. SPIKEware is not responsible for errors in this material as it now exists or will exist, nor does SPIKEware provide technical support for it. Questions or problem reports concerning this material may be addressed to SPIKEware by electronic mail: Webmaster@SPIKEware.com
Also Works On the “Left-Hand” Side of the “=” Sign.
VAR1 changes from ‘12345’ to ’17845’
Can “put into” a variable just as easily as it can “extract out of” a variable
1 data _null_ ; 2 var1 = '12345' ; 3 var2 = '67890' ; 4 substr(var1, 2, 2) = substr(var2, 2, 2) ; 5 put var1 = var2 = ; 6 run ; VAR1=17845 VAR2=67890 NOTE: DATA statement used: real time 0.13 seconds cpu time 0.05 seconds
Common Mistakes: Value Too Large for N
N (7) > length VAR1 ( $5)
“ Invalid third argument ... ” Note
Length of VAR1 passed to the length of VAR2
SUBSTR function pulls as much as possible from the starting POSITION parameter
1 data _null_ ; 2 var1 = '12345' ; 3 var2 = substr(var1, 2, 7) ; 4 run ; NOTE: Invalid third argument to function SUBSTR at line 3 column 11. VAR1=12345 VAR2=2345 _ERROR_=1 _N_=1
Common Mistakes: Value Too Large for POSITION
POSITION (7) > length VAR1 ( $5)
“ Invalid second argument ... ” Note
Length of VAR1 passed to the length of VAR2
SUBSTR function pulls as much as called (in this case 2) from the starting POSITION of 1
1 data _null_ ; 2 var1 = '12345' ; 3 var2 = substr(var1, 7, 2) ; 4 run ; NOTE: Invalid second argument to function SUBSTR at line 3 column 12. VAR1=12345 VAR2=12 _ERROR_=1 _N_=1
Common Mistakes: Value not Positive Integer
POSITION (-2) is not a Positive Integer
“ Invalid second argument ... ” Note
Length of VAR1 passed to the length of VAR2
SUBSTR function pulls as much as called (in this case 1) from the starting POSITION of 1
1 data _null_ ; 2 var1 = '12345' ; 3 var2 = substr(var1, -2, 1) ; 4 run ; NOTE: Invalid second argument to function SUBSTR at line 3 column 12. VAR1=12345 VAR2=1 _ERROR_=1 _N_=1
Troubleshooting Summary
Any deviation from valid arguments results in a NOTE and not a WARNING or ERROR message in the SAS Log
Any occurrence of an invalid argument results in all variables from that observation being written to the log
Be sure to check your log each time you use the SUBSTR function
About the Author
SPIKEware, Inc.
115 ½ West Main Street
West Dundee, IL 60118
Phone (847) 428-6250
Fax (847) 428-6270
SPIKEware, Inc. is a consulting firm dedicated to helping other people solve their problems through technology. We specialize in providing solutions for our customers through the SAS Software System.
http://www. SPIKEware .com/
Paul D. McDonald is the CEO of SPIKEware, Inc. and has been a SAS programmer since 1993. Paul has an A.A. in Electrical Engineering from Cloud County Community College, a B.A. in Physics from Southwestern College, and an M.B.A. in Finance from Keller Graduate School of Management. Paul can be reached by e-mail at PDM@SPIKEware.com
0 comments
Post a comment