Sunteți pe pagina 1din 8

Pavani K, SAS trainer

sastrainer@onlinesastraining.com

Day 13,14: CHARACTER FUNCTIONS

(1) Functions that Change the Case of Characters

(A)UPCASE - use the UPCASE function to change lowercase


letters to uppercase letters

(B)LOWCASE - use the LOWCASE function to change uppercase


letters to lowercase letters

(C)PROPCASE - use the PROPCASE function to capitalize the first


letter of each word
Example:

STRING FUNCTION OUTPUT


‘This is SAS’ UPCASE(String) ‘THIS IS SAS’
‘This is SAS’ LOWCASE(String) ‘this is sas’
‘This is SAS’ PROPCASE(String) ‘This Is Sas’

(2) Functions that Remove Characters from Strings

(A) COMPBL - use the COMPBL function to convert multiple blanks


in a character string to a single blank
Example:

STRING FUNCTION OUTPUT


‘This is SAS’ COMPBL(String) ‘‘This is SAS’’

(B) COMPRESS - use the COMPRESS function to remove


blanks/specified characters from a string
Example:

STRING FUNCTION OUTPUT


‘This is SAS’ COMPRESS(String) ‘ThisisSAS’
‘This (is) SAS’ COMPRESS(String, ‘()’) ‘This is SAS’
‘This 12is 456SAS76’ COMPRESS(String, ‘0123456789’) ‘This is SAS’

COMPRESS Function with the third Argument:


This Function Compresses/removes blanks or specified characters from a given string.
It also has an additional Modifier that could be passed to give more instructions.
Syntax: New_variable = COMPRESS(string, <compress char>, <modifier>);
Modifiers that can be used are:
K = Keep
I = Ignore case
Pavani K, SAS trainer
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’

(3) Functions That Search for Characters

(A) INDEX and INDEXC - functions return the position where the char
or string of characters first occur in the string.

(B) INDEXW- function searches for entire word

Syntax: INDEX(String, Search String);


NDEXC(String, Search String);
INDEXW(String, Search Word);
Example:
STRING FUNCTION OUTPUT
'this is where her bag was’ INDEX (string,'her') 10
'this is where her bag was’ INDEXC(string,'her') 2
'this is where her bag was’ INDEXW(string,'her') 15

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

(C) FIND, FINDC FUNCTIONS:


FIND and FINDC functions return the position where the char or string of
characters first occur in the string.
Modifiers:
I = Ignore Case
T = Ignore Trailing Blanks
V = Search of any other string than required
Startpos: Indicates the Position from where to start the search.
Examples:
STRING FUNCTION OUTPUT
'this is where her bag was’ FIND(String,‘HER,’i’) 10
'this is where her bag was’ FIND(String,'HER‘,'i',11) 15
'this is where her bag was’ FINDC(String,‘HER,’i’) 2

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);

But more interestingly it has got another important use as well.

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

proc print data = temp;


run;

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.

SUBSTR function used on right side,

data temp;
sample_str = "Pin Code 411014";
SUBSTR(sample_str, 4, 5) = ":";
run;

proc print data = temp;


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.

(5) Functions That Join Two or More Strings Together


(A) CAT - This function simply concatenates strings.
(B) CATS - This function concatenates strings by removing leading
and trailing blanks.
(C) CATT - This function concatenates strings by removing railing
blanks.
(D) CATX - This function concatenates strings and puts a Separator in
between after removing leading and trailing blanks.
STRING1, STRING2, STRING3 FUNCTION OUTPUT
‘This ’, ‘ is’ , ‘ SAS’ CAT(String1,String2,String3) This is SAS
‘This ’, ‘ is’ , ‘ SAS’ CATS(String1,String2,String3) ThisisSAS
‘This ’, ‘ is’ , ‘ SAS’ CATT(String1,String2,String3) This is SAS
‘This ’, ‘ is’ , ‘ SAS’ CATX(‘-’String1,String2,String3) This-is-SAS
Pavani K, SAS trainer
sastrainer@onlinesastraining.com

(E) use the concatenation operator | | to join two or more character strings

Example:

A This
B is
C SAS

Output: A| | B | | C This is SAS


strip(A) | | strip(B) | | strip(C); ThisisSAS
trim(A)| | trim(B)| | trim(C); This is SAS
strip(A)| |'@'| |strip(B)| |'@'| |strip(C); This@is@SAS
strip(A)| | '(' | |strip(B)| | ')'| |strip(C); This(is)SAS

(6) Functions That Remove Blanks from Strings

(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

(D) TRIM - The TRIM function removes trailing blanks


Syntax: TRIM(character-value)
Example:
STRING FUNCTION OUTPUT
‘ SAS ’ TRIM(String) ‘ SAS’
‘ 1234 ’ TRIM(String) ‘ 1234’

(7) Functions That Divide Strings into "Words"


(A)SCAN - use the SCAN function to parse a string and/or extract part of a
string
Syntax SCAN(character-value, n-word, <'delimiter-list'>)

“n-word” is the nth word in the string.


If “n-word” is greater than the number of words, the SCAN function returns a value that contains
no characters.
If n is negative, the character value is scanned from right to left.
Delimiter-list is an optional argument. If it is omitted, the default set of delimiters are (for ASCII
environments): blank . < ( + & ! $ * ) ; ^ - / , % |
For EBCDIC environments, the default delimiters are: blank . < ( + | & ! $ * ) ; ¬ - / , % | ¢
Example:

STRING FUNCTION OUTPUT


‘This is SAS’ SCAN(STRING,2) ‘is’
‘This is SAS’ SCAN(STRING,-1) ‘SAS’
‘This is SAS’ SCAN(STRING,3) ‘SAS’
‘This is SAS’ SCAN(STRING,4) No characters
‘This is SAS’ SCAN(STRING,1) ‘This’
‘ONE?TWO THREE+FOUR|FIVE’ SCAN(STRING,2,‘ ’) "THREE+FOUR|FIVE"
‘ONE?TWO THREE+FOUR|FIVE’ SCAN(STRING,4) "FIVE"

(8) Functions That Compute the Length of Strings


(A) LENGTH - To determine the length of a character value, not counting
trailing blanks. A null argument returns a value of 1.
Syntax: LENGTH(character-value)
Pavani K, SAS trainer
sastrainer@onlinesastraining.com

Example

STRING FUNCTION OUTPUT


‘This is SAS ’ LENGTH(STRING) 11
‘ ’ LENGTH(STRING) 1

(B) LENGTHC - To determine the length of a character value, counting trailing


blanks. A null argument returns a value of 1.
Syntax: LENGTHC(character-value)

STRING FUNCTION OUTPUT


‘This is SAS ’ LENGTHC(STRING) 16
‘ ’ LENGTHC(STRING) 1

(9) Functions That Count the Number of Letters or Substrings in a String


(A) COUNT - To count the number of times a given substring appears in a
string.

With the use of a modifier, case can be ignored.


If no occurrences of the substring are found, the function returns a 0.

Syntax: COUNT(character-value, find-string, <'modifiers'>)

character-value -is any SAS character expression.


Find-string -is a character variable or SAS string to be counted.

The following modifiers, placed in single or double quotation marks,


i or I ignore case.
t or T ignore trailing blanks in both the character value and the find-string.

Example

STRING FUNCTION OUTPUT


‘How Now Brown COW’ COUNT(STRING, ’ow’) 3
‘How Now Brown COW’ COUNT(STRING, ’ow’, ‘i’) 4
‘How Now Brown COW’ COUNT(STRING, ’xx’) 0
‘ding and dong’ COUNT(STRING, ‘g ’) 1
‘ding and dong’ COUNT(STRING,‘g ’,‘T’) 2

(B) COUNTW - To count the number of times a given word appears in a string.
Pavani K, SAS trainer
sastrainer@onlinesastraining.com

(10) Functions That Substitute Letters or Words in Strings


(A) TRANWRD- To substitute one or more words in a string with a replacement
word or words. It works like the find and replace feature
Syntax TRANWRD(character-value, from-string, to-string)

character-value-is any SAS character expression.


From-string -is one or more characters that you want to replace with the character or characters in
the to-string.
To-string -is one or more characters that replace the entire from string.

Example

STRING FUNCTION OUTPUT


‘123 Elm Road’ TRANWRD(STRING,’Road’,’Rd’) ’123 Elm Rd’
‘one two three’ TRANWRD(STRING,’four’,’4’) ’one two three’
‘ONE TWO THREE’ TRANWRD(STRING,‘ONE TWO ’,‘A B’) ‘ABTHREE’

S-ar putea să vă placă și