SAS Functions

In this topic, we are going to discuss the SAS functions. Functions allow us to do extensive manipulation in data and existing data sets of SAS.

SAS provides a wide variety of built-in functions that are used in data processing and analysis. We use these functions as a part of the DATA statements.

A function accepts variables as arguments and provides a result, and this result can be stored into another variable. The number of arguments can vary, on the basis of the type of the function. Some functions accept a fixed number of arguments while some accept zero arguments

Syntax:

To implement a function, use the following syntax:

Where,

FUNCTIONNAME: It is the name of the SAS built-in function.

Argument: It is the value provided to the function so that it can take instructional action. The argument can be a variable, constant expression, or another function.

Depending on the data type, functions are categorized in the following categories:

  • CharacterFunctions
  • Date and TimeFunctions
  • MathematicalFunctions
  • TruncationFunctions
  • MiscellaneousFunctions

Character Functions

Character functions are used to manipulate character or string values.

Following are the various SAS built-in character functions used to manipulate string values:

  • UPCASE: It converts all the letters of string into the upper case.
  • LOWCASE: It converts all the letters of string into the lower case.
  • PROPCASE: It converts each first letter of the string into the upper case.
  • REVERSE: It is used to reverse the string letters.
  • SCAN: It returns the string value of a particular index.

Example:

Execute the above code in SAS Studio:

SAS Functions

Output:

SAS Functions

Date and Time Functions

Date and time functions are used to process date and time values.

Following are the various SAS built-in Date and Time functions used to manipulate date and time values:

  • INTCK: It is used to count the total number of years or months between dates.
  • Weekday: It is used to display the weekday on a particular date.
  • Today: It returns the current date in SAS date format.
  • Time: It returns the current time in SAS time format.

Example:

Execute the above code in SAS Studio:

SAS Functions

Output:

SAS Functions

Mathematical Functions:

Mathematical Functions are used to apply mathematical calculations on the numeric or variable values.

Following are the SAS built-in Mathematical Functions used to perform calculations on numeric values:

  • MAX: It returns the maximum value among variable values.
  • MIN: It returns the minimum value among variable values.
  • MEDIAN: It returns the median value by calculating all variable values.
  • RANUNI: It returns random value.
  • SQRT: It returns the square root value of the sum of all variable values.

Example:

Execute the above code in SAS Studio:

SAS Functions

Output:

SAS Functions

Truncation Functions

Truncation Functions are used to truncate numeric values. Following are the SAS built-in Truncation functions used to perform truncation on the integer values.

  • Ceil: It returns the greatest nearest value of the integer value.
  • Floor: It returns the smallest nearest value of the integer value.
  • Int: It returns the numeric portion or the digits left side to the decimal number.
  • Round: It returns the nearest value of the integer value.

Example:

Execute the above code in SAS Studio:

SAS Functions

Output:

SAS Functions

Miscellaneous Functions

Let's now understand two miscellaneous functions of SAS which are usually used.

  • ZIPSTATE
  • MORT

ZIPSTATE

It returns the upper-case two-letter state postal code (or global GSA geographic code for U.S. territories) that corresponds to its five-digit ZIP state code.

Example:

Here we are taking zipstate code 27511 which corresponds to global GSA geographic code NC for U.S. territories.

Execute the above code in SAS Studio:

SAS Functions

Output:

SAS Functions

Mort

It returns amortization parameters.

Syntax:

Where,

a: It is a numeric value which specifies the initial amount.

p: It is a numeric value which specifies the periodic payment.

r: It is a numeric value which specifies the periodic interest rate, which is expressed as a fraction.

n: It is an integer value which specifies the number of compounding periods.

Example:

The amount of ₹ 50,000 is borrowed for 30 years at an annual interest rate of 10 percent. It compounded monthly. So, the monthly payments can be expressed as follows:

Execute the above code in SAS Studio:

SAS Functions

Output:

SAS Functions
Next TopicSAS Macro