Substring Function

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Notes on slide 1

    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

    Favorites, Groups & Events

    Substring Function - Presentation Transcript

    1. The SAS TM SUBSTR Function A Beginner’s Tutorial
    2. The SUBSTR Function
      • focuses on a portion of a string
      • can go on either side of the “=” sign!
      • SYNTAX: SUBSTR(ARGUMENT, POSITION, N) returns N characters, beginning at character number POSITION from the string ARGUMENT .
      • First use pulls from the variable PHONE
      • Second pulls from hard-coded string
      • Third pulls from a macro variable
      • The result: ‘312’ assigned to the variable AREA_CD each time
      Common SUBSTR Function Applications %let phone = (312) 555-1212 ; data _null_ ; phone = ‘(312) 555-1212’ ; area_cd = substr(phone, 2, 3) ; area_cd = substr(‘(312) 555-1212’, 2, 3) ; area_cd = substr(“&phone”, 2, 3) ; run ; SAMPLE CODE
    3. 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
    4. 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
    5. 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
    6. 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
    7. 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
    8. 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
    SlideShare Zeitgeist 2009

    + paulmcdonaldpaulmcdonald Nominate

    custom

    2238 views, 0 favs, 0 embeds more stats

    The Substring function in SAS Programming

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2238
      • 2238 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 2
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?