Characater Functions
Characater Functions
Characater Functions
sastrainer@onlinesastraining.com
D = Digits
A = Alpha (upper and lower)
S = Space
P = Punctuations
Examples:
STRING FUNCTION OUTPUT
‘AB aC AD RON ’ COMPRESS(String,’A','i') ‘BC DRON ’
‘A(919)- B998- 1032zaghH ’ COMPRESS(String, ,'a') ‘(919)- 998- 1032’
‘A(919)- B998- 1032zaghH.'-* ’ COMPRESS(String, ,'p') ‘A919 B998 1032zaghH’
‘A(919)- B998- 1032zaghH.'-*’ COMPRESS(String, ,'kds') ‘919 998 1032’
‘A(919)- B998- 1032zaghH.'-*’ COMPRESS(String,'0','kds') ‘919 998 1032’
(A) INDEX and INDEXC - functions return the position where the char
or string of characters first occur in the string.
Explanation:
INDEX (string,'her') searches for the substring ‘her’ in the string and returns the
position of 10.
INDEXC (string,'her') searches the string for either ‘h’,’e’ or ‘r’ and returns the
position of 2 which is when it first encounters letter ‘h’.
INDEXW (string,'her') searches the string for the word ‘her’ and returns the
position of 15 where if first finds ‘her’.
Index function is case sensitive, so when we search for upcases or lowcases, we have
to mention either those functions or give the search keyword following the case
But that won’t be so fun or easy to do every time, so SAS introduced one more function
called FIND
Pavani K, SAS trainer
sastrainer@onlinesastraining.com
Explanation:
FIND(String,‘HER,’i’) uses modifier ‘i’ which ignores case. The function now searches for substring
‘HER’ as well as ‘her’ and returns that position.
FIND(String,'HER‘,'i',11) searches for substring ‘HER’ by ignoring case starting from position 11. The
function thus returns 15.
FINDC(String,‘HER,’i’) searches for any string other than ‘H’ or ‘E’ or ‘R’ by ignoring case and hence
returns value of 2.
(4) Functions That Extract Parts of Strings
(A) SUBSTR- SAS SUBSTR( ) is mainly used for extracting a part of string
Syntax: SUBSTR (char_string, start_position,no_of_chars_to_read);
when used on Right side its extracting part of string and when used on left side it can used for
replacing the part of string in original string
Example:
SUBSTR function used on left side,
data temp;
sample_str = "Pin Code 411014";
SS = SUBSTR(sample_str,4,5);
run;
Pavani K, SAS trainer
sastrainer@onlinesastraining.com
OUTPUT:
Obs sample_str SS
1 Pin Code 411014 Code
Explanation:
SUBSTR() has returned the part of string; starting at 5th position and counting 4 characters then
onwards.
data temp;
sample_str = "Pin Code 411014";
SUBSTR(sample_str, 4, 5) = ":";
run;
OUTPUT:
Obs sample_str
1 Pin: 411014
Explanation:
SUBSTR() has replaced the part of string; starting at 4th position and counting 5 characters with
given character ‘:’ on the right side.
(E) use the concatenation operator | | to join two or more character strings
Example:
A This
B is
C SAS
(A) LEFT - To left-align text values, LEFT doesn't "remove" the leading
blanks; it moves them to the end of the string.Thus, it doesn't
change the storage length of the variable
Syntax: LEFT(character-value)
Example:
STRING FUNCTION OUTPUT
‘ SAS’ LEFT(String) ‘SAS ’
‘ 1234 ’ LEFT(String) ‘1234 ’
(B) RIGHT - To right-align a text string, the RIGHT function will move the
characters to the end of the string and add the blanks to the
beginning so that the final length of the variable remains the
same
Syntax: RIGHT(character-value)
Example
STRING FUNCTION OUTPUT
‘ SAS’ RIGHT(String) ‘ SAS’
‘ 1234 ’ RIGHT(String) ‘ 1234’
(C) STRIP - The STRIP function removes both leading and trailing blanks
Syntax: STRIP(character-value)
Example:
STRING FUNCTION OUTPUT
‘ SAS’ STRIP(String) ‘ SAS’
‘ 1234 ’ STRIP(String) ‘ 1234’
Pavani K, SAS trainer
sastrainer@onlinesastraining.com
Example
Example
(B) COUNTW - To count the number of times a given word appears in a string.
Pavani K, SAS trainer
sastrainer@onlinesastraining.com
Example