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

From |
Phil Schumm <pschumm@uchicago.edu> |

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 02:05:31 -0600 |

On Nov 4, 2008, at 8:42 PM, Mingfeng Lin wrote:

I looked through the list of string functions but couldn't find onethat fits the bill. Suppose I have a string variable X, and I wouldlike to generate a new numeric variable Y containing the number oftimes a certain string appeared in X. For instanceX = "johnabc johncd"If I'd like to find the number of times "john" shows up in X, I hopeto obtain Y = 2Is there a function in Stata to do this?

tempvar t1 t2 gen `t1' = X gen `t2' = X gen Y = 0 qui while 1 { replace `t1' = subinstr(`t1', "john", "", 1) cap ass `t1'==`t2' if _rc { replace Y = Y + (`t1'!=`t2') replace `t2' = `t1' } else continue, br }

-- Phil * * 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/

**Follow-Ups**:**RE: st: counting the number of times a string appears in a string variable?***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**References**:**st: counting the number of times a string appears in a string variable?***From:*"Mingfeng Lin" <mingfeng.lin@gmail.com>

- Prev by Date:
**st: Reshape limit** - Next by Date:
**Re: st: Reshape limit** - Previous by thread:
**Re: st: counting the number of times a string appears in a string variable?** - Next by thread:
**RE: st: counting the number of times a string appears in a string variable?** - Index(es):

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