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

Re: st: Count Special Character


From   Fred Wolfe <[email protected]>
To   [email protected]
Subject   Re: st: Count Special Character
Date   Sat, 26 Feb 2005 17:52:01 -0600

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

Fred Wolfe


At 03:03 PM 2/26/2005, you wrote:

Hi,

I would appreciate some help on the following problem.

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!!

Eugene Kang


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

Fred Wolfe
National Data Bank for Rheumatic Diseases
Wichita, Kansas
Tel (316) 263-2125     Fax (316) 263-0761
[email protected]


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index