Newsgroups: bit.listserv.sas-l
Date:         Tue, 14 Apr 1992 14:10:27 EDT
Reply-To:     Lawrence Muhlbaier <lhm@BIOSTAT.MC.DUKE.EDU>
From:         Lawrence Muhlbaier <lhm@BIOSTAT.MC.DUKE.EDU>
Subject:      Re: upper case conversion
Comments: To: sas-l%vtvm2.BITNET@ncsuvm.ncsu.edu

Rajpal asks:

 I have a simple (maybe!) problem.
> I am trying to convert following 'upper case' address value to
> the correct form.
>
> input:  1983 MARGARET LANE
>
> what I want is: 1983 Margaret Lane
>
> any simple method?

Several approaches dealing with the translate function have been
suggested.  They all fail with a variety of names (McDonald, O'Connor,
III, etc).  Some time ago, I wrote a macro to do just that (originally
in the SUGI 9 proceedings).  For Rajpal's needs, it just lacks the
state (non)conversion.

Lawrence H. ('Doc') Muhlbaier		muhlb001@dukemc.mc.duke.edu
Assistant Research Professor		muhlb001@dukemc
Duke University Medical Center		919-286-3322 (office)
DUMC 3865 				919-286-9534 (home)
Durham, NC 27710-7510			919-286-2947 (FAX)
---------cut here-----------------
 /*  UPFIRST -- SAS macro procedure:  Capitalize first letter in
                                      each word in a string.

Macro program to capitalize the first letter in each "word" in a
string.  The program changes the string that is its argument.
The macro program was designed primarily for converting the all
uppercase names and addresses that are commonly seen in computer files
and converting them into upper and lower case.  It uses a generous
definition of a word, allowing separation by blanks, hypens, periods,
and apostrophies.  This is to handle the variety of capitalization
seen in names and degrees.  As a special case, it also capitalizes
the letter after "Mc" and "Mac", and records " III" and " II" correctly.

Usage:        %upfirst(string);
              DROP _l_ _j_ _char;     * Do once only in the DATA step;

Example:   DATA; INPUT string $CHAR70.;
           PUT string; %upfirst(string); PUT string;
           CARDS;
           JOHN DOE   KEVIN O'BRIEN   DICK mcduff, iii
           ;
   yields
           John Doe   Kevin O'Brien   Dick McDuff, III

Author:    Doc Muhlbaier
Date:      12 March 1987, based on %UPONE by Muhlbaier & Stafford
                          SUGI 9 Proceedings, 1984, pp 613-614.
Modified:  18Mar87        Bug fix.  First character of word not
                          changed; only rest changed to lower case.
           29Mar87        DROP un-needed working variables.
           26May87        Removed DROP stmt.  Caused bomb if UPFIRST
                          used more than once in a DATA step.
                          Add II & III as special cases.
                                                                  */
%macro UPFIRST(upstring);
_l_=length(&upstring);
substr(&upstring,1,1) = upcase(substr(&upstring,1,1));
do _j_=2 to _l_;
     _char=substr(&upstring,_j_-1,1);  /* previous character */
     if _char=' '|_char='.'|_char=','|_char='-'|_char='''' then
          substr(&upstring,_j_,1) =
          upcase(substr(&upstring,_j_,1));
     else substr(&upstring,_j_,1) =
          translate(substr(&upstring,_j_,1),
          'abcdefghijklmnopqrstuvwxyz',
          'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
     if (_j_<_l_) then do;
          if (_j_>=3) then if (substr(&upstring,_j_-2,2)='Mc')
               then substr(&upstring,_j_,1) =
                    upcase(substr(&upstring,_j_,1));
          if (_j_>=4) then if (substr(&upstring,_j_-3,3)='Mac')
               then substr(&upstring,_j_,1) =
                    upcase(substr(&upstring,_j_,1));
          end;
end;
IF INDEX(&upstring,' Iii')>0 THEN
  SUBSTR(&upstring,INDEX(&upstring,' Iii'),4)=' III';
IF INDEX(&upstring,' Ii')>0 THEN
  SUBSTR(&upstring,INDEX(&upstring,' Ii'),3)=' II';
%mend;
Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU