Академический Документы
Профессиональный Документы
Культура Документы
DAY 4
Functions:
Functions perform a calculation on, or a transformation of, the arguments
given in parentheses following the function name.
SAS functions have the following general form:
function-name(argument1, argument2, ...,argument n);
All functions must have parentheses even if they dont require any arguments.
Arguments are separated by commas and can be variable names, constant
values such as numbers or characters enclosed in quotation marks, or
RESULT expressions.
SAS has a number of in-built functions. Broadly they can be classified as :
Arithmetic Functions
String Functions
Date Time Functions
Each of these functions are described below
Functions:
- Arithmetic Functions
ABS:
Returns the absolute value of the argument.
Syntax:
ABS(argument)
Description:
argument is numeric
Example:
Example
X=ABS(2.4)
X=ABS(-3)
Result
X=2.4
X=3
Continued
INT:
Returns the integer value
Syntax:
INT(argument)
Description:
argument is numeric
Example:
Example
X=INT(2.1)
X=INT(-2.4)
X=INT(3)
Result
X=2
X=-2
X=3
Continued
FLOOR:
Returns the smallest integer that is greater than or equal to the argument.
Syntax:
Floor(argument)
Description:
argument is numeric
Example:
Example
Result
X=floor(2.3)
X=floor(5.5)
X=floor(-2.4)
X=2
X=5
X=-3
Continued
CEIL:
Returns the largest integer that is less than or equal to the argument.
Syntax:
Ceil(argument)
Description:
argument is numeric
Example:
Example
Result
X=ceil(5.5)
X=ceil(-2.5)
X=ceil(2.3)
X=6
X=-2
X=3
Continued
ROUND:
Returns the value rounded off to the nearest round-off unit.
Syntax:
ROUND(argument,round-off unit)
Description:
argument is numeric
round-off unit is numeric and non-negative
Example:
Example
Result
X=ROUND(223.456)
X=ROUND(223.456,0.1)
X=ROUND(223.456,.01)
X=223
X=223.5
X=223.46
Continued
MAX:
Returns the largest of non-missing values.
Syntax:
MAX(argument,argument)
Description:
argument is numeric
At least two arguments are required
Example:
Example
Result
X1 = MAX(2,6)
X1=6
X2 = MAX(2,-3,1,-1) X2=2
Continued
MIN:
Returns the smallest of non-missing values.
Syntax:
MIN(argument,argument)
Description:
argument is numeric
At least two arguments are required
Example:
Example
X1 = MIN(2,6)
X2 = MIN(2,-3,1,-1)
Result
X1=2
X2=-3
Continued
SUM:
Returns the sum of the variables.
Syntax:
SUM(argument,argument...)
Description:
argument is numeric
At least two arguments are required
Example:
Example
Result
X1 = SUM(4,9,3,8)
X1 = 24.00000
X2 = SUM(14,9,13,8,.) X2 = 44.00000
Continued
MEAN:
Returns the average of non-missing values.
Syntax:
MEAN(argument,argument)
Description:
argument is numeric
At least two arguments are required
Example:
Example
X1 = MEAN(2,.,.,6)
X2 = MEAN(1,2,3,2)
Result
X1 = 4
X2 = 2
Continued
MOD:
Returns the remainder value.
Syntax:
MOD(argument1,argument2)
Description:
argument1 is numeric
argument2 is numeric and can not be zero
MOD function returns the remainder when the integer
quotient of argument1 is divided by arguments
Example:
Example
Result
X=MOD(6,3)
X=MOD(10,3)
X=MOD(11,3.5)
X=0
X=1
X=0.5
Continued
SQRT:
Returns the square root of a value.
Syntax:
SQRT(argument)
Description:
argument is numeric and non-negative
Example:
Example
Result
X=SQRT(25)
X=SQRT(4)
X=5.00000
X=2.00000
Continued
STD:
Returns the standard deviation.
Syntax:
STD(argument,argument)
Description:
argument is numeric
At least two arguments are required
Example:
Example
Result
X1 = STD(6,3)
X2 = STD(2,4,6,3,1)
X1 = 2.12132
X2 = 1.92354
Continued
LOG:
Returns natural logarithm.
Syntax:
LOG(argument)
Description:
argument is numeric and positive
Example:
Example
Result
X=LOG(1)
X=LOG(10)
X=0
X=2.30259
Continued
LOG10:
Returns logarithm to the base 10
Syntax:
LOG10(argument)
Description:
argument is numeric and positive
Example:
Example
Result
X=LOG10(1)
X=LOG10(10)
X=0
X=1.0
Continued
EXP:
Returns the exponential value.
Syntax:
EXP(argument)
Description:
argument is numeric
Example:
Example
X=EXP(1)
X=EXP(0)
Result
X=2.71828
X=1
Continued
N:
The N function returns the number of non- missing numeric values among
its arguments.
Syntax:
N(argument1argument n)
N(of Var1 Var n)
Description:
argument is numeric
Example:
Example
X=N(9,.,4,.,7,8,.)
X=N(0,.,.,.,.,8,9)
Result
X=4
X=3
Continued
NMISS:
This function returns the number of missing values in the list of variables.
Syntax:
NMISS(argument1argument n)
NMISS(of Var1 Var n)
Description:
argument is numeric
Example:
Example
X=NMISS(9,.,4,.,7,8,.) X=3
X=NMISS(0,.,.,.,.,8,9) X=4
Result
Continued
MISSING:
This function returns a value of true if its argument is a missing value and
false otherwise.
Syntax:
MISSING(argument)
Description:
argument can be either character or numeric
Example:
Example
X=MISSING(2)
X=MISSING(.)
Result
X=0
X=1
Continued
LARGEST:
Enables you to extract the nth largest value, given a list of variables.
Syntax:
LARGEST(1, variable-list)
Description:
argument is numeric
Example:
Example
X=LARGEST(1, of a1-a10)
X=LARGEST(2, of a1-a10)
X=LARGEST(3, of a1-a10)
Result
X=6
X=5
X=3
Continued
SMALLEST:
Enables you to extract the nth smallest value, given a list of variables.
Syntax:
SMALLEST(1, variable-list)
Description:
argument is numeric
Example:
Example
X=SMALLEST(1, of a1-a10)
X=SMALLEST(2, of a1-a10)
X=SMALLEST(3, of a1-a10)
Result
X=1
X=2
X=3
Continued
LAG:
LAG function returns the value of its argument from the previous
observation.
Syntax:
LAG(argument)
LAGn(argument)
Description:
argument is numeric
Example:
Continued
DIF:
DIF function returns the difference of previous value from current value.
Syntax:
DIF(argument)
Description:
argument is numeric
Example:
Functions:
- String Functions
COMPRESS:
The COMPRESS function allows you to select which characters you
want to remove from a character value.
Syntax:
COMPRESS(source<,characters-to-remove><, modifiers>)
Description:
source
characters-to-remove
Modifier
Continued
Example:
Continued
COMPBL:
Converts two or more blanks to a single blank
Syntax:
COMPBL(source)
Description:
argument is character
Example:
Example
X = ABC
DEF;
Z=COMPBL(X) ;
Result
X = ABC DEF;
Z = ABC DEF;
Continued
INDEX:
Searches the source for the character string specified by the excerpt
and returns the position of the character string in the source if found.
If the character string specified by the excerpt is not found in the source
value 0 will be returned.
Syntax:
INDEX( source, excerpt)
Description:
source
excerpt
Example:
Example
X = ABC.DEF;
Y = F;
Z=INDEX(X,Y) ;
Result
X = ABC.DEF;
Y = F;
Z = 7;
Continued
SUBSTR:
Extracts a substring from an argument.
Syntax:
SUBSTR(argument,position<,n>)
Description:
argument
position
Example:
Example
Result
X = SASDATAANALYTICS;
Y = SUBSTR(X,1,3);
X = SASDATAANALYTICS
Y = SAS
Continued
SCAN:
Returns the n th word in a character expression.
Syntax:
SCAN(argument,n<,delimiters>)
Description:
argument
n
delimiters
Example:
Example
Result
X = character expression;
Y = SCAN(X,2);
X = character expression
Y = expression
Continued
TRANSLATE:
Replaces specific character in a character expression.
Syntax:
TRANSLATE(source,to-1,from-1<,to-n,from-n>)
Description:
source
to
from
Example:
Example
Result
d = xyzb
Continued
TRANSWRD:
TRANWRD function is used to substitute one word for another.
Syntax:
TRANWRD(source,to-1,from-1<,to-n,from-n>)
Description:
source
to
from
Example:
Continued
TRIM:
Removes trailing blanks from character expressions.
Syntax:
TRIM(argument)
Description:
argument: Specifies any valid SAS character expression.
Example:
Example
Result
part1 = apple ;
part2 = sauce;
noblank = TRIM(part1) || part2;
hasblank = part1 || part2
part1 = apple ;
part2 = sauce;
noblank = applesauce
hasblank = apple sauce
Continued
LEFT:
Removes Leading blanks from character expressions.
Syntax:
LEFT(argument)
Description:
argument: Specifies any valid SAS character expression.
Example:
Example
Result
part1 = apple;
part2 = sauce;
noblank = LEFT(part1) || part2;
hasblank = part1 || part2
part1 = apple;
part2 = sauce;
noblank = applesauce
hasblank = applesauce
Continued
STRIP:
Removes trailing and leading blanks from character expressions.
Syntax:
STRIP(argument)
Description:
argument: Specifies any valid SAS character expression.
Example:
Example
Result
part1 = apple ;
part2 = sauce;
noblank = STRIP(part1) || part2;
hasblank = part1 || part2
part1 = apple ;
part2 = sauce;
noblank = applesauce
hasblank = apple sauce
Continued
UPCASE:
Converts all letters to uppercase.
Syntax:
UPCASE(argument)
Description:
argument: Specifies any valid SAS character expression.
Example:
Example
Result
part1 = apple;
part2 = UPCASE(part1);
A = alka gupta
B = UPCASE(A)
part1 = apple;
part2 = APPLE;
A = alka gupta
B = ALKA GUPTA
Continued
LOWCASE:
Converts all letters to lowercase.
Syntax:
LOWCASE(argument)
Description:
argument: Specifies any valid SAS character expression.
Example:
Example
Result
part1 = Apple;
part2 = LOWCASE(part1);
A = ALKA GUPTA
B = LOWCASE(A)
part1 = Apple;
part2 = apple;
A = ALKA GUPTA
B = alka gupta
Continued
PROPCASE:
Converts all letters to propercase.
Syntax:
PROPCASE(argument)
Description:
argument: Specifies any valid SAS character expression.
Example:
Example
Result
part1 = apple;
part2 = PROPCASE(part1);
A = ALKA GUPTA
B = PROPCASE(A)
part1 = apple;
part2 = Apple;
A = ALKA GUPTA
B = Alka Gupta
Continued
INPUT:
Used to convert character values to numeric values.
The function allows you to read argument using any informat specified
by the second argument.
Syntax:
newvar = INPUT(oldvar, informat);
Description:
argument: Specifies any valid SAS character expression.
Example:
Height = input(Char_Height,8.);
Weight = input(Char_Weight,8.);
Date = input(Char_Date,mmddyy10.);
Continued
PUT:
Used to convert numeric values to character values.
The function allows you to read argument using any format specified by
the second argument.
Syntax:
newvar = PUT(oldvar, format);
Description:
argument: Specifies any valid SAS character expression.
Example:
Char_Date = put(Date,date9.);
AgeGroup = put(Age,agefmt.);
Char_Cost = put(Cost,dollar10.);
Continued
LENGTH:
The LENGTH function returns the length of a character value, not
counting trailing blanks but including any blanks within the string.
LENGTHN function is identical to LENGTH except it returns a length of 0
(instead of a length of 1) for a character missing value (also called a null
stringhence the N at the end of the function name).
Syntax:
length(source);
lengthn(source);
Description:
argument: Specifies any valid SAS character expression.
Example:
Example
a='my cat';
x=LENGTH(a);
a=' my cat ';
y=LENGTH(a);
Result
X=6
Y=6
Continued
CAT:
The CAT function takes two or more arguments and concatenates them.
It is almost the same as using the || operator, except that the length of
the resulting string defaults to 200 if you do not define the length first.
The CATS function strips off leading and trailing blanks before joining
the strings (remember S for strip).
The CATX function is similar to the CATS function, except you supply a
separator as the first argument.
Syntax:
cat(string1, string2);
cats(string1, string2);
catx(&,string1, string2);
Continued
Description:
argument: Specifies any valid SAS character expression.
Example:
Continued
FIND:
Find This is used to find a particular string.
FINDC Searches a string for the first occurrence of a character from
a list that you supply as the second argument to the function.
FINDW The FINDW function is similar to the FIND function, except
that it searches for words (hence the W in the function name).
Syntax:
General:- find(string, find-string)
If find-string is not found, the function returns a 0. An older
function, INDEX, was identical to the FIND function as described
here.
Extended:- find(string, find-string, modifiers, starting-position)
You can add two more arguments to the FIND function. One of
these arguments is a modifier that alters how the search works.
The most useful modifier is the i (ignore case) modifier. You can
also specify a starting position at which to start the search. If you
use a negative starting value, the search proceeds from right to
left.
Continued
FINDC- findc(string, find-string, modifiers, starting-position)
FINDW- findw(string, find-string, delimiters, modifiers, starting- position)
Description:
argument: Specifies any valid SAS character expression.
Example:
Continued
ANYALNUM:
Allows you to search for alphanumerics (upper- and lowercase letters
and digits).
ANYALPHA:
Allows you to search for alphas (all letters).
ANYDIGIT:
Allows you to search for digits.
ANYPUNC:
Allows you to search for punctuation characters.
ANYSPACE:
Allows you to search for space characters (blank, tab, line feeds).
Continued
Syntax:
anyalnum(string);
anyalpha(string);
anydigit(string);
anypunc(string);
anyspace(string);
notalnum(string);
notalpha(string);
notdigit(string);
notpunc(string);
notspace(string);
Description:
If the argument of ANYDIGIT contains any digits, the function
returns the position of the first one. If not, ANYDIGIT returns a 0.
Therefore, when ID contains any digits, it returns a value greater
than or equal to 1. argument: Specifies any valid SAS character
expression.
Example:
Continued
VERIFY:
This function acts somewhat like the NOT functionsit returns the
position in a string of the first invalid character and a 0 if it doesnt find
any invalid characters.
Syntax:
verify(string, valid-characters);
Description:
argument: Specifies any valid SAS character expression.
Example:
Continued
COMPARE:
The COMPARE function allows you to compare two character values.
With optional modifiers, you can ignore case and truncate a longer value
to the length of a shorter value before making the comparison.
Syntax:
compare(string1, string2,modifier);
Description:
argument: Specifies any valid SAS character expression.
Example: