*Copyright @ www.mycsg.in;


Function definition

Where can a SAS function can be used?

  1. in data step programming
  2. in a WHERE expression (where statement or where clause)
  3. in proc sql

What is a character function?

Character functions that converts the case of characters

Function Defintion
UPCASE Upcase function is used for converting english alphabet letters of a variable's value to uppercase
LOWCASE Lowcase function is used for convering englight alphabet letters of a variable's value to lowercase
PROPCASE Propcase function is used for converting all words of a variable's value to proper case.
That is, propcase function converts the first letter of each 'word' to uppercase (and other letters of the word to lowercase). By default these characters are considered word separators: blank, forward slash, hyphen, open parenthesis, period

Create sample data

Create 3 new variables named upper, lower and proper by appying upcase, lowcase and propcase functions respectively on sring variable

Character Functions that return the length of the strings

Function Definition
LENGTH Length function is used for obtaining the number of characters in a character string excluding trailing blanks, retruns a value of 1 for a blank value
LENGTHN LengthN function is used for obtaining the number of characters in a character string excluding trailing blanks, retruns 0 for blank value

Create sample data

Create two new variables length and lengthn by obtaining the length of string variable using LENGTH and LENGTHN functions

Character function to extract words from a string

Function Definition
SCAN The SCAN function extracts a specified word from a character string. A word is a sequence of characters separated by delimiters. By default, these characters are considered delimiters: blank, !, $, %, &, (, ), *, +, ,, -, ., /, ;, <, ^, |.

Create sample data

Create new variables to extract specific words using SCAN function

Character function to extract or modify parts of a string

Function Definition
SUBSTR The SUBSTR function is used to extract a substring from a character string, starting at a specified position for a specified length. It can also be used to replace part of a string by assigning a value to a substring.

Create sample data

Extract and modify parts of a string using SUBSTR function

Character function to find the position of a substring in a string

Function Definition
FIND The FIND function returns the position of the first occurrence of a substring within a character string. It includes options to ignore case sensitivity or specify a starting position. If the start position is negative, the search begins at that position and proceeds backwards through the string.

Create sample data

Find the position of a substring using FIND function

Example 1: Basic Usage

Example 2: Using 'i' Modifier to Ignore Case

Example 3: Using a Negative Start Position for Reverse Search

Example 4: Combining Modifiers and Start Position

Character Functions to Count Substrings, Characters, and Words

Function Definition
COUNT The COUNT function counts the number of times a specified substring appears in a character string. The search is case-sensitive by default.
COUNTC The COUNTC function counts the number of specific characters that appear in a character string. It allows for multiple characters to be counted at once.
COUNTW The COUNTW function counts the number of words in a character string. Words are defined as sequences of characters separated by delimiters. By default, delimiters include spaces, punctuation marks, and other special characters.

Create sample data

Create variables count_substring, count_characters, and count_words using COUNT, COUNTC, and COUNTW functions

Character Function to Replace Substrings in Clinical Trials Data

Function Definition
TRANWRD The TRANWRD function replaces all occurrences of a specified substring within a character string with another substring. This function is useful in clinical trials data to standardize variable values or correct data inconsistencies.

Create sample clinical trials data

Standardize visit descriptions using TRANWRD function

Character Function to Remove or Retain Specific Characters in Strings

Function Definition
COMPRESS The COMPRESS function removes specific characters from a character string. It can also be used to retain specific characters by using the optional `modifiers` argument. The 'k' modifier (keep) is combined with other modifiers (e.g., 'd' for digits) to retain specific characters.

Create sample data

Examples for removing specific characters using COMPRESS function

Examples for retaining specific characters using COMPRESS function