SAS : Character Functions

Deepanshu Bhalla 59 Comments

This tutorial covers the most frequently used SAS character functions with examples. Dealing with character strings can be a little tricky compared to numeric values. Therefore, it is necessary to understand the practical usage of character functions.

1. COMPBL Function

The COMPBL function compresses multiple blanks to a single blank.

In the example below, the Name variable contains a record "Sandy   David". It has multiple spaces between the first and last name.

Create a Sample Data Set
Data char;
Input Name $ 1-50 ;
Cards;
Sandy    David
Annie Watson
Hello ladies and gentlemen
Hi, I am good
;
Run;
Use COMPBL Function
Data char1;
Set char;
char1 = compbl(Name);
run;
Output
COMPBL Function
2. STRIP Function

The STRIP function removes leading and trailing spaces.

Data char1;
Set char;
char1 = strip(Name);
run;
3. COMPRESS Function

The COMPRESS function removes leading, between and trailing spaces.

SYNTAX
COMPRESS(String, characters to be removed, Modifier)
Data char1;
Set char;
char1 = compress(Name);
run;
Output
COMPRESS Function
Remove specific characters
data _null_;
x='ABCDEF-!1.234';
string=compress(x,'!4');
put string=;
run;
It returns ABCDEF-1.23. In SAS 9.1.3, the additional parameter called MODIFIER was added to the function.
The following keywords can be used as modifiers-
  1. a – Remove all upper and lower case characters from String.
  2. ak - Keep only alphabets from String.
  3. kd - Keeps only numeric values
  4. d – Remove numerical values from String.
  5. i – Remove specified characters both upper and lower case from String.
  6. k – keeps the specified characters in the string instead of removing them.
  7. l – Remove lowercase characters from String.
  8. p – Remove Punctuation characters from String.
  9. s – Remove spaces from String. This is default.
  10. u – Remove uppercase characters from String.
Example 1 : Keep only alphabets from alphanumeric values
data _null_;
x='ABCDEF-!1234';
string=compress(x,'','ak');
put string=;
run;
It returns ABCDEF
Example 2 : Keep only numeric from alphanumeric
data _null_;
x='ABCDEF-!1234';
string=compress(x,'','kd');
put string=;
run;
It returns 1234
Example 3 : Remove all punctuation from string
data _null_;
x='ABCDEF-!1234';
string=compress(x,'','p');
put string=;
run;
It returns ABCDEF1234
Example 4 : Keep Integer Values from String
data _null_;
x='ABCDEF-!1.234';
string=compress(x,'0123456789.','k');
put string=;
run;
It returns 1.234
4. LEFT Function

The LEFT function moves leading blanks to the end of the value. The length of the string does not change.

Data char1;
Set char;
char1 = left(Name);
run;
5. TRIM Function

The TRIM function removes trailing spaces.

Data char1;
Set char;
char1 = trim(Name);
run;
6. TRIM(LEFT(string))

It is equivalent to STRIP function. It first removes leading spaces and then trailing spaces.

7. CAT Function

The CAT function concatenates character strings. It is equivalent to || sign.

data _null_;
a = 'abc';
b = 'xyz';
c= a || b;
d= cat(a,b);
put c= d =;
run;

Both c and d returns "abcxyz".

Concatenate String and Numeric Value
data _null_;
x = "Temp";
y = 22;
z = x||y;z1 = cats(x,y);z2 = catx("",x,y);
put z = z1= z2 =;
run;
z = Temp   22
z1=Temp22
z2=Temp 22
Note -
  1. The || keyword inserts multiple spaces when numeric and text values are concatenated.
  2. CATS strips both leading and trailing blanks, and does not insert separators.
  3. CATX strips both leading and trailing blanks, and inserts separators. The first argument to CATX specifies the separator.
8. SCAN Function

The SCAN Function extracts words within a value that is marked by delimiters.

SCAN( text, nth word, <delimiters>)
For example :

We wish to extract first word in a sentence 'Hi, How are you doing?'. In this case, delimiter is a blank.

data _null_;
string='Hi, How are you doing?';
first_word=scan(string, 1, ' ' );
put first_word =;
run;
first_word returns 'Hi,' since it is the first word in the above sentence using blank as a delimiter. We wish to extract last word in a sentence 'Hi, How are you doing?'. In this case, delimiter is a blank.
data _null_;
string='Hi, How are you doing?';
last_word=scan(string, -1, ' ' );
put last_word =;
run;

last_word returns 'doing?' since it is the last word in the above sentence.

Let's make it a little complicated.

Suppose, delimiter is a character instead of blank or special sign.

string='Hello SAS community people';
beginning= scan( string, 1, 'S' ); ** returns "Hello ";
middle = scan( string, 2, 'S' ); ** returns "A";
end= scan( string, 3, 'S' ); **returns " community people";
9. SUBSTR Function

The SUBSTR function extracts strings based on character position and length. It is equivalent to MS Excel's MID Function.

= substr(old_var, starting_position, number of characters to keep);

Examples :

data _null_;
t="AFHood Analytics Group";
new_var=substr(t,8,9);
put new_var =;
run;

Result: new_var=Analytics

10. SUBSTR(Left of =) Function

It replaces a portion of string with new string

data _null_;
string='old_variable';
substr(string,1,8) = "new_data";
put string =;
run;

Result: string=new_dataable

In this case, SUBSTR replaces the first 8 characters of string with the value "new_data". After this line executes, the value of string will be 'new_dataable'

11. LOWCASE, UPCASE and PROPCASE

LOWCASE converts the character string to lowercase.

UPCASE converts the character string to uppercase.

PROPCASE returns the word having uppercase in the first letter and lowercase in the rest of the letter (sentence format).

data _null_;
  name = 'Hello world';
  name_upper = upcase(name);
  name_lower = lowcase(name);
  name_proper = propcase(name);  
  put name_upper=;
  put name_lower=;
  put name_proper=;  
run;
Output: name_upper=HELLO WORLD name_lower=hello world name_proper=Hello World
12. INDEX Function

The INDEX function finds characters or words in a character variable

data _null_;
string='Hi,How are you doing?';
x = index(string, "How");
put x=;
run;

x returns 4 as "How" starts from 4th character.

To select all the records having 'ian' in their character.

if index(name,'ian') > 0;

To select all the records having first letter 'H'

if name =: 'H';
13. FIND Function

The FIND function locates a substring within a string

FIND(character-value, find-string <,'modifiers'> <,start>)

STRING1 = "Hello hello goodbye"

Examples :

1. FIND(STRING1, "hello") returns 7

2. FIND("abcxyzabc","abc",4) 7

14. TRANWRD Function

The TRANWRD function replaces all occurrences of a word in a character string. It doesn't replace full phrase (entire value content).

TRANWRD (variable name, find what , replace with)

Example : name : Mrs. Joan Smith

name=tranwrd(name, "Mrs.", "Ms.");

Result : Ms. Joan Smith

15. TRANSLATE Function

The TRANSLATE function replaces specific characters in a character expression.

TRANSLATE(source, replace with, find what)

Example : x = translate('XYZW','AB','VW');

Result : "XYZB"

Difference between TRANWRD and TRANSLATE Functions

The TRANSLATE function converts every occurrence of a user-supplied character to another character. TRANSLATE can scan for more than one character in a single call. In doing this, however, TRANSLATE searches for every occurrence of any of the individual characters within a string. That is, if any letter (or character) in the target string is found in the source string, it is replaced with the corresponding letter (or character) in the replacement string.

The TRANWRD function differs from TRANSLATE in that it scans for words (or patterns of characters) and replaces those words with a second word (or pattern of characters).

16. PRXMATCH

The PRXMATCH function can be used for the following cases :

  1. When you want to identify if there is alphanumeric (has any letter from A to Z) in a variable.
  2. If you need to search a character variable for multiple different substrings.
PRXMATCH (perl-regular-expression, source);
Perl Regular Expression
  1. ^ - start with
  2. $ - end with
  3. \D - any non digits
  4. \d - digits
  5. ? - may or may not have?
  6. | - or
  7. * - repeating
  8. ( i:) - turns ON the case insensitive search
  9. (-i:) - turn OFF the case insensitive search
1. Check alphanumeric value
DATA test;
INPUT string $ 1-8;
prxmatch=prxmatch("/[a-zA-Z]/",string);
CARDS;
ACBED
11
12
zx
11 2c
abc123
;
run; 

Note : prxmatch("/[a-zA-Z]/",string) checks first character.

2. Replace multiple words with a new word

if prxmatch('/Luthir|Luthr|Luther/',name) then name='Luthra' ;
17. INPUT and PUT Function

The INPUT Function is used to convert character variable to numeric.

new_num=input(character-variable, 4.);
Example -
data temp;
x = '12345';
new_x = input(x,5.);
run;

In the above example, the variable x is a character variable as it is defined in quotes '12345'. The newly created variable new_x is in numeric format.

The PUT Function is used to convert numeric variable to character.

new_char=put(numeric,4.0);
data temp;
x = 12345;
new_x = put(x,5.);
run;

In this example, the variable x is originally in numeric format and later converted to character in the new variable new_x.

18. LENGTH

The LENGTH function returns length of a string.

data _null_;
x='ABCDEF-!1.234';
n= length(x);
put n=;
run;

It returns 13.

If you need to calculate the number of digits in a numeric variable -

First, we need to convert our numeric variable to character to count the number of digits as LENGTH function works only for character variable.

data _null_;
x = 12345;
cnt = length(strip(put(x,12.)));
put cnt=;
run;

In the above nested function, we first converted the variable x to character and then remove spaces by using STRIP function and then count number of digits by using LENGTH() function.

Another Method -
data _null_;
x = 12345;
cnt = int(log10(x)) + 1;
put cnt=;
run;

We can also use LOG10 function to solve it. LOG10 has a property which says :

Number of Digits = Integer value of [LOG10(x)] + 1. For example, LOG10(100) = 2 so Number of digits in 100 = 2 +1. See LOG10(1100) = 3.04 => INT(3.04) = 3 => 3+1 = Number of digits in 1100.

19. IF THEN

IF THEN replaces the entire phrase in a string.

data mydata;
input names $30.;
cards;
Raj Gates
Allen Lee
Dave Sandy
William Gates
Jon Jedi
;
run;
data mydata2;
set mydata;
length new_names $30.;
if find(names, "Raj") then new_names = "Raj Kumar";
else new_names = names;
run;

In the above SAS program we are checking if the string "Raj" is found within the variable "names". The FIND function is used to perform this search. If the string "Raj" is found, the if block is executed, and the value "Raj Kumar" is assigned to the "new_names" variable in the mydata2 dataset. If the string "Raj" is not found in the names variable, the else block is executed, and the value of the "names" variable is assigned to the "new_names" variable.

20. COUNT Function

The COUNT function counts the number of times that a specified substring appears within a character string.

data _null_;
name = "DeepAnshu Bhalla";
x = count(name,"a");
x1 = count(name,"a","i");
put x= x1=;
run;

Result : x=2 as there are 2 lower case 'a's in the variable name. x1=3 as there are 3 'a's in the variable name (The 'i' modifier ignores case sensitivity)

21. COUNTW Function

The COUNTW function counts the number of words in a character string.

data readin;
input name$15.;
cards;
Trait Jhonson
3+3=6
;
run;

data out;
set readin;
x = countw(name);
x1 = countw(name,' ');
proc print;
run;
COUNTW Function
Output : COUNTW Function

If you don't specify delimiter in the second parameter of COUNTW function, the function will use the default delimiter, which is a blank space.

Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

59 Responses to "SAS : Character Functions"
  1. Hi ,I am unable to understand this concept.
    There is no Delimiter specified other than blank in the specified string .
    Question is why we are using 'S' in the delimiter field.
    ===============================================================
    Let's make it a little complicated.

    Suppose, delimiter is a character instead of blank or special sign.
    string='Hello SAS community people';
    beginning= scan( string, 1, 'S' ); ** returns "Hello ";
    middle = scan( string, 2, 'S' ); ** returns "A";
    end= scan( string, 3, 'S' ); **returns " community people";

    ReplyDelete
    Replies
    1. We can use any character as a delimeter.
      If we specify 'S' as a delimeter, SAS would read it like -

      First Word - Hello Second Word - A Third Word - community people

      Delete
    2. third word is "community" only correct ?

      Delete
    3. here "S" acting as a delimiter so the third word will whole "community people".

      Delete
    4. if we give "S" as delimiter how it will take third word as community people can you explain in brief

      Delete
    5. because you are considering blank also....whereas the delimiter here is 'S' and not space. So it will consider 'community people' as one word not two.

      Delete
    6. Here "S" acting as delimiter
      So obviously it will be
      Hello "S" A "S" community people

      Delete
    7. Can we give any other delimiter other than "s"

      Delete
  2. Deepanshu, I simply love reading your posts, in fact I just passed Base SAS Certification, and I would give a great deal of credit to you for that. Extremely grateful for providing us such a useful website. Have a small request, is it possible for you to provide PDF format for SAS interview questions ? Regards,

    ReplyDelete
    Replies
    1. Glad you found posts useful and congratulations for clearing Base SAS Certification. Cheers!

      Delete
  3. IN SAS University Edition the space between 2 words is always single space. thus there is no point of using COMPBL.

    ReplyDelete
    Replies
    1. In real-world datasets, we generally encounter a case wherein we need to remove extra space within the text. In corporate, you would not use SAS University Edition. You would rather use enterprise version of SAS or SAS EG to perform data cleaning.

      Delete
  4. Hi Deepanshu!
    I have collected data on change in educational practices using a questionaire and wish to do the analysis. What all types of analysis can be done using SAS?

    ReplyDelete
    Replies
    1. SAS supports many survey analysis such as Crosstabs, Linear Regression, Logistic Regression, Cox-Regression Analysis for survey data. You can explore PROC SURVEYLOGISTIC, PROC SURVEYREG, PROC SURVEYPHREG, PROC SURVEYSELECT etc

      Delete
  5. Hi Deepanshu!
    Need a favor from you. Can you suggest some topics for last year project that uses SAS Analytics. Thnaks in advance!

    ReplyDelete
  6. Hi

    What is the difference between strip and compress function please ?

    regards

    ReplyDelete
    Replies
    1. STRIP removes leading and trailing spaces. Whereas, COMPRESS removes leading, within text spaces and trailing spaces. Check the article above for examples.

      Delete
  7. Hi Deepanshu.
    need a favour could you also place the pharma data and programs(TLF's) which will help full for pharma ppl also.

    ReplyDelete
  8. Hi Deepanshu,

    Very nice post. I am stuck with a problem. I have a file with a column that contains the following types of strings:

    Word1 Word2 ABCD XYZ. or
    Word1 ABC XYZ. or
    Word1 AB XYZ.

    What I need to do is to separate strings ABCD XYZ, ABC XYZ and
    AB XYZ and put them in separate column. So my final result would be 2 columns:

    COLUMN1| COLUMN2
    Word1 Word2 | ABCD XYZ
    Word1 |ABC XYZ
    Word1 |AB XYZ

    I tried to combine reverse and substr functions to separate strings into two columns but was not able to do because length of the string ABCD XYZ is 8, length of ABC XYZ is 7 and of AB XYZ is 6.

    Would you be able to tell me how would you separate these strings into two columns?

    Thanks

    ReplyDelete
    Replies
    1. try this :

      e=substr(a,length(a)-4,5);

      change the digits n variable name as per your requirement

      Delete
    2. can you explain in detail .

      Delete

    3. DATA X;
      STR="ABCDXYZ";
      RUN;

      DATA ABC;
      SET X;
      COLUMN2= SUBSTR(STR,1,2)||SUBSTR(STR,5,3);
      COLUMN1= SUBSTR(STR,1,7);
      WORD1= SUBSTR(STR,1,4);
      WORD2= SUBSTR(STR,5,3);

      RUN;

      PROC TRANSPOSE DATA = ABC OUT=KK;
      VAR COLUMN1 COLUMN2 WORD1 WORD2;
      QUIT;

      Delete
  9. Hi Deepanshu, Many thanks for this info. It is simple and easy to understand.

    I have learned base SAS. Can you please guide me how to prepare and clear base SAS exam.

    Your suggestions are utmost important to me.

    Thanks
    Naresh

    ReplyDelete
  10. Thank you for the great post. The examples were really helpful in clarifying. I have my base sas cert and looking for work, any suggestions?

    thanks
    Charles

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. data t;
    input a $200.;
    cards;
    red blood cells(rbc)
    white blood cells(wbc)
    ;
    run;

    I need output as
    Red Blood Cells(RBC)
    White Blood Cells(WBC)

    How to write the code for this output?
    Thanks in Advance

    ReplyDelete
    Replies
    1. data p;
      set t;

      b=scan(a,1,'');
      c=scan(a,2,'');
      d=scan(a,3,'( ') ;
      e=substr(a,length(a)-4,5);
      f=propcase(b);
      g=propcase(c);
      h=propcase(d);
      i=upcase(e);
      j=compbl(f||(g||h));
      k=cats(j,i);
      drop b c d e f g h i j;
      run;

      Delete
    2. This comment has been removed by the author.

      Delete
    3. DATA D;
      SET T;
      NE=PROPCASE(scan(a,1));
      d=propcase(scan(a,2));
      e=propcase(scan(a,3));
      cb=substr(a,length(a)-4,5);
      cv=upcase(cb);
      full=catx(" ",ne,d,e,cv);
      keep full;
      run;

      Delete
    4. data t2;
      set t;
      a=propcase(a);
      a=tranwrd(a,'bc)','BC)');
      run;

      Delete
    5. DATA W;
      SET t;
      x=propcase(scan(a,1,'('));
      y=upcase(substr(a,index(a,'('),5));
      z=cats(x,y);
      drop x y;
      run;

      Delete
  13. data p;
    set t;

    b=scan(a,1,'');
    c=scan(a,2,'');
    d=scan(a,3,'( ') ;
    e=substr(a,length(a)-4,5);
    f=propcase(b);
    g=propcase(c);
    h=propcase(d);
    i=upcase(e);
    j=compbl(f||(g||h));
    k=cats(j,i);
    drop b c d e f g h i j;
    run;

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. data asd;
    set t;

    a=propcase(scan(a,1,''));
    b=propcase(scan(a,2,''));
    c=scan(a,3,'');
    e=find(c,'(');
    f=propcase(substr(c,1,e-1))||upcase(substr(c,e,length(c)-1));

    n=catx("",a,b);
    m=catx("",n,f);
    keep a m;
    run;

    ReplyDelete
  16. Hi All,

    Any idea on : "The || keyword inserts multiple spaces when numeric and text values are concatenated." How many spaces are being inserted in between Temp and 22 in this example in case of cat()?

    Also, can someone please share the default size of string value and numeric value in sas?

    ReplyDelete
  17. data _null_;
    string='Hi, How are you doing?';
    first_word=scan(string, 1, ' ' );
    put first_word =;
    run;

    Should return "Hi," instead of Hi.

    Thank you.

    ReplyDelete
    Replies
    1. Hi, please check the below code for "Hi," result.
      data _null;
      string='Hi, How are you doing?';
      first_word=quote(scan(string, 1,' '));
      *quote is used to give quotes for varaible;
      put first_word =;
      run;

      Delete
  18. data names;
    infile datalines;
    input name $;

    datalines;
    arun krishna
    gopal rao
    venu vardhan reddy
    rames krishna rao sunkara
    kiran
    ;
    run;

    I want to 3 more variables like
    first name
    middle name
    sir name

    if the variable does not have the middle name or last name then it should be blank please can you tell me the how to solve that particular problem

    ReplyDelete
    Replies
    1. data na (keep=name firstname middlename lastname);
      set names ;
      firstname=scan(name,1);
      y=scan(name,2);
      z=scan(name,3);
      r=scan(name,4);
      q=catx(' ',y,z,r);
      lastname=scan(q,-1);
      s1=scan(q,-2);
      s2=scan(q,-3);
      middlename=catx(' ',s1,s2);
      run;

      Delete
  19. Thanks for excellent materials .
    question 1.Left function will not remove leading spaces.it will move the content to left and spaces will be moved to right.but your description seems to be wrong.

    ReplyDelete
  20. data _null_;
    x = 12345.6;
    cnt = length(strip(put(x,12.)));
    put cnt=;
    run;

    should return 6 but its returning 7,please help

    ReplyDelete
    Replies
    1. data _null_;
      x = 12345;
      cnt = length(strip(put(x,12.1)));
      put cnt=;
      run;

      should return 6 but its returning 7,please help

      prev post is incorrect sorry

      Delete
    2. in this case the value of x is "12345.0" , so the length is 7.

      Delete
  21. how to find second occurrence of a word or letter in a string.kindly explain

    ReplyDelete
  22. Hello Deepanshu,

    data readin;
    input name$15.;
    cards;
    Trait Jhonson
    3+3=6
    ;
    run;

    data out;
    set readin;
    x = countw(name);
    x1 = countw(name,' ');
    proc print;
    run;

    Output : COUNTW Function


    In this example involving 3+3=6, how x gives 2 as the number of words? Are + or = consider as special characters?

    ReplyDelete
  23. Hi,

    If x="Ramachandra";

    In this character Value, I need to output only "r" and "R" Like x1="Rr" .

    Do we have any particular function to achieve this ?!

    ReplyDelete
    Replies
    1. data g;
      x="Ramachandra";
      x1=compress(x,'Rr','k');
      run;

      Delete
  24. Hi,

    If x="Ramachandra";

    In this character Value, I need to output only "r" and "R" Like x1="Rr" .

    Do we have any particular function to achieve this ?!

    ReplyDelete
    Replies
    1. Hi Harish,

      Yes we have compress function with modifier. Please check the below code.

      data temp2;
      x="Ramachandra";
      y=compress(x,'R''r','k');
      run;

      Delete
    2. data p1;
      name='Ramchandra';
      p2=compress(name,'amchnd');
      run;

      Delete
  25. Hi

    You are doing a great job. Thanks.
    Please would you be able to explain the round function.

    ReplyDelete
  26. Great Work sir, keep posting always.
    Thank for creating listen data.

    ReplyDelete
  27. data qwer;
    input name $18.;
    datalines;
    imran khan
    mohsin uzzama
    mohammed oviz khan
    ;
    run;

    output should be like this ;-

    NAME Fname Lname MailId
    imran khan imran khan imran.khan@gmail.com
    mohsin uzzama mohsin uzzama mohsin.uzzama@gmail.com
    mohammed oviz khan mohammed oviz khan mohammed.oviz.khan@gmail.com

    ReplyDelete
  28. Hi, Please check the below code.
    data temp;
    set qwer;
    Fname = scan(name,1,' ');
    Lname= scan(name,-2,"");
    mailid= tranwrd(cats(name,"@gmail.com"), " ",".");
    run;

    ReplyDelete
  29. data t;
    input a $200.;
    cards;
    red blood cells(rbc)
    white blood cells(wbc)
    ;
    run;

    I need output as
    Red Blood Cells(RBC)
    White Blood Cells(WBC)

    How to write the code for this output?
    Thanks in Advance

    Ans:
    HI the below code will give the result as WBC AND RBC.
    data t2;
    set t;
    b= (scan(a,-1,'('));
    b= tranwrd(b,')',' ');
    b=upcase(b);
    run;

    Thanks.

    ReplyDelete
Next → ← Prev