LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (March 2002, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Fri, 22 Mar 2002 08:52:32 -0500
Reply-To:   Ian Whitlock <WHITLOI1@WESTAT.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Ian Whitlock <WHITLOI1@WESTAT.COM>
Subject:   Re: How to get a count of space in a full name?
Comments:   To: Jie Qin <c674010@YAHOO.COM>
Content-Type:   text/plain; charset="iso-8859-1"

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/


Back to: Top of message | Previous page | Main SAS-L page