
In the previous blog, we discussed a few character functions in SAS; now, we shall look at the remaining crucial functions comprehensively.
What is the SAS Character function?
Character functions in SAS are a set of built-in operations and tools that allow users to manipulate and transform character or text data within SAS programs. These functions provide a wide range of capabilities, from modifying the case of characters with functions like UPCASE (converting to uppercase) and LOWCASE (converting to lowercase) to extracting specific substrings or characters from text using functions like SUBSTR or SCAN.
Character functions are instrumental in data cleaning, text parsing, and formatting tasks, making it easier to prepare and analyze textual data in SAS datasets. By leveraging these functions effectively, data analysts and programmers can ensure data consistency, extract relevant information, and enhance the overall quality of data processing and reporting within the SAS environment.
Now, we shall discuss character functions in SAS and its benefits. To have comprehensive understanding of SAS functions and its characters, you join SAS certification course to obtain worldwide recognized certification.
LEFT, RIGHT Functions:
These functions are used to align text either to the left or to the right. It is important to note that the length of a character variable remains constant when utilizing these two functions. The LEFT function repositions the first non-blank character to the beginning of a string, while any leading blanks are relocated to the end. Conversely, the RIGHT function is employed to shift the non-blank text towards the right, while any trailing blanks are relocated to the left.
LEFT Function:
To align text values to the left, the LEFT function does not eliminate the leading blanks; instead, it relocates them to the end of the String. Therefore, the storage length of the variable remains the same, regardless of whether the result of the LEFT function is assigned to a new variable. Utilizing the LEFT function proves highly advantageous when values have been obtained by implementing the $CHAR information, as this method effectively retains any leading blank spaces.
RIGHT Function:
To align a text string to the right. It is essential to observe that when the length of a character variable has been predetermined, including trailing blanks, the RIGHT function will reposition the characters to the end of the String and append the blanks to the beginning, maintaining the original length of the variable.
Syntax: RIGHT (character-value) Trimming and Concatenation Functions:
Frequently, the DATA you receive may contain undesirable gaps, which you should eliminate to clean your DATA. SAS includes many practical character functions for dealing with blanks in a character String.
Several commonly employed functions are utilized to eliminate leading blanks, trailing blanks, or multiple blanks within the middle of a String. The objective is to enhance the accuracy and efficiency of DATA manipulation.
The functions include TRIM, TRIMN, STRIP, LEFT, COMPRESS, COMPBL, and a few concatenation functions, including CAT, CATT, CATS, and CATX.
TRIM, TRIMN, and STRIP Functions:
The TRIM, TRIMN, and STRIP functions are used in programming and data manipulation to remove leading and trailing spaces from a string. The TRIM function removes both leading and trailing spaces, while the TRIMN function removes only leading spaces. The STRIP function also removes both leading and trailing spaces, but it also removes any additional spaces within the String, condensing multiple spaces into a single space.
TRIM Function:
To eliminate any trailing white spaces from a character value, this feature proves to be particularly advantageous when concatenating many Strings, as each String may potentially have trailing white spaces. It is noteworthy that the length of the variable obtained from the TRIM function will remain unchanged unless the length of said variable has been explicitly specified beforehand.
Syntax: TRIM (character-value)
Data Test;
Length T_FullName $ 30;
Informat NAME1 NAME2 $15.;
Infile Datalines Missover;
Input NAME1 NAME2 NAME3;
T_FullName = TRIM (NAME1) || ‘ ‘ || TRIM (NAME2); FullName = NAME1 || NAME2;
Keep T_FullName FullName;
Datalines;
Sriram Prakhyath
David Anand
Mahesh Iyer ;
Run;
Combining names involves utilizing the concatenate operator (||). The TRIM function removes any trailing whitespace from individual words, each 15 bytes in length, before their
concatenation. In the absence of the TRIM function, there exists an occurrence of additional spaces between each of the names.
STRIP Function:
♦ To strip leading trailing blanks from character variables or Strings.
♦ STRIP (CHAR) is equivalent to TRIMN (LEFT(CHAR)), but more convenient.
Syntax: STRIP (character-value)
Character value refers to a type of expression in SAS representing a character. When the STRIP function is employed to generate a new variable, the length of the stated variable will correspond to the length of the Argument provided by the STRIP function. If leading or trailing blanks are removed, the resulting output will be supplemented with trailing blanks to achieve the desired length. The STRIP function is a valuable tool when employing the concatenation operator.
Data _NULL_;
ONE = “ S101 “; *Note: three leading and trailing blanks; TWO = “ P107 “; *Note: three leading and trailing blanks; N_STP = “:†|| ONE || “-†|| TWO || “:â€;
STP = “:†|| STRIP(ONE) || “-†|| STRIP(TWO) || “:â€;
PUT ONE= TWO= / N_STP= / STP=;
RUN;
Functions That Join Two or More Strings Together:
Concatenation functions make it considerably easier to join Strings together and, if desired, to insert one or more separator characters between the Strings. This is true even if you can use the || concatenation operator with the STRIP, TRIM, or LEFT functions.
CAT Functions (CAT, CATS, CATT, and CATX):
When joining two or more character strings, the CAT function concatenates (joins) them while maintaining the same leading and trailing blanks. This function does the identical operation as the concatenation operator (||).
Syntax: CAT (String-1, String-2 <, String-n>)
COMPRESS Function
Compress function Compresses/removes blanks (By default) or specified characters from a given String.
Syntax:
New_Var = Compress (String,,);
Data _Null_;
String = ‘ Y Lakshmi Prasad ‘;
Cmp_Str = COMPRESS (String);
Put String =;
Put Cmp_Str =;
Run;
COMPRESS Function with the second Argument:
Data _Null_;
Mob_Num=’n897-878-+ +/&4848’;
Cmp_Num = COMPRESS (Mob_Num, ‘n -’);
Put Mob_Num =;
Put Cmp_Num =;
Run;
COMPRESS Function with the Third Argument:
Modifiers that can be used are:
K = Keep
I = Ignore case
D = Digits
A = Alpha (upper and lower)
S = Space
P = Punctuations
Data Test;
String= ‘Sasi 905-911-9070’;
Cmp_V1= COMPRESS (String,’S’,’I’);
Cmp_V2= COMPRESS (String, ,’A’);
Cmp_V3= COMPRESS (String, ,’P’);
Cmp_V4= COMPRESS (String, ,’KDS’);
Cmp_V5= COMPRESS (String,’0’ ,’KDS’);
Run;
Cmp_V1: Compress (String, 'S', 'I') would compress the letter 'S' and ignore the case using the modifier 'I'. As a result, 'S' and's' will be compressed.
Cmp_V2: The function Compress(String, 'A') lacks the second argument; however, it accepts the modifier 'a' to indicate dropping Alpha (both upper and lower). As a result, each alphabet character in the String is compressed.
Cmp_V3: Compress (String,, 'P') lacks the second argument but includes the modifier 'p', which means drop punctuations. As a result, all of the punctuation in the String is compressed. It is worth noting that the blank spaces are not punctuation or compressed.
Cmp_V4: Compress(String,, 'KDS') does not have a second argument but includes the modifiers 'KDS', which stand for 'Keep', 'Digits', and 'Space'. As a result, only digits and spaces from the String remain unchanged.
Cmp_V5: Compress (String,'0,'KDS') has an additional parameter and modifiers. The second input '0' instructs the function to compress all zeros in the String. However, the third argument, 'KDS' suggests retaining all digits and space. As seen in the result, the third argument takes precedence over the second.
Utilizing the COMPRESS function, particularly when accompanied by the third argument (new modifiers), is advantageous in streamlining our code and reducing the probability of errors.
COMPBL Function:
The Blank or Compbl function compresses many blank spaces into a single blank space. It is useful when we need to condense a lengthy string of words that may have several blank spaces between them into a single blank. The COM-PRESS function can eliminate blank spaces and specified characters from a given String.
Syntax:
New_Var = COMPBL (String);
Data Test;
Aeterm=’ Dry Skin’; Ae_cbl = Compbl (Aeterm);
Run;
REPEAT Function:
Repeat Function is to make multiple copies of a String. Syntax: REPEAT (character-value, n)
♦ Character-value is any SAS character expression.
♦ n is the number of repetitions.
The output of this function is the initial String concatenated with n repeats. Therefore, when n equals 1, the outcome will consist of two identical replicas of the initial String. If the length of the character variable containing the output of the REPEAT function is not explicitly specified, it will assume a default value of 200.
Functions That Compute the Length of Strings:
LENGTH function: To calculate the length of a character value without including trailing blanks. A Null argument returns the value 1.
Syntax: LENGTH (character-value)
♦ The LENGTH and LENGTHN functions return the length of a character variable, not counting trailing blanks.
♦ The only difference between the LENGTH and LENGTHN functions is that the LENGTH function returns a value of 1 for a Null String while the LENGTHN function returns a 0.
♦ The LENGTHC function does count trailing blanks in its calculations. Finally, the LENGTHM function returns the number of bytes of memory used to store the variable.
Data Test;
String1= ‘ Nausea ’; *String with three leading and three trailing blanks; String2= ‘ ‘;
*Empty String; len_str1= length (String1);
lenn_str1= lengthn (String1);
lenc_str1 = lengthc (String1);
len_estr2 = length (String2);
lenn_esst2 = lengthn (String2);
Run;
PUT and INPUT Functions: The PUT and INPUT functions are considered to be highly valuable tools in the realm of data manipulation since they facilitate the conversion of variables between character and numeric data types.
PUT Function:
A variable or target, a number or a character, is changed into a character output as part of the process. This conversion is done with the use of a specific style. You can use the Put method with both character and number inputs. On the other hand, it always gives a character result.
INPUT Function:
The conversion process involves transforming a character variable or target into a numeric or character output or variable. This conversion is enabled by applying an Informat, which plays a crucial role in the conversion process. The INPUT function is restricted to character targets, but the output can be either character or numeric.
In conclusion, the SAS Character Functions Demystified: A Comprehensive Guide provides a thorough understanding of character functions within SAS programming.
By breaking down complex concepts and providing practical examples, this guide equips readers with the knowledge and skills needed to manipulate and analyze character data in SAS effectively. Whether you are a beginner or an experienced user, this comprehensive resource will undoubtedly enhance your proficiency and confidence in utilizing character functions to their fullest potential.
Find a course provider to learn SAS
Java training | J2EE training | J2EE Jboss training | Apache JMeter trainingTake the next step towards your professional goals in SAS
Don't hesitate to talk with our course advisor right now
Receive a call
Contact NowMake a call
+1-732-338-7323Enroll for the next batch
SAS Certification Course
- Jun 16 2025
- Online
SAS Certification Course
- Jun 17 2025
- Online
SAS Certification Course
- Jun 18 2025
- Online
SAS Course Online Training
- Jun 19 2025
- Online
SAS Course Online Training
- Jun 20 2025
- Online
Related blogs on SAS to learn more

A Comprehensive Guide on Character Functions of SAS
Now we shall discuss a Comprehensive Guide on Character Functions of SAS in detail.

From Data to Graphs: Harnessing SAS/GRAPH for Effective Analysis
Learn how to effectively analyze and present your data with this comprehensive guide to SAS/GRAPH.

Introduction to SAS/Library
In this blog, we shall discuss introduction to SAS and its library in detail

Importing Data into a SAS Data Set: Effective Methods and Techniques
we have discussed how to import data into SAS data set, best practices for data reading in SAS, and how to create a dataset

Comprehensive Guide to SAS Functions
Learn how to leverage SAS functions to streamline your data analysis and understand the features of SAS functions.

INTRODUCTION TO SAS
We have discussed What is SAS, variables in SAS, six attributes of SAS stores and many other unique concepts of SAS.

SAS to help British Forces to hunt down Nazi Criminals!
As the SAS empowers the professionals to accomplish various success, it has also revealed it’s to open the secrets and postwar exploits helping the government to hunt down the Nazi crime people.

SAS Factory Miner will take your business analysis to next level!
SAS gained significant reach across the globe by providing a reliable platform for advanced statistical analytics. As the more and more organizations grow their need to acquire analytical software, SAS skills

Welcome SAS Factory Miner which takes your business analysis to next level!
SAS gained significant reach across the globe by providing a reliable platform for advanced statistical analytics. As the more and more organizations grow their need to acquire analytical software, SAS

SAS unveils Viya, destined to be the foundation of future products
The ability to analyze, manage and modify data from multiple types of sources makes SAS an unique and popular data analytic software suite in the world. The software suite already become friendly with the non-technical users with its GUI (Graphical U
Latest blogs on technology to explore

What Does a Cybersecurity Analyst Do? 2025
Discover the vital role of a Cybersecurity Analyst in 2025, protecting organizations from evolving cyber threats through monitoring, threat assessment, and incident response. Learn about career paths, key skills, certifications, and why now is the be

Artificial intelligence in healthcare: Medical and Diagnosis field
Artificial intelligence in healthcare: Medical and Diagnosis field

iOS 18.5 Is Here: 7 Reasons You Should Update Right Now
In this blog, we shall discuss Apple releases iOS 18.5 with new features and bug fixes

iOS 18.4.1 Update: Why Now is the Perfect Time to Master iPhone App Development
Discover how Apple’s iOS 18.4.1 update (April 2025) enhances security and stability—and why mastering iPhone app development now is key to building future-ready apps.

What is network security Monitoring? A complete guide
In the digital world, we have been using the cloud to store our confidential data to register our details; it can be forms, applications, or product purchasing platforms like e-commerce sites. Though digital platforms have various advantages, one pri

How to Handle Complex and Challenging Projects with Management Skills
Discover actionable strategies and essential management skills to effectively navigate the intricacies of challenging projects. From strategic planning to adaptive problem-solving, learn how to lead your team and achieve exceptional outcomes in compl

What are the 5 phases of project management?
A streamlined approach to ensure project success by breaking it into five essential stages: Initiation, Planning, Execution, Monitoring & Controlling, and Closing. Each phase builds on the other, guiding the team from concept to completion with clear

About Microsoft Job Openings and Certification Pathway to Explore Job Vacancies
Explore exciting Microsoft job openings across the USA in fields like software engineering, data science, cybersecurity, and more. Enhance your career with specialized certifications and land top roles at Microsoft with Sulekha's expert courses.