Functions
Functions
Functions
SAS functions
When planning modifications to SAS datasets,
examine the many SAS functions that are
available
SAS has many built in functions that greatly
reduce the need to enter commands
SAS functions are pre-written expressions that
provide shortcuts for many calculations and
manipulations of data
2
SAS Operators
Operators
– comparison operator : <, >, and =
– addition (+), subtraction (-), division (/),
and multiplication (*)
• meanscore= (score1 + score2) / 2;
• A missing value is assigned if any argument is
missing
– concatenation operator : ||
• idnum = schcode || id;
3
Some Arithmetic Functions
1. SQRT(value) returns the square root of value
– newx = SQRT(x);
2. LOG/LOG2/LOG10 - calculates the logarithm to the base
e/2/10
– newx = LOG (x);
3. ABS(value) returns the absolute value of the numeric
argument
– newx = ABS (x);
4. MOD(value,divisor) returns the integer remainder when
value is divided by divisor
– newx = MOD (x,3);
5. EXP(value) returns the constant e raised to the power
given by value.
4
Some Sample Statistic
Functions
1. MEAN (v1,v2,...,vn) - computes the arithmetic mean (average)
meanz= MEAN (4,9,3,8,.);
“WHAT is your answer?”
2. CV (v1,v2,...,vn) - calculates the coefficient of variation
3. MAX (v1,v2,...,vn) - returns the largest value
4. MIN (v1,v2,...,vn) - returns the smallest value
5. N (v1,v2,...,vn) - returns the number of nonmissing arguments
6. NMISS (v1,v2,...,vn) - returns the number of missing
arguments
7. RANGE (v1,v2,...,vn) - calculates the range
8. STD (v1,v2,...,vn) - calculates the standard deviation
9. STDERR (v1,v2,...,vn) - calculates the standard error of the
mean
10. SUM (v1,v2,...,vn) - calculates the sum of the arguments
6
Some Truncation
Functions
1. CEIL (value): returns the smallest integer greater
than or equal to the argument
2. FLOOR (value): returns the largest integer less
than or equal to the argument
3. INT (value): returns the integer value
4. ROUND(value,decimals) returns value rounded o
to the nearest value based upon the decimals
value.
newx1 = ROUND (x, 0.01);
newx2 = ROUND (x, 0.1);
7
Some Character Functions
1. SCAN(string,n,delimiters): returns the nth word from
the character string string, where words are delimited
by the characters in delimiters. If delimiters is omitted
from the function, then blanks and most punctuation
and special characters are used as the delimiters.
2. SUBSTR(string,start,n): returns a substring or part of
string beginning with the character at the position start
in the string and continuing for n characters. If n is
omitted, then the remainder of the string is extracted.
3. SUBSTR(string1,start)=string2: replaces the
characters in string1 beginning at position start in
string1 with string2.
8
Some Character Functions
4. LOWCASE(string) - converts all to lowercase
5. UPCASE (string) - converts all to uppercase
6. PROPCASE(string) – capitalizes the first letter of
each word
9
Some Character Functions
7. COMPRESS(string<, compress-list>): removes
specified characters from the string
8. COMPBL(string): replaces multiple blanks with a
single blank
9. TRIM(string): returns a new string whose trailing
blanks have been removed and whose length
corresponds with the position of the last non-blank
character in string. A blank string, however, is
returned as a string with one blank character
10. TRIMN(string): is like TRIM(string), but a blank
string is returned as a null string (length of zero).
10
Some Character Functions
11. INDEX(string,value):returns the position in the string
where the first character of value begins within the
string. If value is not contained within the string, then
the function returns zero
12. ANY/NOT functions: returns the first position in the
string where a specified value does/doesn’t locate from
the start position; 0 for not found
– ANY/NOTalnum (string<,start>): search any letter or number
– ANY/NOTalpha (string<,start>): search a letter
– ANY/NOTdigit (string<,start>): search a number
– ANYNOTpunct (string<,start>): search a punctuation
– ANY/NOTspace (string<,start>): search a space
11
Some Character Functions
13. LEFT(string): will left align a character string by
removing any leading blanks from the string. Note
that the string's length is not changed by this
function
14. RIGHT(string): will right align a character string
by removing trailing blanks from the end of the
string and inserting them at the beginning of the
string. Thus, it does not change the length of the
string
12
Some Character Functions
15. LENGTH(string): returns the “length” of a string. Here,
the “length” is defined to be the position of the right-
most non-blank character in the string, rather than the
number of characters reserved for storage of the string
16. LENGTHN (string): return the same value for non-
blank character strings as LENGTH. LENGTHN
returns a value of 0 for blank character strings, whereas
LENGTH returns a value of 1.
17. PUT(value, format.) : converting numeric values to
character values
18. INPUT(value, informat.): converting character values to
numeric values
13
Some Date and Time
Functions
1. MDY - returns a SAS date value from month, day,
and year
For example, MDY(10,17,91) returns the SAS date value
'17OCT91'D
2. DAY - returns the day of the month from a SAS date
value
DAY ('17OCT91'D) give a number 17
3. MONTH - returns the month from a SAS date value
or literal
MONTH ('17OCT91'D) give a number 10
4. YEAR - returns the year from a SAS date value
YEAR ('17OCT91'D) give a 4-digit number 1991
14
Some Date and Time
Functions
5. DATE() & TODAY(): with no argument returns the
current system date
6. WEEKDAY(date): returns an integer from 1 to 7,
1=Sunday, 7=Saturday, corresponding to the day of
the week for the SAS date
7. WEEK: returns the week number (0 to 53) with the
first Sunday of the year as week 1
15
Some Array Functions
e.g. ARRAY rainfall[1996:2005] rain1996-
rain2005
1. DIM(array-name): determines the number of
elements in an array (10)
2. HBOUND(array-name): determines the
upper bound in an array (2005)
3. LBOUND(array-name): determines the
lower bound in an array (1996)
16
In-Class Assignment
1. Generate the following data in SAS
class num firstname lastname dob test1 test2 test3
A 1 Mary Ann 1/10/1985 85 70 75
A 2 John johnson 5/15/1988 64 80 62
A 3 Ann TAYLOR 12/16/1978 60 65 60
B 1 jeff white 3/8/1981 90 85 90
B 2 dick tracy 7/7/1983 77 80
B 3 john wang 8/15/1990 60 60 60
17
In-Class Assignment
3. Calculate the age for all students
Age: round up (REF – dob)/365.25 to a integer
REF is set as TODAY
4. Create a new variable combining first and last name
together (without redundant spaces)
5. List the mean score for students whose first name starts
with a “J or j”
6. List the mean score for students whose birth year was
in 80’s
7. List the mean score for students who were born on
Sunday
18
HOMEWORK
Understand and explain how the follow functions
work
– FIND
– FINDC
– VERIFY
– STRIP
– REVERSE
– LAG & LAGn
– DIF and DIFn
19