Some Character Functions in SAS
Prior to version 7, the default length or the maximum length was
200. In the later versions,
the maximum length of a character variables was
increased to 32767.
|
||||||||||
1. Upcase(argument)
|
||||||||||
changes the argument to upper case.
|
||||||||||
It has no effects on the special characters.
|
||||||||||
example: CHAR = "ABCxyz"
|
||||||||||
UPCASE(CHAR)
returns
"ABCXYZ"
|
||||||||||
UPCASE("a1%m?")
returns "A1%M?"
|
||||||||||
If the length of the resulting variable is not previously
defined then the length will be
that of the argument.
|
||||||||||
2. Lowcase(argument)
|
||||||||||
changes the argument to lower case.
|
||||||||||
It also has no effects on the special characters.
|
||||||||||
If the length of the resulting variable is not previously
defined then the length will be
that of the argument.
|
||||||||||
CHAR = "ABCxyz"
|
||||||||||
LOWCASE(CHAR)
Returns "abcxyz"
|
||||||||||
LOWCASE("A1%M?")
returns "a1%m?"
|
||||||||||
3. Propcase(argument)
|
||||||||||
changes the first letter of the argument to upper case and the
remaining letters to lower
case
|
||||||||||
It also has no effect on the special character.
|
||||||||||
If the length of the resulting variable is not previously
defined then the length will be
that of the argument.
|
||||||||||
CHAR = "ABCxyz"
|
||||||||||
PROPCASE(CHAR) ------------- "Abcxyz"
|
||||||||||
PROPCASE("a1%m?") ---------- "A1%m?"
|
||||||||||
PROPCASE("mr. george w. bush") ---------------
"Mr. George W. Bush"
|
||||||||||
Notice here that prop case function works on every word of the
given argument
|
||||||||||
4. Compress(argument, <"compress-list">)
|
||||||||||
Compress function removes the blanks from the argument if no
"compress-list" is
provided.
|
||||||||||
If "compress-list is provided, then it removes those
characters in the list from the
argument.
|
||||||||||
If the length of the resulting variable is not previously
defined then the length will be
that of the argument.
|
||||||||||
5. Compbl(argument)
|
||||||||||
Compbl function converts multiple blanks into a single blank in
the argument. The
blanks can be trailing blank, leading blanks or
intermediate blanks
|
||||||||||
This function only works on blanks.
|
||||||||||
CHAR = "A C123XYZ"
|
||||||||||
COMPRESS("A C XYZ") -------------- "ACXYZ"
|
||||||||||
COMPRESS("(908) 777-1234"," (-)")
------------------- "9087771234"
|
||||||||||
COMPRESS(CHAR,"0123456789") --------------- "A
CXYZ"
|
||||||||||
If the length of the resulting variable is not previously
defined then the length will be
that of the argument.
|
||||||||||
6. ANYALNUM(argument <,start>)
|
||||||||||
It is used to locate the first occurrence of any alpha numeric
characters and returns the
position. If start is omitted then the function
searches from left to right.
|
||||||||||
It returns 0 if no alphanumeric character is found
|
||||||||||
If the start value is positive then it searches from left to
right, if it is negative then it
will start searching from right.
|
||||||||||
If the start position is negative and greater than the length of
the argument, then the
function will start from the left most position. If
the start position is positive and
greater than the
|
||||||||||
length of the argument
then it will result in 0
|
||||||||||
STRING = "ABC 123 ?xyz_n_"
|
||||||||||
ANYALNUM(STRING) ----------------------- 1 (the position of
"A")
|
||||||||||
ANYALNUM("??$$%%") ---------------------- 0
(no
alpha-numeric characters)
|
||||||||||
ANYALNUM(STRING,5) ----------------------- 5 (the position of
"1")
|
||||||||||
ANYALNUM(STRING,-4) ---------------------- 3 (the position of
"C")
|
||||||||||
ANYALNUM(STRING,6) ----------------------- 6 (the position of
"2")
|
||||||||||
7. ANYALPHA(argument <,start>)
|
||||||||||
It is used to locate the first occurrence of any alpha character
in the argument. Returns 0
if not found.
|
||||||||||
start works the same way as above.
|
||||||||||
STRING = "ABC 123 ?xyz_n_"
|
||||||||||
ANYALPHA(STRING) ----------------- 1 (position of "A")
|
||||||||||
ANYALPHA("??$$%%") -------------- 0 (no alpha
characters)
|
||||||||||
ANYALPHA(STRING,5) ----------------10 (position of
"x")
|
||||||||||
ANYALPHA(STRING,-4) ----------------3 (position of
"C")
|
||||||||||
ANYALPHA(STRING,6) -----------------10 (position of
"x")
|
||||||||||
8. ANYDIGIT(argument <,start>)
|
||||||||||
It is used to find out the first occurrence (position) of any
digit within the argument. If not
found then it returns 0
|
||||||||||
start works the same way as above.
|
||||||||||
STRING = "ABC 123 ?xyz_n_"
|
||||||||||
ANYDIGIT(STRING) --------------- 5 (position of "1")
|
||||||||||
ANYDIGIT("??$$%%") ------------- 0 (no digits)
|
||||||||||
ANYDIGIT(STRING,5) --------------- 5 (position of "1")
|
||||||||||
ANYDIGIT(STRING,-4) -------------- 0 (no digits from position 4
to 1)
|
||||||||||
ANYDIGIT(STRING,6) --------------- 6 (position of "2")
|
||||||||||
9. ANYPUNCT(argument <,start>)
|
||||||||||
It is used to find the first occurrence of the punctuation
character within the argument.
Returns 0 if not found.
|
||||||||||
Start works the same way as above.
|
||||||||||
In the ASCII character set, the following are considered as
punctuation:
|
||||||||||
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
|
||||||||||
STRING = "A!C 123 ?xyz_n_"
|
||||||||||
ANYPUNCT(STRING) --------------- 2 (position of "!")
|
||||||||||
ANYPUNCT("??$$%%") ------------- 1 (position of
"?")
|
||||||||||
ANYPUNCT(STRING,5) ------------- 9 (position of "?")
|
||||||||||
ANYPUNCT(STRING,-4) -------------- 2
(starts at position 4 and
goes left, position of "!" )
|
||||||||||
ANYPUNCT(STRING,-3) -------------- 2
(starts at "C" and
goes left, position of "!")
|
||||||||||
10. ANYSPACE(argument <,start>)
|
||||||||||
It is used to locate the first occurrence of the white space
character within the argument
(a blank, horizontal or vertical tab, carriage
return, linefeed, formfeed). Returns 0 if not
found.
|
||||||||||
Start works the same way as above.
|
||||||||||
STRING = "ABC 123 ?xyz_n_"
|
||||||||||
ANYSPACE(STRING) ---------------- 4 (position of the first
blank)
|
||||||||||
ANYSPACE("??$$%%") ------------- 0 (no spaces)
|
||||||||||
ANYSPACE(STRING,5) ------------ 8 (position of the second blank)
|
||||||||||
ANYSPACE(STRING,-4) ------------- 4 (position of the first
blank)
|
||||||||||
ANYSPACE(STRING,6) --------------- 8 (position of the second
blank)
|
||||||||||
DATA ANYWHERE;
|
||||||||||
INPUT STRING $CHAR20.;
|
||||||||||
ALPHA_NUM =
ANYALNUM(STRING);
|
||||||||||
ALPHA_NUM_9 =
ANYALNUM(STRING,-999);
|
||||||||||
ALPHA = ANYALPHA(STRING);
|
||||||||||
ALPHA_5 =
ANYALPHA(STRING,-5);
|
||||||||||
DIGIT = ANYDIGIT(STRING);
|
||||||||||
DIGIT_9 =
ANYDIGIT(STRING,-999);
|
||||||||||
PUNCT = ANYPUNCT(STRING);
|
||||||||||
SPACE = ANYSPACE(STRING);
|
||||||||||
DATALINES;
|
||||||||||
Once upon a time 123
|
||||||||||
HELP!
|
||||||||||
9.88E+08
|
||||||||||
;
|
||||||||||
PROC PRINT DATA=ANYWHERE NOOBS HEADING=H;
|
||||||||||
TITLE "Listing of
Data Set ANYWHERE";
|
||||||||||
RUN;
|
||||||||||
Note that there are other functions which works exactly opposite
to these functions.
They are:
|
||||||||||
1. NOTALNUM(argument <,start>)
|
||||||||||
2. NOTALPHA(argument <,start>)
|
||||||||||
3. NOTDIGIT(argument <,start>)
|
||||||||||
4.NOTUPPER(argument <,start>)
|
||||||||||
example for notupper is as follows:
|
||||||||||
STRING = "ABC 123 ?xyz_n_"
|
||||||||||
NOTUPPER("ABCDabcd") ----------- 5 (position of
"a")
|
||||||||||
NOTUPPER("ABCDEFG") ------------- 0 (all uppercase
characters)
|
||||||||||
NOTUPPER(STRING) ------------------ 4 (position of 1st blank)
|
||||||||||
NOTUPPER("??$$%%") -------------- 1 (position of
"?")
|
||||||||||
NOTUPPER(STRING,5) -------------- 5 (position of "1")
|
||||||||||
NOTUPPER(STRING,-6) -------------- 6 (position of "2")
|
||||||||||
NOTUPPER(STRING,6) ---------------- 6 (position of
"2")
|
||||||||||
11. FIND(argument1, argument2 <,"modifiers",
start>)
|
||||||||||
To locate the subtsring (argument2) within the string
(argument1). It returns the first
position of the substring if found and
returns 0 if not found.
|
||||||||||
modifiers can take the following values within the quotes. It
can be given in upper case opt
lower case.
|
||||||||||
1. I for ignore case
|
||||||||||
2. t for ignoring the
trailing blanks in both argument1 and argument2
|
||||||||||
start tells sas where to start searching. If it is positive
number then sas starts from left to
right and at the exact position that you
have given. If the positive number is greater than
the length of the
|
||||||||||
argument1 then it
returns 0. If it is negative number then sas starts from right to left and
at
the exact position you have provided. If the negative number is greater than
the length
of the argument1
|
||||||||||
then sas will start from
the right most position.
|
||||||||||
The length of the result will be equal to the length of the
argument1.
|
||||||||||
STRING1 = "Hello hello goodbye" and STRING2 = hello
|
||||||||||
FIND(STRING1, STRING2) ----------- 7
|
||||||||||
FIND(STRING1, STRING2, 'I') ----------- 1
|
||||||||||
FIND(STRING1,"bye") ----------------- 17
|
||||||||||
FIND("abcxyzabc","abc",4) ------------- 7
|
||||||||||
FIND(STRING1, STRING2, "i", -99) ------------ 7
|
||||||||||
12. FINDC(argument1, argument2 <,"modifiers",
start>)
|
||||||||||
It is used to locate a character in argument2 which appears or
does not appears in the
argument1.
|
||||||||||
Modifiers and start works the same way as above.
|
||||||||||
Modifiers can also take the value v which tells sas to search
for the character in argument2
that does not appear in argument1
|
||||||||||
STRING1 = "Apples and Books" and STRING2 =
"abcde"
|
||||||||||
FINDC(STRING1, STRING2) ------------ 5
|
||||||||||
FINDC(STRING1, STRING2, 'i') ---------------- 1
|
||||||||||
FINDC(STRING1,"aple",'vi') -------------------- 6
|
||||||||||
FINDC("abcxyzabc","abc",4) -----------------
7
|
||||||||||
13. INDEX(argument1, arguement2)
|
||||||||||
Index is similar to find function but has less capability such
as it can only look from left
to
right and start from left most position of
the argument1, cannot ignore the cases.
|
||||||||||
Index function takes the substring as argument2 and searches
within the argument1. It
has
the capability to search for the partial word.
Returns the position first occurrence of the
partial word within
|
||||||||||
the argument1.
returns 0 if not found.
|
||||||||||
STRING = "ABCDEFG"
|
||||||||||
INDEX(STRING,'C') ---------------------- 3 (the position of the
'C')
|
||||||||||
INDEX(STRING,'DEF') ------------------ 4 (the position of the
'D')
|
||||||||||
INDEX(STRING,'X') -------------------- 0 (no "X" in
the string)
|
||||||||||
INDEX(STRING,'ACE') -------------------- 0 (no "ACE"
in the string)
|
||||||||||
14. INDEXW(argument1, argument2)
|
||||||||||
Indexw is similar to index but searches for the whole word
within the argument1. It
considers argument2 as a complete word within the
argument1 and then searches. So if
the
argument2 is a
|
||||||||||
partial word in the
argument1 then it returns 0
|
||||||||||
STRING1 = "there is a the here" and STRING2 =
"end in the."
|
||||||||||
INDEXW(STRING1,"the") ----------------- 12 (the word
"the")
|
||||||||||
INDEXW("ABABAB","AB") ---------------- 0
(no
word boundaries around "AB")
|
||||||||||
INDEXW(STRING1,"er") ----------------- 0 (not a word)
|
||||||||||
INDEXC(STRING2,"the") ---------------------- 0
(punctuation is not a word boundary)
|
||||||||||
data a;
|
||||||||||
STRING1 = "there is a the here";
|
||||||||||
STRING2 = "end in the.";
|
||||||||||
string3= "there is a the here end in the.";
|
||||||||||
a=indexw(string1, string2);
|
||||||||||
b=indexw(string3, string2);
|
||||||||||
run;
|
||||||||||
proc print;
|
||||||||||
run;
|
||||||||||
a will be 0 and b will be 21
|
||||||||||
15. Indexc(argument1, argument2)
|
||||||||||
This function takes multiple characters for argument2 and
searches for each individual
characters in argument1. It returns the location
of the character that occurs first in the
argument1.
|
||||||||||
You can club multiple characters into the argument2 or give each
character as an
argument.
|
||||||||||
STRING = "ABCDEFG"
|
||||||||||
INDEXC(STRING,'F','C','G') ------------------ 3 (position of the
"C")
|
||||||||||
INDEXC(STRING, 'FCG') ------------------- 3 (position of the
"C")
|
||||||||||
INDEXC(STRING,'FCG') --------------------- 3 (position of the
"C")
|
||||||||||
INDEXC(STRING,'X','Y','Z') ------------------- 0
(no
"X", "Y", or "Z" in STRING)
|
||||||||||
16. VERIFY(argument1, argument2)
|
||||||||||
This function returns the first occurance of the character that
is not present in the
argument2
and present in the argument1. Returns 0 if
the argument1 does not contain any
characters
other than
|
||||||||||
those present in the
argument2
|
||||||||||
STRING = "ABCXABD" and V = "ABCDE"
|
||||||||||
VERIFY(STRING,V) ------------------ 4 ("X" is not in
the verify string)
|
||||||||||
VERIFY(STRING,"ABCDEXYZ") ---------------- 0
(no
"bad" characters in STRING)
|
||||||||||
VERIFY(STRING,"ACD") --------------------- 2 (position
of the "B")
|
||||||||||
VERIFY("ABC ","ABC") -----------------------
4
(position of the 1st blank)
|
||||||||||
VERIFY(TRIM("ABC "),"ABC") --------------- 0
(no invalid characters)
|
||||||||||
17. Left(argument)
|
||||||||||
If the argument has the leading blanks, then this function
brings the characters to the left
and the leading blanks become the trailing
blanks
|
||||||||||
18. RIGHT(argument)
|
||||||||||
This works opposite to the left function
|
||||||||||
19. Trim(argument)
|
||||||||||
If the argument contains the trailing blanks then this function
removes the trailing blanks.
|
||||||||||
20. STRIP(argumemnt)
|
||||||||||
this function works as shown below:
|
||||||||||
TRIM(LEFT(argument))
|
||||||||||
21. SUBSTR(argument, start <,length>)
|
||||||||||
Substr function returns the partial word from the argument. It
reads the words or characters
from left to right from the start position. If
the length is not given then it starts at the start
position
|
||||||||||
and reads till the
end. If you have given the length then sas starts at the start position and
reads the number of characters equal to the length
|
||||||||||
The length of the result will be equal to that of the argument
|
||||||||||
STRING = "ABC123XYZ"
|
||||||||||
SUBSTR(STRING,4,2) ----------------- "12"
|
||||||||||
SUBSTR(STRING,4) -------------------- "123XYZ"
|
||||||||||
SUBSTR(STRING,LENGTH(STRING)) -------------- "Z"
(last character in the
string)
|
||||||||||
There is another interesting use of the substr function i.e.
using the substr function on the
left hand side of the equal sign. The syntax
is as follows:
|
||||||||||
SUBSTR(argument, start
<,length>)="character-value"
|
||||||||||
Character-value is any sas expression
|
||||||||||
start is the starting position where you want to place the new
characters.
|
||||||||||
Length specifies how many characters to be replaced.
|
||||||||||
If you don’t mention the length then sas will replace all the
characters starting from start
position with the character-value
|
||||||||||
EXISTING = "ABCDEFGH", NEW = "XY"
|
||||||||||
SUBSTR(EXISTING,3,2) = NEW --------------
EXISTING is now =
"ABXYEFGH"
|
||||||||||
SUBSTR(EXISTING,3,1) = "*" --------------------
EXISTING is now = "AB*DEFGH"
|
||||||||||
data a;
|
||||||||||
EXISTING = "ABCDEFGH" ;
|
||||||||||
NEW = "XY";
|
||||||||||
a=existing;
|
||||||||||
b=existing;
|
||||||||||
substr(EXISTING,3,2) =new;
|
||||||||||
substr(a, 3,3)=new;
|
||||||||||
substr(b,3,1)=new;
|
||||||||||
run;
|
||||||||||
proc print;
|
||||||||||
run;
|
||||||||||
existing will be "ABXYEFGH", a will be "ABXY
FGH",
b will be "ABXDEFGH"
|
||||||||||
22. CAT(argument1,….., argumentN)
|
||||||||||
This function combines the arguments into one without any spaces
in between
(i.e. delimiter). If you want delimiters then you have to specify
them in the arguments.
|
||||||||||
A = "Bilbo" (no blanks)
|
||||||||||
B = " Frodo" (leading blanks)
|
||||||||||
C = "Hobbit " (trailing blanks)
|
||||||||||
D = " Gandalf " (leading and trailing blanks)
|
||||||||||
C1-C5 are five character variables, with the values of 'A', 'B',
'C', 'D',
and 'E' respectively
|
||||||||||
CAT(A, B) ------------------ "Bilbo Frodo"
|
||||||||||
CAT(B, C, D) -------------------- " FrodoHobbit Gandalf
"
|
||||||||||
CAT("Hello", D) ---------------------- "Hello
Gandalf "
|
||||||||||
CAT(OF C1-C5) ------------------ "ABCDE"
|
||||||||||
23. CATS(argument1,….., argumentN)
|
||||||||||
This function is also used to concatenate the arguments into
one. The major difference
is that this function strips the arguments and then
concatenates. That means if any
argument is having leading or
|
||||||||||
trailing blanks then
they will be stripped and the resulting string will not be having
any spaces
in between.
|
||||||||||
A = "Bilbo" (no blanks)
|
||||||||||
B = " Frodo" (leading blanks)
|
||||||||||
C = "Hobbit " (trailing blanks)
|
||||||||||
D = " Gandalf " (leading and trailing blanks)
|
||||||||||
C1-C5 are five character variables, with the values of 'A', 'B',
'C', 'D',
and 'E' respectively.
|
||||||||||
CATS(A, B) ---------------- "BilboFrodo"
|
||||||||||
CATS(B, C, D) ------------ "FrodoHobbitGandalf"
|
||||||||||
CATS("Hello", D) -------------
"HelloGandalf"
|
||||||||||
CATS(OF C1-C5) -------------- "ABCDE"
|
||||||||||
24. CATT(argument1,….., argumentN)
|
||||||||||
This function is similar to CATS but it strips only the trailing
blanks.
|
||||||||||
A = "Bilbo" (no blanks)
|
||||||||||
B = " Frodo" (leading blanks)
|
||||||||||
C = "Hobbit " (trailing blanks)
|
||||||||||
D = " Gandalf " (leading and trailing blanks)
|
||||||||||
C1-C5 are five character variables, with the values of 'A', 'B',
'C', 'D', and 'E'
respectively.
|
||||||||||
CATT(A, B) ----------------------- "Bilbo Frodo"
|
||||||||||
CATT(B, C, D) --------------------- " FrodoHobbit
Gandalf"
|
||||||||||
CATT("Hello", D) ------------------ "Hello
Gandalf"
|
||||||||||
CATT(OF C1-C5) ------------------ "ABCDE"
|
||||||||||
25. CATX(delimiter, argument1,……,argumentN)
|
||||||||||
This function also combines multiple arguments into one. But
this function takes the
first parameter as a delimiter. The first parameter
(delimiter) can be a variable or a
value in quotes.
|
||||||||||
A = "Bilbo" (no blanks)
|
||||||||||
B = " Frodo" (leading blanks)
|
||||||||||
C = "Hobbit " (trailing blanks)
|
||||||||||
D = " Gandalf " (leading and trailing blanks)
|
||||||||||
C1-C5 are five character variables, with the values of 'A', 'B',
'C', 'D',
and 'E' respectively.
|
||||||||||
CATX(" ", A, B) -------------------"Bilbo
Frodo"
|
||||||||||
CATX(":"B, C, D) -----------------
"Frodo:Hobbit:Gandalf"
|
||||||||||
CATX("***", "Hello", D) -------------
"Hello***Gandalf"
|
||||||||||
CATX("," ,OF C1-C5) -----------------
"A,B,C,D,E"
|
||||||||||
Note that the default result of the resultant varaible will be
200 in CAT, CATS, CATT,
and CATX functions.
|
||||||||||
26. TRIMN(argument)
|
||||||||||
The only difference between the TRIM and TRIMN function is that
when the
argument is having all blanks then TRIM will result in single blanks
and TRIMN will
result null with variable length 0
|
||||||||||
STRING1 = "ABC " and STRING2 = " XYZ"
|
||||||||||
TRIMN(STRING1) ----------------- "ABC"
|
||||||||||
TRIMN(STRING2) ---------------- " XYZ"
|
||||||||||
TRIMN("A B C ") ----------------- "A B C"
|
||||||||||
TRIMN("A ") || TRIM("B ")
----------------"AB"
|
||||||||||
TRIMN(" ") ------------------------- ""
(length = 0)
|
||||||||||
27. RANK(argument)
|
||||||||||
Here argument must be a character letter. The function returns
the ASCII value of that
character.
|
||||||||||
If the argument contains more than one characters then the
function returns the ASCII
value of the first character.
|
||||||||||
STRING1 = "A" and STRING2 = "XYZ"
|
||||||||||
RANK(STRING1) ------------- 65
|
||||||||||
RANK(STRING2) -------------- 88
|
||||||||||
RANK("X") ---------------------- 88
|
||||||||||
RANK("a") -------------- 97
|
||||||||||
28. REPEAT(argument1, argument2)
|
||||||||||
Here argument1 is the string and argument2 is the number of time
you want to repeat
the string.
|
||||||||||
This function repeats the string as many times as you have given
in the argument2
|
||||||||||
STRING = "ABC"
|
||||||||||
REPEAT(STRING,1) ---------------- "ABCABC"
|
||||||||||
REPEAT("HELLO ",3) --------------
"HELLO HELLO
HELLO HELLO"
|
||||||||||
REPEAT("*",5) ----------- "******"
|
||||||||||
29. REVERSE(argument)
|
||||||||||
This function produces the mirror image of the given string in
the argument i.e. it
reverses the argument.
|
||||||||||
STRING1 =
"ABCDE" and STRING2 = "XYZ "
|
||||||||||
REVERSE(STRING1) -------------- "EDCBA"
|
||||||||||
REVERSE(STRING2) --------------- " ZYX"
|
||||||||||
REVERSE("1234") -----------------------
"4321"
|
||||||||||
30. LENGTH(argument)
|
||||||||||
This function returns the number of characters including leading
and intermediate
blanks in the argument.
|
||||||||||
CHAR = "ABC "
|
||||||||||
LENGTH("ABC") --------------- 3
|
||||||||||
LENGTH(CHAR) ---------------- 3
|
||||||||||
LENGTH(" ") -------------------- 1
|
||||||||||
31. LENGTHC(argument)
|
||||||||||
This function returns the number of characters including the
leading, intermediate
and trailing blanks within the argument.
|
||||||||||
CHAR = "ABC "
|
||||||||||
LENGTH("ABC") ---------- 3
|
||||||||||
LENGTH(CHAR) ----------- 6
|
||||||||||
LENGTH(" ") --------------- 1
|
||||||||||
32. TRANSLATE(argument, replacement, to-be-replaced)
|
||||||||||
This function takes the multiple characters to
"to-be-replaced" and replaces these
characters in the argument to
corresponding replacement characters.
|
||||||||||
CHAR = "12X45", ANS = "Y"
|
||||||||||
TRANSLATE(CHAR,"ABCDE","12345")
------------------ "ABXDE"
|
||||||||||
TRANSLATE(CHAR,'A','1','B','2','C','3','D','4','E','5')
-----------------
"ABXDE"
|
||||||||||
TRANSLATE(ANS,"10","YN")
--------------------- "1"
|
||||||||||
33. Tranwrd(argument, to-be-replaced, replacement)
|
||||||||||
This function takes the whole word into
"to-be-replaced" and replaces this word with
corresponding
replacement word.
|
||||||||||
The length of the resultant variable will be 200
|
||||||||||
STRING = "123 Elm Road", FROM = "Road" and
TO ="Rd."
|
||||||||||
TRANWRD(STRING,FROM,TO) ------------------- "123 Elm
Rd."
|
||||||||||
TRANWRD("Now is the time","is","is
not") -----------------
"Now is not the time"
|
||||||||||
TRANWRD("one two
three","four","4") ---------------------
"one
two three"
|
||||||||||
TRANWRD("Mr. Rogers","Mr."," ")
----------------------- " Rogers"
|
||||||||||
TRANWRD("ONE TWO THREE","ONE TWO","A
B") -------------
"A B THREE"
|
||||||||||
data c;
|
||||||||||
x="i am going to bangalore";
|
||||||||||
y=tranwrd(x, "am going", "am not going");
|
||||||||||
run;
|
||||||||||
proc print;
|
||||||||||
run;
|
||||||||||
proc contents;
|
||||||||||
run;
|
||||||||||
/***************MORE ABOUT COMPRESS
FUNCTION**************************/
|
||||||||||
COMPRESS(source, chars, modifiers)
|
||||||||||
source specifies the character constant, variable or expression
from which the specified
chars to be removed.
|
||||||||||
chars specifies the list of characters to be removed from the
source.
|
||||||||||
If you specify K in the modifier then the chars in the
"chars" list will be kept and others
are removed
|
||||||||||
You can add more characters to remove in this list by specifying
modifiers in the third
argument
|
||||||||||
modifier specifies constant, variable or expression in which
each non blank character
modifies the action of the compress function. These
modifiers has to provided in
quotes if character constants.
|
||||||||||
1. A adds alphabetic
characters to the "chars" list
|
||||||||||
2. C adds control characters to the list
|
||||||||||
3. D adds digits to the list
|
||||||||||
4. F adds an underscore and English characters to the list
|
||||||||||
5. G adds graphic characters to the list
|
||||||||||
6. H adds horizontal tab characters to the list
|
||||||||||
7. I ignores the case of the characters to be kept or removed
|
||||||||||
8. L adds lower case characters in the source to the list
|
||||||||||
9. P adds punctuation characters to the list
|
||||||||||
10. S adds space characters to the list
|
||||||||||
11. T trims trailing blanks from the first and the second
characters
from the list
|
||||||||||
12. U adds uppers case letters to the list
|
||||||||||
13. W adds printable characters to the list
|
||||||||||
14. Adds hexadecimal characters to the list
|
||||||||||
If when you are pulling the data and you got special characters
then
give KP or KW in the third argument
Some Numeric Functions in SAS
Numeric functions
1. Int(value)
returns
the integer value of the value.
data
p4;
b=int(10.33);
proc
print;
run;
the
result will be 10
2. MOD(value1, value2)
Returns
the remainder when value1 is divided by value2
data
p4;
a=mod(10.33,1);*to
extract the fraction part;
proc
print;
run;
the
result will be .33
3. ABS(argument)
Returns
the absolute value of the argument
data
p4;
d=abs(-10.33);
proc
print;
run;
The
result will be 10.33
4. ROUND(argument1, argument2)
Rounds
the number depending on the second argument.
data
p5;
x=123.456789123456789;
a=round(x,
0.0001);*the digit in the place of 1 will get rounded and the remaining digits
will be deleted;
b=round(x,
0.00001);
c=round(x,
0.00000001);
d=round(x,
1);
e=round(x,
10);
f=round(x,
100);
proc
print;
run;
When
you are rounding off the decimal values, SAS rounds off the digit corresponding
to the location of 1 in the argument2 (see a,b,c). For the remaining
1 means
round off to whole number (without decimals), 10 means round off to a value
which is a multiple of 10, 100 means round off to a value which is a multiple
of 100 and so on.
5. CEIL(argument)
CEIL
function works only on the numbers having decimal values. It rounds off the
argument to the next higher whole number. If the argument is a whole number
then it will be returned as it is.
data
abc;
x=ceil(1.22);
y=ceil(4);
z=ceil(-1.22);
run;
proc
print;
run;
The
result for x will be 2, y will be 4, z will be -1
6. FLOOR(argument)
FLOOR
function works only the numbers with decimal values. It rounds of the argument to the lower whole number.
data
abc;
x=floor(1.22);
y=floor(4);
z=floor(-1.22);
run;
proc
print;
run;
the
result for x will be 1, y will be 4 and z will be -2
7. FACT(argument)
FACT
function returns the factorial of a number
data
p6;
b=fact(3);
proc
print;
run;
the
result will be 6
8. LOG(argument)
LOG
function will return the natural logarithm of a number
9. LOG10(argument)
This
function returns the logarithm to the base 10 for a number
10. MIN(argument1,…..,argumentN)
Returns
the minimum value among the arguments
11. MAX(argument1,…..,argumentN)
Returns
the maximum value among the arguments
12.
MEAN(argument1,…..,argumentN)
Returns
the mean value of the arguments. If any value is a missing value then that
value will be discarded (not counted)
13. STD(argument1,…..,argumentN)
Returns
the standard deviation of the arguments. If any value is missing value then
that value will be discarded
14. N(argument1,…..,argumentN)
Returns
the number of the non missing values among the arguments
data
a;
input
a;
b=n(a,2,3,4,.);
c=nmiss(a,2,3,4,.);
cards;
1
2
3
4
.
.
;
proc
print;
run;
15
NMISS(argument1,…..,argumentN)
Returns
the number of missing values among the arguments
16. SQRT(argument)
Returns
the square root of the argument
17. LAG(argument)
Returns
the previous observation value of the argument
18. DIF(argument)
Returns
the difference of the argument's value in the current and the previous
observations
19. DIM(array-name, bound-n)
Returns
the dimension of the array. It actually returns the number of elements in the
arry. Bound-n specifies whether it is a multi-dimension array or a single
dimension array.
For a
single dimension array, bound-n is not required.
20. DIVIDE(argument1, argument2)
Returns
the quotient when argument1 gets divided by argument2
21. SIGN(argument)
Returns
1 or -1 depending upon the sign of the argument. Sign of 0 will be 0 and it
will be missing for a missing value.
data
a;
x=sign(-50);
y=sign(50);
z=sign(0);
a=sign(.);
run;
proc
print;
run;
22.
Range(argument1,…….,argumentN)
Returns
the range of the arguments i.e. largets value - smallest value.
23. Missing(character argument
or numeric argument)
Returns
0 if not missing and 1 if the argument is a missing value
data
a;
x=missing(1);
y=missing("char");
run;
proc
print;
run;
24. FUZZ(argument)
Returns
the nearest integer if the if the argument is within 1E-12
25. TRUNC(argument, length)
Truncates
the numeric argument to the specified length |
No comments:
Post a Comment