Date: Fri, 26 May 2006 10:33:35 -0700
Reply-To: "Pardee, Roy" <pardee.r@GHC.ORG>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Pardee, Roy" <pardee.r@GHC.ORG>
Subject: Incrementally add to a character var in a do loop?
Content-Type: text/plain; charset="US-ASCII"
Hey All,
After a collegue hipped me to the new propcase() function in v9 I
thought I'd try porting some ruby code I have for pretty-casing
names/addresses to sas. But I'm stumped early on it seems--I'd like to
loop through the words contained in my char var & then tack them on to
the end of a new var one by one (after making semi-intelligent choices
about which (bits of) each word need to be up- or down- cased). But
when I run this code:
data nom ;
length name $ 35 ;
input name 1-35 ;
datalines ;
ROY EDMUND PARDEE III, ESQ
;
run ;
data gnu ;
length __word pretty_name $ 35 ;
set nom ;
__i = 1 ;
__word = scan(name, __i) ;
pretty_name = propcase(__word) || '~' ;
do while (__word ne "") ;
put __word = ;
pretty_name = pretty_name || propcase(__word) || '~' ;
__i + 1 ;
__word = scan(name, __i) ;
end ;
drop __: ;
run ;
proc print ;
run ;
I wind up w/just "Roy" in pretty_name, even though I can see from the
put statement in my while loop that __word is changing on each
iteration, just as I expected. I don't even see the tilde characters.
Is || not the append operator for strings in sas?
Many thanks for any help!
-Roy
P.S. In case anybody's interested, here's the ruby code:
# These we want to stay upcase.
@abbreviations = %w(PO NE NW SE SW P.O. N.E. N.W. S.E. S.W. C/O P.O)
# Downcase for these ordinal suffixes.
@suffixes = %w(ST ND RD TH)
# Upcase the letter immediately following these.
@prefixes = %w(Mc O' D')
# Dashes, slashes, and other markers of apt numbers & other things
that should be upcased.
@dash_reg = Regexp.new("([#-/\"][A-Za-z])")
# Pretty version of prettify2--by David Black.
def CaseManager.prettify4(address)
address.split(' ').each do |word|
word.capitalize! unless (@abbreviations.include?(word) or
word[0..0] =~ /[0-9#]/ )
word[2..2] = word[2..2].upcase if
@prefixes.include?(word[0..1])
word[-2, 2] = word[-2, 2].downcase if
@suffixes.include?(word[-2,2])
end.join(' ').gsub(@dash_reg) { $1.upcase }
end