Wednesday, January 25, 2012

SAS Character functions(Part 1):A quick reference

 SAS Character functions(Part 1)
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 first of the two posts on SAS Character functions.

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.

All the SAS function posts can be found here:
SAS Character function Part 1
SAS Character function Part 2  
SAS Date and Time functions Part 1
SAS Date and Time functions Part 2


Function Usage Syntax Function Example Returns
ANYALNUM Searches for the first occurrence of an Alphanumeric character in a string.
Second argument gives the position to start the search.
ANYALNUM(String <,startpos>) Anyalnum("%$#sa1",2) 2
ANYALPHA Searches for the first occurrence of an Alphabet character in a string.
Second argument gives the position to start the search.
ANYALPHA(String <,startpos>) Anyalpha("%$#sa1",1) 4
ANYDIGIT Searches for the first occurrence of an Alphabet character in a string.
Second argument gives the position to start the search.
ANYDIGIT(String <,startpos>) Anydigit("%$#sa1",1) 6
ANYLOWER Searches for the first occurrence of an Alphabet character in a string.
Second argument gives the position to start the search.
ANYLOWER(String <,startpos>) Anylower("%$#sa1",1) 4
ANYPUNCT Searches for the first occurrence of a punctuation (! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~) character in a string.
Second argument gives the position to start the search.
ANYPUNCT(String <,startpos>) Anypunct("%$#sa1",1) 1
ANYSPACE Searches for the first occurrence of an space(whitespace,tab etc.) in a string.
Second argument gives the position to start the search.
ANYSPACE(String <,startpos>) Anyspace("%$ #sa1",1) 3
ANYUPPER Searches for the first occurrence of a uppercase character in a string.
Second argument gives the position to start the search.
ANYUPPER(String <,startpos>) Anyupper("%$#sa1",1) 0
CAT Concatenates two or more character strings. CAT(str1,str2<,str3>...) CAT("ABC ","B CD","  DEF") ABC B CD  DEF
CATS Concatenates two or more character strings and removes leading and trailing blanks. CATS(str1,str2<,str3>...) CATS("ABC ","B CD","  DEF") ABCB CDDEF
CATT  Concatenates two or more character strings and removes only trailing blanks blanks. CATT(str1,str2<,str3>...) CATT("ABC ","B CD","  DEF") ABCB CD  DEF
CATX  Concatenates two or more character strings. Removes leading and trailing blanks and inserts a charatcter between  strings CATX(sepaerator,str1,str2<,str3>...) CATX("-","ABC ","B CD","  DEF") ABC-B CD-DEF
CHAR  Extracts a character froma string from the position given CHAR(string,position) CHAR("HELLO",4) L
CHOOSEC  Select n numbered string from the list of strings. Based on the location passed as an argument. CHOOSEC(n,str1,str2,str3) CHOOSEC(2,"vineet","Naveen","Vybhav") Naveen
CHOOSEN  Select nth number from the list of numbers based on the location passed as an argument. CHOOSEN(n,str1,str2,str3) CHOOSEN(2,2,7,6) 7
COALESCEC  Returns first non-missing value from a list of character values COALESCEC(char1,char2,char3) COALESCEC(" ","B","c" ) B
COMPBL  Compresses two or more spaces into one. COMPBL(string) COMPBL("leo frank      murthy") leo frank murthy
COMPRESS  Compresses the characters specified as second arguments from the string passed as first argument COMPRESS(char_valu,'compress list') COMPRESS("anandsingh",'as') nndingh
COUNT  To count the number of times a given substring appears in a string COUNT(string,search string) COUNT("Well fell bell","ell") 3
COUNTC  To count the number of individual characters in a string COUNTC(string,search char list) COUNTC("Well fell bell","ell") 9
COUNTW  To count number of words in a character value COUNTW(string<,delimiters>) COUNTW("Well,fell bell",",") 2
FIND To locate a substring within a string. We can provide start position, direction of search and can ignore case FIND(character-value, find-string <,'modifiers'><,start>) FIND("hi hello hi", "hello") 4
FINDC To locate a character within a string. We can provide start position, direction of search and can ignore case FINDC(character-value, find-characters <,'modifiers'><,start>) FINDC("hi hello hi", "e") 5
FINDW To locate a word within a string. We can provide delimiter, start position, direction of search and can ignore case FINDW(character-value, find-word<,delim> <,'modifiers'><,start>) FINDW("hi hello hi", "hello") 4


Conclusion : This and the next 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.

4 comments:

  1. I have a variable that records both first and last names. How do I separate them into two variable, one for first and one for last?

    ReplyDelete
    Replies
    1. use scan function.

      First_Word = scan(string, 1);
      Last_Word = scan(string, -1);

      Delete
  2. I assume stratpos is really startpos throughout?

    ReplyDelete
  3. Yes it is...its a typo...will correct it. Thanks for bringing this up

    ReplyDelete