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 (November 2001, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 2 Nov 2001 17:15:17 -0500
Reply-To:     Edward Heaton <HEATONE@WESTAT.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Edward Heaton <HEATONE@WESTAT.COM>
Subject:      Re: Simple Base SAS Qn.
Comments: To: Jeff Morison <jmt_mtf@YAHOO.COM>
Content-Type: text/plain; charset="iso-8859-1"

Jeff;

There are a couple of solutions to your problem. You might try ...

Data _null_ ; Input x $36. ; If index(x,"Y") then numberOfYs = length( compress( x , "N" ) ) ; Else numberOfYs = 0 ; Put x "has " numberOfYs "Ys." ; Cards4 ; NNNNYNNNNNNYYYNYYYNNYNNYNNNNYYYNNYYY NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY ;;;;

which produces

1 Data _null_ ; 2 Input x $36. ; 3 If index(x,"Y") 4 then numberOfYs = length( compress( x , "N" ) ) ; 5 Else numberOfYs = 0 ; 6 Put x "has " numberOfYs "Ys." ; 7 Cards4 ;

NNNNYNNNNNNYYYNYYYNNYNNYNNNNYYYNNYYY has 15 Ys. NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN has 0 Ys. YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY has 36 Ys. NOTE: DATA statement used: real time 0.26 seconds Memory 136k

11 ;;;;

This works because your example has only Ns and Ys. Suppose you had lots of different characters in variable length strings. Then you might try ...

Data _null_ ; Input x $36. ; numberOfYs = 0 ; Do i=1 to length(x) ; If ( subStr(x,i,1) eq "Y" ) then numberOfYs = numberOfYs + 1 ; End ; Put x "has " numberOfYs "Ys." ; Cards4 ; NNNNYNNNNNNYYYNYYYNNYNNYNNNNYYYNNYYY NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY ;;;;

It works as well; I'll leave it up to you to test this code and check the log.

Hope this helps, Ed

Edward Heaton, Senior Systems Analyst, Westat (An Employee-Owned Research Corporation), 1550 Research Boulevard, Room 2018, Rockville, MD 20850-3195 Voice: (301) 610-4818 Fax: (301) 294-3992 mailto:EdwardHeaton@westat.com http://www.westat.com

-----Original Message----- From: Jeff Morison [mailto:jmt_mtf@YAHOO.COM] Sent: Friday, November 02, 2001 4:55 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Simple Base SAS Qn.

Hi Folks:

How to count # of times a particular letter appears in a string.

For Example: I need to count how many Ys are in the string

'NNNNYNNNNNNYYYNYYYNNYNNYNNNNYYYNNYYY'

TIA.

__________________________________________________ Do You Yahoo!? Find a job, post your resume. http://careers.yahoo.com


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