11 Must-Know Character
Functions in Base SAS
✂️ Clean 🔍 Extract 🔁 Replace
➕ Concatenate 📏 Align 🔠 Format Case
Whether you're learning Clinical SAS or already working in the field —
this guide is for you.
Let's decode each with clear logic and syntax! 👉
By- Dayanand Yengandul 🇮🇳
✅ All character functions in SAS follow this format:
new_var = FUNCTION(varname, options);
🔍 SCAN() – Extract Word from Character String
1.
🧪 Syntax: SCAN(varname, n, 'delimiters')
💡 Use: Extracts the nth word from a text string. Delimiters optional.
✅ Example: SCAN("Alzheimer-India/Trial", 2, "-/") → "India"
🧠Pro Tip: Great for splitting names, regions, or terms in text fields.
2. 🔍 SUBSTR() – Extract Substring from Character String
🧪 Syntax: SUBSTR(varname, start, <length>)
💡 Use: Returns a part of a character string starting from a specific
position. You can also control how many characters to extract
(substring).
✅ Example: SUBSTR("CLINICALSAS", 9, 3) → "SAS"
🧠Pro Tip: Use to Extract Study IDs, suffixes, or specific values from
long text variables.
3. LEFT() and RIGHT() – Align Text in Character Variables
🧪 Syntax: LEFT(varname) / RIGHT(varname)
💡 Use: LEFT() Aligns character text to the left;
RIGHT() Aligns character text to the right.
✅ Example:
LEFT(" SAS") → "SAS " | RIGHT("SAS ") →" SAS"
🧠Pro Tip: Character data is left-aligned, numeric is right-aligned by
default—helps in neat display and easy comparison.
4. TRIM() and STRIP() – Remove Extra Spaces from Character Value
🧪 Syntax: TRIM(varname) | STRIP(varname)
💡 Use: TRIM() removes trailing spaces (at the end).
STRIP() removes leading and trailing spaces.
✅ Example:
TRIM("SAS ") → "SAS" | STRIP(" SAS ") → "SAS"
🧠Pro Tip: Use STRIP() when comparing or cleaning character values—
more reliable than TRIM() alone.
5. COMPRESS() – Remove Characters or Spaces from Text
🧪 Syntax: COMPRESS(varname, 'characters-to-remove', 'modifiers');
💡 Use: Removes specified characters and by default also removes all
spaces.
✅ Example:
clean1 = COMPRESS(name); → (Removes all spaces in name variable)
clean2 =COMPRESS("SAS123", , "kd") → "123" Keeps only digits
🧠Pro Tip: Use modifiers like 'd' for digits, 'a' for alphabets, 'k' to keep only
specified characters.
6. COMPBL() – Replace Multiple Spaces with a Single Space
🧪 Syntax: COMPBL(varname);
💡 Use: Replaces two or more consecutive spaces with a single space —
useful for cleaning messy character data.
✅ Example:
clean1 = COMPBL(name); → Converts "Clinical SAS Programmer" to
"Clinical SAS Programmer"
clean2 = COMPBL(address); → Makes "Pune Maharashtra" into
"Pune Maharashtra"
🧠Pro Tip:
Use COMPBL() when you want to clean extra spacing without removing
all spaces, unlike COMPRESS().
7. TRANWRD() – Replace Part of Text with New Text
🧪 Syntax: TRANWRD(varname, 'old', 'new');
💡 Use: Replaces a word or part of the text with another word.
✅ Examples:
text1 = TRANWRD(name, "Trial", "Study"); → "Alzheimer Trial" becomes
"Alzheimer Study"
text2 = TRANWRD(dept, "HR", "Human Resources"); → "HR" becomes
"Human Resources"
🧠Pro Tip:
Use this when you need to update or standardize words in a column (e.g.,
fix spelling, rename departments, etc.).
8. INDEX() – Find Position of Text (Case-Sensitive)
🧪 Syntax: INDEX(varname, 'search-text');
💡 Use: Returns the position (number) where the search text starts in the
string. Returns 0 if not found.
✅ Examples:
post1 = INDEX(name, "India");→ "Alzheimer India" → Returns 11
post2 = INDEX(dept, "sas"); → "SAS Team" → Returns 0 (case-sensitive)
🧠Pro Tip:
Use INDEX() when you need to locate a word or phrase in a string exactly
as typed (matching case).
9. FIND() – Search for Text (Case-Insensitive Option)
🧪 Syntax:
new_var = FIND(varname, 'search-text' <, start-pos, 'modifiers'>);
💡 Use: Returns the position of the search text in a string.
You can ignore case using modifiers like 'i'.
✅ Examples:
post1 = FIND(name, "india"); → →
"Alzheimer India" Returns 11
→
post2= FIND(name, "india", 1, 'i'); →
Ignores case Returns 11
🧠 Pro Tip:
Prefer FIND() over INDEX() when you're unsure about letter case or want
more control (like setting a start position).
10. UPCASE(), LOWCASE(), PROPCASE() – Change Letter Case in Text
🧪 Syntax:
UPCASE(varname) | LOWCASE(varname) | PROPCASE(varname)
💡 Use:
UPCASE() converts text to uppercase
LOWCASE() converts text to lowercase
PROPCASE() capitalizes the first letter of each word
✅ Example:
NEW1 = UPCASE("sas training") → "SAS TRAINING"
NEW2 = LOWCASE("Clinical SAS") → "clinical sas"
NEW3 = PROPCASE("john DOE") → "John Doe"
🧠Pro Tip: Use these to standardize input for better matching,
reporting, and readability.
11. CAT(), CATT(), CATS(), CATX() – Concatenate Text in SAS
🧪 Syntax:
CAT(var1, var2) | CATT(var1, var2) | CATS(var1, var2) |
CATX("delimiter", var1, var2)
💡 Use:
CAT() joins values without removing spaces
CATT() removes trailing spaces
CATS() removes leading and trailing spaces
CATX() removes spaces and inserts a delimiter
✅ Example:
name1 = CAT("Base ", " SAS") → "Base SAS"
name2 = CATT("Base ", " SAS ") → "BaseSAS"
name3 = CATS(" Base ", " SAS ") → "BaseSAS"
new_id = CATX("-", study, patient); → "ALZ-001"
🧠Pro Tip:
CATS() is best when you want to remove unwanted spaces during
merging.
CATX() is ideal for cleanly joining values with delimiters like
commas or hyphens.
Use these instead of || for more control over spacing and
formatting.
Thank You for Exploring SAS Character Functions!
Hope you found it helpful and practical.
🧠 Let’s learn together
📌 Follow for more SAS insights
💬 Drop your feedback below.
🎯 Coming Next: Numeric & Date Functions – Simplified,
Practical & Beginner-Friendly
Dayanand Yengandul 🇮🇳
Learning Clinical SAS | B. Pharmacy Graduate