An Introduction To SAS Character Functions (Including Some New SAS 9 Functions)
An Introduction To SAS Character Functions (Including Some New SAS 9 Functions)
An Introduction To SAS Character Functions (Including Some New SAS 9 Functions)
FIND FINDC
Functions That Compute the Length of Strings: Purpose: To determine the length of a character value, not counting trailing blanks. Syntax: LENGTH(character-value)
Example: For these examples CHAR = "ABC
FUNCTION LENGTH("ABC") LENGTH(CHAR) LENGTH(" ") RETURNS 3 3 1
Function: LENGTHC
Purpose: To determine the length of a character value, including trailing blanks.
Function: LENGTHM
Purpose: To determine the length of a character variable in memory. Syntax: LENGTHM(character-value)
RETURNS 3 6 1
Function: LENGTHN
Purpose: To determine the length of a character value, not counting trailing blanks.
Syntax: LENGTHN(character-value)
FUNCTION
LENGTH("ABC") LENGTH(CHAR) LENGTH(" ")
RETURNS
3 3 0
value
V30.450 V30.450 V30.450
c1
-4 0 1
c2
-4 0 -6
c3
0 0 0
SAS Log
11 12 13 14 15 16 17 18 19 20 data chars1; length string $ 7; string = 'abc'; storage_length = lengthc(string); length = length(string); display = ":" || string || ":"; put storage_length= / length= / display=; run;
SAS Log
1 data chars2; 2 string = 'abc'; 3 length string $ 7; WARNING: Length of character variable string has already been set. Use the LENGTH statement as the very first statement in the DATA STEP to declare the length of a character variable. 4 storage_length = lengthc(string); 5 length = length(string); 6 display = ":" || string || ":"; 7 put storage_length= / 8 length= / 9 display=; 10 run;
Function: SUBSTR
Purpose: To extract part of a string. When the SUBSTR function is used on the left side of the equal sign. Syntax: SUBSTR(character-value, start <,length>) Examples For these examples, let STRING = "ABC123XYZ"
Function
Returns
SUBSTR(STRING,4,2) SUBSTR(STRING,4)
"12" "123XYZ"
DATA SUBSTRING; INPUT ID $ 1-9; LENGTH STATE $ 2; STATE = SUBSTR(ID,1,2); NUM = INPUT(SUBSTR(ID,7,3),3.); DATALINES; NYXXXX123 NJ1234567 ; PROC PRINT DATA=SUBSTRING NOOBS; TITLE 'Listing of Data Set SUBSTRING'; RUN;
Multiple
Name Ron Cody Bill Brown City Flemington North City Address 89 Lazy Brook Rd. 28 Cathy Street State NJ NY Zip 08822 11518
Function: TRIM
Purpose: To remove trailing blanks from a character value. Syntax: TRIM(character-value) Examples For these examples, STRING1 = "ABC Function TRIM(STRING1) TRIM(STRING2) Returns "ABC" " XYZ"
XYZ
TRIM("A B C
")
"A B C"
"AB" " " (length = 1)
Function: STRIP Purpose: To strip leading and trailing blanks from character variables or strings.
STRIP(CHAR) is equivalent to TRIMN(LEFT(CHAR)), but more convenient. Syntax: STRIP(character-value) Examples For these examples, let STRING = "
abc
STRIP Function
data _null_; string = ' Testing '; try1 = strip(string); try2 = trim(left(string)); put string= quote12./ try1= quote12./ try2= quote12.; run;
Partial log:
string=" Testing" try1="Testing" try2="Testing"
Substring Example
data pieces_parts; input Id $9.; length State $ 2; state = substr(id,1,2); Num = input(substr(id,7,3),3.); datalines; Listing of Data Set PIECES_PARTS NYXXXX123 NJ1234567 Id State Num ;
NYXXXX123 NJ1234567 NY NJ 123 567
Changing Case
Data case; input name $15.; upper = upcase(name); lower = lowcase(name); proper = propcase(name); Datalines; gEOrge SMITH The end ;
Listing of Data Set CASE name gEOrge SMITH The end upper GEORGE SMITH THE END lower george smith the end proper George Smith The End
Parsing a String
data take_apart; input @1 Cost $10.; Integer = input(scan(Cost,1,' /'),8.); Num = input(scan(Cost,2,' /'),8.); Den = input(scan(Cost,3,' /'),8.); if missing(Num) then Amount = Integer; else Amount = Integer + Num/Den; datalines; Listing of Data Set TAKE_APART 1 3/4 12 1/2 Cost Integer Num Den Amount 123 ; 1 3/4 1 3 4 1.75
12 1/2 123 12 123 1 . 2 . 12.50 123.00
index = 1 indexw = 15
Note: You can specify delimiters for indexw in a third argument
Spelling distance
data compare; length string1 string2 $ 15; input string1 string2; points = spedis(string1,string2); datalines; Listing of Data Set COMPARE same same same sam string1 string2 points first xirst last lasx same same 0 receipt reciept same sam 8 ; first xirst 40
last receipt lasx reciept 25 7
Concatenation Functions
data join_up; length cats $ 6 catx $ 17; string1 = 'ABC '; string2 = ' XYZ '; string3 = '12345'; cats = cats(string1,string2); catx = catx('***',string1,string2,string3); run;
Without the length statement, cats and catx would have a length of 200
XYZ
Contact Information
Author: Ron Cody You may download copies of the Powerpoint presentation from: www2.umdnj.edu/codyweb/biocomputing