Example: quiz answers

SUGI 25: The SAS SUBSTR Function - A Beginner's Tutorial

Paper 88-25 The SAS SUBSTR Function A beginner s TutorialPaul D. McDonald, SPIKE ware, Inc., Schaumburg, ILABSTRACTThis paper is written for SAS Users and SAS Programmers of anyexperience level. It reviews the syntax, basic applications, andtroubleshooting of theSUBSTRData Step SAS data step functionSUBSTR(commonly pronounced sub-string ) Function is used to work with a specific position or positionsof characters within a defined character variable. The functionfocuses on a portion of a string and can go on either side of the = sign in a data step has three arguments: SUBSTR (SOURCE, POSITION, N)The Function returnsNcharacters, beginning at character numberPOSITION from the stringSOURCE. SOURCE This is the larger or reference string. It can be avariable or a string of characters. POSITION This value is a positive integer and references thestarting point to begin reading the internal group of characters. N This value is a positive integer and references the numberof characters to read from the starting pointPOSITIONin are two basic applications of theSUBSTR Function : on the right side of an assignment statement or on the left side of anassignment this example, we are going to assign the area code of a telephonenumber to a variable 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 ;Each time the Function is appli

Paper 88-25 The SAS SUBSTR Function –A Beginner’s Tutorial Paul D. McDonald, SPIKEware, Inc., Schaumburg, IL ABSTRACT This paper is written for SAS Users and SAS Programmers of any

Tags:

  Beginner, Functions, Sas substr function a beginner s, Substr

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of SUGI 25: The SAS SUBSTR Function - A Beginner's Tutorial

1 Paper 88-25 The SAS SUBSTR Function A beginner s TutorialPaul D. McDonald, SPIKE ware, Inc., Schaumburg, ILABSTRACTThis paper is written for SAS Users and SAS Programmers of anyexperience level. It reviews the syntax, basic applications, andtroubleshooting of theSUBSTRData Step SAS data step functionSUBSTR(commonly pronounced sub-string ) Function is used to work with a specific position or positionsof characters within a defined character variable. The functionfocuses on a portion of a string and can go on either side of the = sign in a data step has three arguments: SUBSTR (SOURCE, POSITION, N)The Function returnsNcharacters, beginning at character numberPOSITION from the stringSOURCE. SOURCE This is the larger or reference string. It can be avariable or a string of characters. POSITION This value is a positive integer and references thestarting point to begin reading the internal group of characters. N This value is a positive integer and references the numberof characters to read from the starting pointPOSITIONin are two basic applications of theSUBSTR Function : on the right side of an assignment statement or on the left side of anassignment this example, we are going to assign the area code of a telephonenumber to a variable 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 ;Each time the Function is applied, theSOURCEis a different type,but the end result is the first use pulls from the second pulls from hard-coded third pulls from thequoted macro variable& : 312 assigned to the variableAREA_CDeach SIDEAPPLICATIONS uppose you want to changejust a few characters of a variable use theSUBSTR Function on the left side of the _null_ ; phone = (312) 555-1212 ; SUBSTR (phone, 2, 3) = 773 ;run.

2 In this example, the area code of the variablePHONEwas changedfrom 312 to 773 .TROUBLESHOOTINGVALUETOOLARGEFOR POSITIONThe second argumentPOSITION cannot point past the end of data _null_ ;2 var1 = '12345' ;3 var2 = SUBSTR (var1, 7, 2) ;4 run ;NOTE: Invalid second argument to functionSUBSTR at line 3 column VAR2=12 _ERROR_=1 _N_=1In the above example,VAR1has a length of$5and theSUBSTR Function is supposed to read2characters, beginning in that2-character read from position7will attempt to start thepointer past the length ofSOURCEVAR1,theSUBSTR functioncannot work result of this type of incorrect usage is aNOTE:in the SAS NThe third argumentNcannot read past the end of data _null_ ;2 var1 = '12345' ;3 var2 = SUBSTR (var1, 2, 7) ;4 run ;NOTE: Invalid third argument to functionSUBSTR at line 3 column VAR2=2345 _ERROR_=1 _N_=1In the above example,VAR1has a length of$5,andtheSUBSTR Function is supposed to read7characters, beginning in that7-character read from position2will put the pointer pastthe length ofSOURCEVAR1,theSUBSTR Function cannot result of this type of incorrect usage is aNOTE:in the SAS POSITIVEINTEGERThe second and third argumentsPOSITIONandNmust be data _null_ ;2 var1 = '12345' ;3 var2 = SUBSTR (var1, -2, 1) ;4 run ;NOTE: Invalid second argument to functionSUBSTR at line 3 column VAR2=1 _ERROR_=1 _N_=1In the above example, argumentVAR1has a length of$5and theSUBSTR Function is supposed to read1characters beginning inposition 2.

3 Since it is impossible to begin in position 2before theCoders' Cornerstart of the string (just as it is impossible to begin after the end of thestring), theSUBSTR Function cannot work rule holds true when trying to read a negative string in theopposite direction: a negative number may not be placed in the result of this type of incorrect usage is aNOTE:in the SAS Any deviation from valid arguments results in aNOTE and notaWARNINGor ERROR message in the SAS Log Any occurrence of an invalid argument results in all variablesfrom that observation being written to the log BesuretocheckyourlogeachtimeyouusetheSUB STR functionCONTACT INFORMATIONYour comments andquestions are valued and author at:Paul D. McDonaldSPIKE ware, American Lane Suite1400 Schaumburg, IL60173 Work Phone: (847)330-4460 Fax: Corner


Related search queries