Sample 24576: Capitalize the first letter of every word in a string
Convert a text string into mixed case.
Note:
Beginning in SAS 9.1, this task is easily accomplished
with the PROPCASE function. See Sample 2 on the Full Code tab.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Sample 1: COMPBL, LOWCASE, SCAN, INDEX, UPCASE, SUBSTR */
data sample;
input name $char50.;
/* Lowercase the entire string, remove consecutive blanks */
newname=compbl(lowcase(name));
length next $ 20;
i=0;
next=scan(newname,1,' ');
do while(next ne ' ');
i+1;
/* Scan off each 'word' based upon a space, locate the position */
/* of the first letter in the original string, UPCASE the first */
/* letter and use SUBSTR to replace the byte. */
pos=indexw(newname,trim(next));
substr(newname,pos,1)=upcase(substr(newname,pos,1));
next=scan(newname,i,' ');
end;
keep name newname;
datalines;
Jane DOE
min ning chou
HENRIK HANSSON
D ETCHEVERRY, Charo B
;
proc print;
run;
/* Sample 2: PROPCASE (available in SAS 9.1) */
data sample;
input name $char50.;
mixed=propcase(name," ',");
datalines;
tom o'shea
min ning chou
LENA HANSSON
murder, she wrote
;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Sample 1: COMPBL, LOWCASE, SCAN, INDEX, UPCASE, SUBSTR
Obs name newname
1 Jane DOE Jane Doe
2 min ning chou Min Ning Chou
3 HENRIK HANSSON Henrik Hansson
4 D ETCHEVERRY, Charo B D Etcheverry, Charo B
Sample 2: PROPCASE (available in SAS 9.1)
Obs name mixed
1 tom o'shea Tom O'Shea
2 min ning chou Min Ning Chou
3 LENA HANSSON Lena Hansson
4 murder, she wrote Murder, She Wrote
Convert a text string into mixed case.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Character
|
| Date Modified: | 2006-02-15 03:03:22 |
| Date Created: | 2004-09-30 14:08:55 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |