SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
Authors: SAS Institute
Published year: 2004
Pages: 413-415/704
Buy this book on amazon.com >>

SUBPAD Function

Returns a substring that has a length you specify, using blank padding if necessary

Category: Character

Syntax

SUBPAD ( string , position <, length >)

Arguments

string

  • specifies a character string.

position

  • is a positive integer that specifies the position of the first character in the substring.

length

  • is a non-negative integer that specifies the length of the substring. If you do not specify length , the SUBPAD function returns the substring that extends from the position that you specify to the end of the string.

Details

If the SUBPAD function returns a value to a variable that has not yet been assigned a length, by default the variable is assigned a length of 200.

If the substring that you specify extends beyond the length of the string, the result is padded with blanks.

Comparisons

The SUBPAD function is similar to the SUBSTR function except for the following differences:

  • If the value of length in SUBPAD is zero, SUBPAD returns a zero-length string. If the value of length in SUBSTR is zero, SUBSTR

    • writes a note to the log stating that the third argument is invalid

    • sets _ERROR_=1

    • returns the substring that extends from the position that you specified to the end of the string.

  • If the substring that you specify extends past the end of the string, SUBPAD pads the result with blanks to yield the length that you requested . If the substring that you specify extends past the end of the string, SUBSTR

    • writes a note to the log stating that the third argument is invalid

    • sets _ERROR_=1

    • returns the substring that extends from the position that you specified to the end of the string.

See Also

Function:

  • 'SUBSTRN Function' on page 840



SUBSTR (left of =) Function

Replaces character value contents

Category: Character

Syntax

SUBSTR ( variable, position <, length >)= characters -to-replace

Arguments

variable

  • specifies a character variable.

position

  • specifies a numeric expression that is the beginning character position.

length

  • specifies a numeric expression that is the length of the substring that will be replaced .

  • Restriction: length cannot be larger than the length of the expression that remains in variable after position .

  • Tip: If you omit length , SAS uses all of the characters on the right side of the assignment statement to replace the values of variable .

characters-to-replace

  • specifies a character expression that will replace the contents of variable .

  • Tip: Enclose a literal string of characters in quotation marks.

Details

If you use an undeclared variable, it will be assigned a default length of 8 when the SUBSTR function is compiled.

When you use the SUBSTR function on the left side of an assignment statement, SAS replaces the value of variable with the expression on the right side. SUBSTR replaces length characters starting at the character that you specify in position .

Examples

SAS Statements

Results


a='KIDNAP';


substr(a,1,3)='CAT';


put a;


CATNAP


b=a;


substr(b,4)='TY';


put b;


CATTY

See Also

Function:

  • 'SUBSTR (right of =) Function' on page 838



SUBSTR (right of =) Function

Extracts a substring from an argument

Category: Character

Syntax

< variable => SUBSTR ( string, position <, length >)

Arguments

variable

  • specifies a valid SAS variable name .

string

  • specifies any SAS character expression.

position

  • specifies a numeric expression that is the beginning character position.

length

  • specifies a numeric expression that is the length of the substring to extract.

  • Interaction: If length is zero, a negative value, or larger than the length of the expression that remains in string after position , SAS extracts the remainder of the expression. SAS also sets _ERROR_ to 1 and prints a note to the log indicating that the length argument is invalid.

  • Tip: If you omit length , SAS extracts the remainder of the expression.

Details

If the SUBSTR function returns a value to a variable that has not yet been assigned a length, by default the variable length is determined by the length of the first argument.

The SUBSTR function returns a portion of an expression that you specify in string . The portion begins with the character that you specify by position , and is the number of characters that you specify in length .

Examples

SAS Statements

Results

 

----+----1----+----2


date='06MAY98';


month=substr(date,3,3);


year=substr(date,6,2);


put @1 month @5 year;


MAY 98

See Also

Functions:

  • 'SUBPAD Function' on page 836

  • 'SUBSTR (left of =) Function' on page 837

  • 'SUBSTRN Function' on page 840


SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
Authors: SAS Institute
Published year: 2004
Pages: 413-415/704
Buy this book on amazon.com >>