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.

A Comprehensive Guide on Character Functions of SAS

  • Link Copied

We may receive data from various sites in clinical studies, and each site may enter data in its preferred format (Upper, Lower Case). We may desire to convert all of our character values to UPCASE, PROPCASE, or LOWCASE when working with such data.


Different internal codes represent uppercase and lowercase values, so you won't get a match if you test for a value like R for a variable when the real value is r. Therefore, before performing any logical comparisons, it is usually beneficial to convert all character values to either uppercase or lowercase.


These three functions change the Case of the argument.


♦ Character case can be changed to Upper or Lower using UPCASE and LOWCASE, respectively.


♦PROPCASE, which stands for "proper case," lowercase the remaining letters, and capitalizes the first character of each "word."


♦ To force the PROPCASE function to capitalize each word, the default delimiter is a space.


The second optional argument to this function is a list of delimiters.


Here is an example


Data _Null_;


SOCTERM=’Cardiac disorders’;


Up_Soc= Upcase (SOCTERM);


Lo_Soc = Lowcase (SOCTERM);


Prop_Soc = Propcase (SOCTERM);


Put Up_Soc = Lo_Soc = Prop_Soc =;


Run;


If we get this data, the task is to create a new dataset that includes the subjects who were randomly assigned to Active Treatment and experienced Adverse Events.


Data Test;


Length Patid 3 Trt $7 AE $10;


Input Patid Trt $ AE $;


Datalines;


101 active nausea 102 placebo no


105 active No 1


08 Active insomnia


109 ACTIVE Dizziness


110 Placebo Bodypain


;


Run;


Converting the cases of both TRT and AE to uppercase Data Modi;


Set Test;


Trtup = Upcase (Trt);


Aeup = Upcase (Ae);


Run;


Data Final (Drop=Trtup Aeup);


Set Modi;


Where Trtup=’ACTIVE’ and Aeup ne ‘NO’;


Run;


ANY FUNCTIONS:


These functions return the first position of ANY DIGIT, ALPHA, ALNUM, PUNCT, or SPACE in the String of characters.


Syntax: ANY***** (String, Startpos);


♦ DIGIT = any digit


♦ ALPHA = any character


♦ ALNUM = any character or digit


♦ PUNCT = any punctuation


♦ SPACE = any space


ANYALNUM FUNCTION:


This function is used to find the position of the first instance of any alphanumeric character, such as an upper- or lowercase letter or number, and return it. The function returns a 0 if none are found. This function can start its search anywhere in the String, if desired, search from right to left by passing in an optional parameter.


Syntax: ANYALNUM (character-value <, start>);


If the start value is positive, the search goes from left to right;


If the value is negative, the search goes from right to left.


Data _Null_;


String = ‘S02-P107 has Nausea’;


ADF1= ANYDIGIT (String);


ADF2= ANYDIGIT (String,6);


ADF3= ANYDIGIT (String,-25);


APF= ANYPUNCT (String);


ASF=ANYSPACE (String);


AAF=ANYALPHA (String);


AANF=ANYALNUM (String);


Put ADF1= ADF2= ADF3= APF= ASF= AAF= AANF=;


Run;


ANYDIGIT (String): ANYDIGIT is a function that takes a string as input and checks if it contains any digits.


It returns true if the String contains at least one digit and false if it does not. This function helps validate input and perform operations based on the presence of digits in a string.


♦ ANYDIGIT (String, 6) ANYDIGIT is a function that checks whether a string contains at least 6 digits.


It returns true if the String contains 6 or more digits and false if it does not. This function helps verify a specific number of digits in a string.


♦ ANYDIGIT (String,-25)The input value "-25" is invalid for the function ANYDIGIT. The function expects a positive integer value as the second parameter to specify the number of digits to check for in the String. Please provide a valid positive integer value for the second parameter.


♦ ANYPUNCT (String)ANYPUNCT is a function that checks whether a given string contains any punctuation characters. Punctuation characters are defined as any character that is not a letter, digit, or whitespace. Examples of punctuation characters include commas, periods, semicolons, hyphens, and brackets.


♦ ANYSPACE (String) The ANYSPACE function determines whether a given string contains whitespace characters. Whitespace characters include spaces, tabs, and newline characters.


♦ ANYALPHA (String) ANYALPHA is a function that checks whether a given string contains at least one alphabetic character. It returns true if the String contains at least one letter and false if it does not. This function helps validate input and ensure that a string contains alphabetic characters.


♦ ANYALNUM (String)The ANYALNUM function checks if a given string contains any alphanumeric characters, including letters and digits. It returns true if the String contains at least one alphanumeric character and false if it does not.


INDEX, INDEXC, AND INDEXW FUNCTIONS


INDEX and INDEXC functions return the position where the char or String of characters first occurs in the String. INDEXW function searches for the entire word.


Syntax:


♦ INDEX (String, Search String)


♦ INDEXC (String, Search String)


♦ INDEXW (String, Search Word)


Data _Null_;


String = ‘THIS IS SASI, A SAS ANALYST’;


Ind_val = INDEX (String, ‘SAS’);


Indc_val = INDEXC (String, ‘SAS’);


Indw_val = INDEXW (String, ‘SAS’);


Put Ind_val = Indc_val = Indw_val =;


Run;


INDEX (String, ‘SAS’) searches for the substring ‘SAS’ in the String and returns the position of Nine (9) when it encounters the String SAS, i.e., in SASI.


INDEXC (String, ‘SAS’) searches the String for either ‘S’ or ‘A’ and returns the position of Four (4) when it first encounters the letter ‘S’. i.e., in THIS


INDEXW (String, ‘SAS’) searches the String for the word ‘SAS’ and returns the position of 17 where if first finds ‘SAS’ i.e., exact word SAS.


FIND, FINDC, AND FINDW FUNCTIONS


The FIND, FINDC, and FINDW functions are used in spreadsheet software, such as Microsoft Excel, to locate the position of a specific character or substring within a cell. The FIND function is used to find the position of a particular character or substring within a cell, while the FINDC function is used to find the position of a case-sensitive character or substring.


The FINDW function finds the position of a specific character or substring within a cell, ignoring any punctuation or white space. These functions are particularly useful for manipulating and analyzing large data sets, as they allow users to quickly and easily locate specific information within a spreadsheet.


Syntax:


♦ FIND (String, Search String, Startpos, Modifier);


♦ FINDC (String, Search String, Startpos, Modifier);


The "modifiers" and "starting position" parameters can be specified using the find function, which likewise looks for a search string within a string. There are two modifiers: "t" and "i." The "t" modifier removes trailing blanks from the String, while the "i" modifier instructs SAS to ignore the Case. SAS is informed of the starting position in the String, indicating where to start.


Data _Null_;


String=’This is SASI, a SAS Analyst’;


Fnd_Val = Find (String, ‘S’);


Fndi_Val =Find (String, ‘S’, ‘i’);


Fndsp_val =Find (String, ‘S’,15);


Put Fnd_Val= Fndi_Val= Fndsp_val=;


Run;


COUNT and COUNTC Functions:


These functions count the occurrences of a String in a bigger character String. ;


Syntax:


♦ COUNT (String, Search String, <Modifier>);


♦ COUNTC (String, Search String, <Modifier>);


Modifiers: I = Ignore Case, T = Remove Leading and Trailing Blanks Data _Null_;


String = ‘Lp lp de lp LP gh lp’;


Nst_Val = COUNT (String, ‘lp’);


Cst_Val = COUNT (String, ‘LP’);


Ict_Val = COUNT (String, ‘lp’, ‘I’);


Ccnt_Val = COUNTC (String, ‘lp’);


Iccnt_Val = COUNTC (String, ‘lp’, ‘I’);


Put Nst_Val = Cst_Val = Ict_Val = Ccnt_Val = Iccnt_Val =; Run;


♦ Nst_Val count all the occurrences of ‘lp’ in the String and hence returns a value


of 3.


♦ Cst_Val count all the occurrences of ‘LP’ in the String and returns a value of 1.


♦ Ict_Val count all the occurrences of ‘lp’ and ‘LP’ due to ‘I’ modifier in the String and hence returns value of 5.


♦ Ccnt_Val count all the occurrences of ‘l’ as well as ‘p’ in the String and hence re- turn the value of 7.


♦ Iccnt_Val count all the occurrences of ‘l’,’L’,’p’, and ‘P’ in the String due to the ‘I’ modifier and hence returns the value of 10.


These are the few character functions of SAS; moreover, in the next blog, we shall discuss the remaining SAS character functions in detail.


In conclusion, the Character Functions of SAS are essential tools for manipulating and analyzing character data. With the ability to extract substrings, concatenate strings, and perform various other operations, SAS users can efficiently and effectively handle character data in their analysis.


By mastering these functions, users can save time and improve the accuracy of their results. Whether you are a beginner or an experienced SAS user, understanding and utilizing these functions can greatly enhance your data analysis skills. So, start exploring the world of SAS character functions today and take your data analysis to the next level!

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