www.sas.com > SAS UK > In the Know Homepage Search | Contact Us    
SAS UK Newsletter Banner SAS - The power to know(tm)  

How Do I Capitalize the First Letter of a Word and Lowercase all the Other Letters in a Word?

(Written by Ifor Lewis, HFC Bank Ltd)

By using Macros and the SAS datastep functions UPCASE, LOWCASE and SUBSTR you can convert your text strings to proper case making your text more readable.

This mimics a function which is available in Oracle - INITCAP - to capitalize the first letter of a word and lowercase all the other letters in a word. In my job this is useful, particularly for names and addresses when creating mailing files and sending data outside of our company.

It is designed to run within a dataset and I have included an example dataset and usage example:

%macro initcap(col_name);
     /* Copyright Iffy 2004 */
     do i=1 to length(&col_name) - 1;
          if substr(&col_name,i,1) in (' ','-',"'")
               then substr(&col_name,i+1,1) = upcase(substr(&col_name,i+1,1 ));
          else substr(&col_name,i+1,1) = lowcase(substr(&col_name,i+1,1));
     end;
     substr(&col_name,1,1) = upcase(substr(&col_name,1,1));
     drop i;
%mend;

/* Example dataset */
data temp;
     length firstname $5 fullname $25;
     infile datalines delimiter=',';
     input firstname $ fullname $;
     datalines;
dave,david johnson
john,joHn herrington-PROOPS
STEVE,steven barrington edwards
andY,andrew brown
jo,jo o'brien
;
run;

/* Usage - within a datastep */
data temp;
     set temp;
     %initcap(firstname);
     %initcap(fullname);
run;

In addition to the customer's suggestion, please find below alternative SAS datastep examples when using Base SAS 8.2. If you have the module SAS Data Quality Cleanse licensed in SAS 8.2, you will have access to the function PROPERCASES that automatically does this for you. Beginning with Base SAS 9 there is a new function PROPCASE which will convert all words in an argument to proper case.

http://ftp.sas.com/techsup/download/sample/datastep/char.html

http://www2.sas.com/proceedings/sugi25/25/cc/25p079.pdf