Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: counting the number of times a string appears in a string variable?


From   "Friedrich Huebler" <fhuebler@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: counting the number of times a string appears in a string variable?
Date   Wed, 5 Nov 2008 01:14:36 -0500

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 <mingfeng.lin@gmail.com> 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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index