Monday, April 8, 2013

SAS Character functions(Part 2)


 SAS Character functions(Part 2)
A quick reference
Objective : This article aims at creating a one stop solution for all SAS character functions with explanation and usage examples. This is the Second of the two posts on SAS Character functions. The first post can be found here:
SAS Character function Part 1.
Other posts on functions can be found below:
SAS Date Time Functions(Part 1)

Theory : SAS functions are one of the most useful constructs designed to make our life simple and easy. Here I will try to cover all frequently used SAS character functions with examples. You can also define your own functions and subroutines but that is a separate topic in itself and I will discuss it in a separate post.
Below is the alphabetic list of character functions I have used with a simple examples and usage notes. You can just scroll through to find the function you are searching for.




Function

Usage

Syntax

Function Example

Returns

FIRST  To extract first character from a string FIRST(string) FIRST("Saurabh") S
INDEX To locate the starting position of a substring in a string. INDEX(String,Search string) INDEX("being human","human") 7
INDEXC To search a character string for one or more characters INDEXC(String,character list) INDEX("being human","abc") 1
LEFT    Left aligns character values by moving leading blanks to the end of string LEFT(string) LEFT("   Hello") Hello
LENGTH  Returns the length of a string, not counting trailing balnks LENGTH(String) LENGTH("ABC    ") 3
LENGTHC  Returns the length of a string, counting trailing balnks LENGTHC(String) LENGTHC("ABC    ") 6
LENGTHM  Returns the length of a string, in memory LENGTH(String) LENGTHM(" ") 1
LOWCASE  Converts all characters to Lowercase LOWCASE(String) LOWCASE("HeLLO")  hello
MISSING  Used ti determine whether the argument's value is missing. Returns 0 for non-missing otherwise 1 MISSING(String) MISSING(" ") 1
NOTALNUM  Searches for the first occurrence of an Alphanumeric character in a string. ANYALNUM(String <,startpos>) Anyalnum("%$#sa1",2) 2
Second argument gives the position to start the search.
NOTALPHA  To determine the position of the first character in a string that is not an alphanumeric. NOTALPHA(String <,startpos>) notalpha("%$#sa1",1) 1
NOTDIGIT  To determine the position of the first character in a string that is not a numeric. NOTDIGIT(String <,startpos>) notdigit("%$#sa1",1) 1
NOTLOWER To determine the position of the first character in a string that is not a lowercase character. NOTLOWER(String <,startpos>) notlower("%$#sa1",1) 1
NOTUPPER  To determine the position of the first character in a string that is not an alphanumeric. NOTUPPER(String <,startpos>) Anypunct("%$#sa1",1) 1
PROPCASE  Capitalizes first letter of each word in a string PROPCASE(String) Propcase("indira gandhi") Indira Gandhi
RANK  To give ranks or obtain relative position of ASCII characters RANK(char) RANK("A") 65
REPEAT  Repeats a given string. Result is the original string plus repapeated ones REPEAT(String,number of times) REPEAT("Hi",3) HiHiHiHi
REVERSE  Reverses a given string REVERSE(String) REVERSE("HELLO") OLLEH
RIGHT    Right aligns a string by putting trailing blanks at beginning RIGHT(String) RIGHT("ABC    ")    "    ABC"
SCAN  Returns the nth word from a string SCAN(String,number) SCAN("how are you",3) you
STRIP Removes leading as well as trailing blanks STRIP(string) STRIP("  ABC  ") "ABC"
SUBSTR  Extracts a part of a string SUBSTR(String,startpos<,length>) SUBSTR("mohit",2,3) ohi
TRANSLATE  Changes one character value to another. Lists can be passed and the character will be replaced by their corresponding characters TRANSLATE(String,to_list,from_ list) TRANSLATE("mr india","xy","mi") xr yndya
TRANWRD  Replaces one word by other TRANWRD(String, find , replace) TRANWRD("how are you","how","who") who are you
TRIM  Removes trailing blanks Trim(string) TRIM("ABC   ") "ABC"
UPCASE  Converts all characters to Upppercase UPCASE(String) UPCASE("Hello")  "HELLO"



Conclusion : This and the previous post is not an exhaustive list of all SAS Character functions available but it covers almost all functions you’ll use in day to day job
And a alphabetical list like this can act as your quick reference or you can even print and stick it on your desk for super quick reference.
I have tried my best to eliminate erros but if any please write it as a comment. Suggestion are always welcome.

Will be back with some more topics. Till then goodbye.

Saurabh Singh Chauhan
(er.chauhansaurabh@gmail.com)

Note : If you need an article on a specific topic please post as comment.

Disclaimer :
SAS® and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc.in the USA and other countries. ® Indicates USA registration.
Other brand and product names are registered trademarks or trademarks of their respective companies. 
The contents of this post are the works of the author(s)and do not necessarily represent the opinions,recommendations, or practices of any organization whatsoever.

No comments:

Post a Comment