Certification Summary---Functions

User:Ywang10/Work

SAS Functions can be used to convert data and to manipulate the values of character variables. Functions are written by specifying the function name, then it's arguments in parentheses. Arguments can include variables, constants, or expressions. Although arguments are typically separated by commas, they can also be specified as variable lists or arrays.

YEAR, QTR, MONTH and DAY Functions
To extract the year, quarter, month or day value from a SAS date value, specify the YEAR, QTR MONTH, or DAY function followed by the SAS date value in parentheses.

WEEKDAY Function
To extract the day of the week from a SAS date value, specify the function WEEKDAY followed by the SAS date value in parentheses.

MDY Function
To create a SAS date value for a month, day and year, specify the MDY function followed by the date values. The result can be displayed in several ways by applying a SAS date format.

DATE and TODAY Function
To convert the current date to a SAS date value, specify the DATE or TODAY function without arguments.

SUBSTR Function
The SUBSTR function can be used to extract or replace any portion of a character string. To extract value, place the function on the right side of an assignment statement and specify, in parentheses, the name of the character variable, the starting character position, and the number of characters to extract. To replace values, place the function on the left side of an assignment statement and specify, in parentheses, the name of the variable being modified, the starting character position, and the number of characters to replace.

General form, SUBSTR function:

SUBSTR(argument, position,)

where


 * argument specifies the character variable or expression to scan.
 * position is the character position to start from.
 * n specifies the number of characters to extract. If n is omitted, all remaining characters are included in the substrin.

Example:

1. To replace the fourth and fifth characters of a variable named Test with the value 92. substr(test,4,2)='92'; Results: S7381K2>S7392K2 S7381K7>S7392K2

2. To returns the requested string. MiddleInitial=substr(middlename,1,1); Results: MiddleName       MiddleInitial Marie            M LEE               L Thomas            T

INDEX Function
To test character values for the presence of a string, use the INDEX function and specify, in parentheses, the name of the variable and the string enclosed in quotation marks. The INDEX function can be used with an IF statement when you are creating a data set. However, only those observations in which the function finds the string and returns a value greater than 0 are written to the new data set.

See also the assignment #1 in Session 9.

UPCASE Function
The UPCASE function converts all letters in a character expression to uppercase. include the function in an assignment statement, and specify the variable name in parentheses.

LOWCASE Function
Uppercase letters in character values can be converted to lowercase by using the LOWCASE function. Include the function in an assignment statement, and specify the variable name in parenthesis.

INT Function
To return the integer portion of a numeric value, use the INT function. Any decimal portion of the INT function argument is discarded.

ROUND Function
To round values to the nearest specified unit, use the ROUND function. If a round-off unit is not provided, the argument is rounded to the nearest integer.

TRIM Function
The TRIM function enables you to remove trailing blanks from character values.

Some Sample Certification Examples
1.Which funciton calculates the average of the variables Var1, Var2, Var3, and Var4? a. mean (Var1, Var4) b. mean (Var1-Var4) c. mean (of Var1, Var4) d. mean (of Var1-Var4)

Correct answer:D

Use a variable list to specify a range of variables as the function argument. When specifying a variable list, be sure to precede the list with the word OF. If you omit the word OF, the function argument might not be interpreted as expected.

2.Within the data set Hrd.Temp, payrate is a character variable an d Hours is a numeric variable. What happens when the following program is run? data work.temp; set hrd.temp; salary=payrate*hours; run;

a.SAS converts the values of Payrate to numeric values. No message is written to the log.

b.SAS converts the values of Payrate to numeric values. A message is written to the log.

c.SAS converts the values of Hours to character values. No message is written to the log.

d.SAS converts the values of Hours to character values. A message is written to the log.

Correct answer:B

When this Data step is executed, SAS automatically converts the character values of Payrate to numeric values so that the calculation can occur. Whenever data is automatically converted, a message is written to the SAS log stating that the conversion has occurred.

3. A typical value for the character variable Target is 123,456. Which statement correctly converts the values of Target to numeric values when creating the variable TargetNo?

a.TargetNo=input(target,comma6.)

b.TargetNo=input(target,comma7.)

c.TargetNo=put(target,comma6.)

d.TargetNo=put(target,comma7.)

Correct answer:B

You explicitly convert character values to numeric values by using the INPUT function. Be sure to select an informat that can read the form of the values.

Points to remember

 * Even if a function doesn’t require arguments, the function name must still be followed by parentheses.


 * When specifying a variable list or an array as a function argument, be sure to precede the list or the array with the word OF.


 * To remember which function requires a format versus an informat, note that the INPUT function requires the INformat.


 * If you specify an invalid date in the MDY function, a missing values is assigned to the target variable.


 * The SCAN function treats contiguous delimiters as one delimiter; leading delimiters have no effect.


 * When using the SCAN function, you can save storage space by adding a LENGTH statement to your DATA step to set an appropriate length for your new variables. Place the LENGTH statement before the assignment statements that contain the SCAN function.


 * When the SUBSTR function is on the left side of an assignment statement, it replaces variable values. When SUBSTR is on the right side of an assignment statement, it extracts variable values. The syntax of the function is the same; only the placement of the function changes.


 * The INDEX function is case sensitive.