Welcome to Sulekha IT Training.

Unlock your academic potential here.

“Let’s start the learning journey together”

Do you have a minute to answer few questions about your learning objective

We appreciate your interest, you will receive a call from course advisor shortly
* fields are mandatory

Verification code has been sent to your
Mobile Number: Change number

  • Please Enter valid OTP.
Resend OTP in Seconds Resend now
please fill the mandatory fields including otp.

SAS Character Functions Demystified: A Comprehensive Guide

  • Link Copied

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.

Take the next step toward your professional goals

Talk to Training Provider

Don't hesitate to talk to the course advisor right now

Take the next step towards your professional goals in SAS

Don't hesitate to talk with our course advisor right now

Receive a call

Contact Now

Make a call

+1-732-338-7323

Enroll for the next batch

Related blogs on SAS to learn more

Latest blogs on technology to explore

X

Take the next step towards your professional goals

Contact now