Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Count Special Character


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Count Special Character
Date   Sun, 27 Feb 2005 17:07:56 -0000

In addition, note also -split-. 

-split- doesn't help directly with
counting, but the splitting might be 
useful anyway. And the count is
possible as a by-product. 

split name, p(/) 

gen numnames = 0 
foreach v of var `r(varlist)' { 
	replace numnames = numnames + 1 if !mi(`v') 
} 

Nick 
n.j.cox@durham.ac.uk 

Fred Wolfe
 
> As usual, in Stata there are a number of different ways to do 
> this. Here is 
> a quick (and dirty) way to do it.
> 
>   set obs 1
> obs was 0, now 1
> 
> . gen names = "smith/jones/la bush"
> 
> . gen b = subinstr(names," ","_",.)
> 
> . l names
> 
>       +---------------------+
>       |               names |
>       |---------------------|
>    1. | smith/jones/la bush |
>       +---------------------+
> 
> . l b
> 
>       +---------------------+
>       |                   b |
>       |---------------------|
>    1. | smith/jones/la_bush |
>       +---------------------+
> 
> . replace b = subinstr(b,"/"," ",.)
> (1 real change made)
> 
> . l names b
> 
>       +-------------------------------------------+
>       |               names                     b |
>       |-------------------------------------------|
>    1. | smith/jones/la bush   smith jones la_bush |
>       +-------------------------------------------+
> 
> . gen wcount = wordcount(b)
> 
> . l names b wcount
> 
>       +----------------------------------------------------+
>       |               names                     b   wcount |
>       |----------------------------------------------------|
>    1. | smith/jones/la bush   smith jones la_bush        3 |
>       +----------------------------------------------------+
> 
> drop b
 
Eugene Kang

> >I have a variable that captures individual names in my 
> dataset. If there
> >is more than 1 individual, then the names are separated by a /. For
> >instance, there are 2 individuals if the variable shows
> >riedel/baysinger, while there are 3 individuals if the variable shows
> >riedel/baysinger/hitt.
> >
> >I need to create a new variable that captures the number of 
> individuals
> >for an observation. Does STATA allow me to count the number of / in a
> >variable? In the above example, 1 / would indicate 2 individuals for
> >that observation, while 2 / will indicate 3 individuals, and so on. I
> >could count the number of / manually, but I have close to 20,000
> >observations in the dataset!!

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



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