|
Jie,
The mistake in your program was the failure to trim name in the loop.
Replace
index=index(name,' ');
with
index=index(trim(name),' ');
and your program works. Remember that SAS is a fixed length string
language. One cost is the need for extra TRIMs in many expressions.
However, as others have pointed out a more efficent way is a direct
calculation:
count = length(compl(name)) - length(compress(name)) ;
IanWhitlock@westat.com
-----Original Message-----
From: Jie Qin [mailto:c674010@YAHOO.COM]
Sent: Thursday, March 21, 2002 3:50 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: How to get a count of space in a full name?
I want to know how many spaces in a full name which is
composed by first name, middle name, last name and
suffix and separated by space. Here is my raw data and
program:
raw data:
name
DAN SMITH
JACK JOHN SR
WENDY ANN F WILLIAMS
My program:
data one;
set rawdata;
count=0;
do i=1 to length(name);
index=index(name,' ');
if index then count=sum(count,1);
else leave;
name=substr(name,index+1);
end;
run;
proc print;run;
Here is my output:
Obs name count i index
1 9 10 1
2 12 13 1
3 20 21 1
The count is supposed to be:
name count
DAN SMITH 1 (there is 1 space)
JACK JOHN SR 2 (there are 2
spaces)
WENDY ANN F WILLIAMS 3 (there are 3
spaces)
What's wrong with my program? Other better solution?
__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards®
http://movies.yahoo.com/
|