MET MA 603: SAS Programming and Applications
MET MA 603:
SAS Programming and Applications
Functions
1
1
Using Functions
In mathematics, a function (e.g., f(x)) is a specific relationship between an input and an output. For example, the square root function takes a number as input, and returns the result of raising the number to the one half power as an output.
Functions in SAS work in much the same way. The input values are entered inside parenthesis following the name of the function. Some functions have more than one input, while others don’t have any input value at all.
Note that in SAS the exponential function, i.e., xy, is entered using the syntax x ** y.
In almost all cases, a given function’s input and output correspond to a particular data type.
2
2
Examples of Functions with Character Input
CAT: Combines two or more inputs
Cat(City, “, ”, State) -> Boston, MA
COMPRESS: Removes all spaces from the input
Compress(“New York, New York) -> NewYork,NewYork
SUBSTR: Returns a specific part of the input
Substr(“Hello, World!”, 3, 6) -> llo, W
LENGTH: Returns the number of characters in the input
Length(“Hello, World!”) -> 13
FIND: Returns the position of a substring within
find(‘She sells seashells? Yes, she does.’,’she ‘) -> 27
3
3
Examples of Functions with Numeric Input
LOG: Returns the natural log of the input
Log(100) -> 4.605
MAX: Returns the maximum of the inputs
Max(5, 55, 2, 42, 100, 8, 9) -> 100
MIN: Returns the minimum of the inputs
Max(5, 55, 2, 42, 100, 8, 9) -> 2
SUM: Returns the sum of the inputs
Sum(5, 55, 2, 42, 100, 8, 9) -> 221
ROUND: Returns the value of the rounded input
Round(4.605, .1) -> 4.6
Round(4.605) -> 5
4
4
Examples of Functions with Date Input
MDY: Returns the data corresponding to the inputs
MDY(1,3,1960) -> 2
DAY: Returns the day of the month of the input
Day(2) -> 3
MONTH: Returns the month of the input
Month(2) -> 1
YEAR: Returns the year of the input
Year(2) -> 1960
TODAY: Returns the current date (does not use an input)
Today() -> 21342
5
5
Practice
Use the reps.sas dataset. Create a temporary SAS dataset, named reps, which has a new variable containing the office number.
Hint: Use the FIND function described in the Readings link.
Your result should look similar to what is below (only the first several records are shown):
6
6
Readings
Textbook sections 3.1, 3.2, 3.3, 3.4, 3.9
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002267763.htm
7
7
/docProps/thumbnail.jpeg