Mingfeng,
Do your strings always look as in your example? Here is one solution
to your problem.
clear
set obs 1
gen x = "johnabc johncd"
split x
forval i = 1/2 {
gen john`i' = regexm(x`i',"john")
}
egen y = rowtotal(john1 john2)
list, noobs
+-------------------------------------------------------+
| x x1 x2 john1 john2 y |
|-------------------------------------------------------|
| johnabc johncd johnabc johncd 1 1 2 |
+-------------------------------------------------------+
Friedrich
On Tue, Nov 4, 2008 at 9:42 PM, Mingfeng Lin <[email protected]> wrote:
> Greetings,
>
> I looked through the list of string functions but couldn't find one
> that fits the bill. Suppose I have a string variable X, and I would
> like to generate a new numeric variable Y containing the number of
> times a certain string appeared in X. For instance
>
> X = "johnabc johncd"
>
> If I'd like to find the number of times "john" shows up in X, I hope to obtain
> Y = 2
>
> Is there a function in Stata to do this?
>
> Thank you very much for your help!
>
> Mingfeng
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/