> -----Original Message-----
> From: Lee Chuntao [mailto:[email protected]]
> Sent: Thursday, October 10, 2002 9:15 AM
> To: [email protected]
> Subject: st: string function
>
>
> Hi, listers
>
> A piece of cake for you all but urgent for me. I wonder
> if there is
> some function that can find how many times a character
> occured in a string,
> i.e., suppose the function is string1(), so that,
> string1("abcd","a" )=1
> string1("abac","a" )=2
> string1("a/b/c","/" )=2
> string1("abcd","/" )=0
>
> thanks in advance
>
> Chuntao
If you have the string in a local macro, you can use the macro extended
function subinstr to get this:
local x "a/b/cdef/ghi"
local x : subinstr local x "/" "/" , all count(local n)
display `n'
How this works: The construct
local x : subinstr local y "from" "to" , all count(local n)
will take the local macro -y-, change occurrences of the string "from"
to the string "to", put the result in the macro -x-, and will put the
count of the number of changes in the local macro -n-.
Since you don't want to change your string--you only want the count--the
construction I listed above will take the local -x-, replace all the
slashes slashes, and put the result back in local -x-. In other words,
it will not change x. But it will put the count you want in the local
-n-.
If you have a string variable, and you want to count the number of times
your target string appears in each observation, then the following
-egen- program will do the trick. Save the code below as _gnoccur.ado
on your adopath. Syntax is:
egen newvar = noccur(stringvar) , String("/")
--Nick Winter
**** BEGIN _gnoccur.ado
program define _gnoccur
version 7
gettoken type 0 : 0
gettoken g 0 : 0
gettoken eqs 0 : 0
syntax varlist(min=1 max=1 string) [if] [in] , String(string)
local size = length(`"`string'"')
tempvar new pos count
qui {
gen str1 `new'=""
replace `new'=`varlist'
gen `count'=0
gen `pos' = index(`new',`"`string'"')
capture assert `pos'==0
while _rc {
replace `count'=`count'+(`pos'!=0)
replace `new'=substr(`new',`pos'+`size',.)
replace `pos' = index(`new',`"`string'"')
capture assert `pos'==0
}
gen `type' `g' = `count' `if' `in'
}
end
********** END OF _gnoccur.ado
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/