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

st: RE: Spilt a variable into several parts


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Spilt a variable into several parts
Date   Tue, 1 Apr 2003 10:41:41 +0100

Kate wrote 
> 
> Somebody sent me a dataset with variables like this
> one. There were 5 procedures and more than one could
> be chosen.  Instead of making each a variable, a
> single variable was made with commas separating the
> procedure number.
> 
>   procedure |      Freq.
> ------------+-----------
>    01,02,03 |         11
> 01,02,03,04 |          1
>          02 |          6
>       02,03 |        107
>    02,03,04 |         14
>       02,04 |          1
>          03 |          3
>       03,04 |          2
>          04 |          2
>          05 |          6
> 
> I want to make 5 variables that record yes or no for
> each procedure. Index() seems to be the way to go but 
> but I don't see how to use it when there's more than
> one instance of the index character. Can somebody
> point me in the direction?

James Hesford suggested 

> In Stata, type "findit split"

> You will find a FAQ by Nick Cox on splitting a string variable into
> parts. The FAQ can also be found at
> http://www.stata.com/support/faq/data/splitstr.html

That should be 

http://www.stata.com/support/faqs/data/splitstr.html

For Stata 8 this FAQ is superseded by the -split- command. 
I will revise the FAQ ASAP. However, this isn't the answer 
to Kate's question, as -split- will not create the 
indicator variables Kate needs. 

Shannon Driver suggested  

> You might try something like the following:

>     forvalues i = 1/5 {
>          gen proc0`i' = cond(index(procedure,"0`i'"),1,0)
>     }

which does seem the right way to go. Note in passing that 

cond(index(procedure,"0`i'"),1,0) 

could also be 

index(procedure,"0`i'") > 0 

There is a more general FAQ on multiple 
responses by Ulrich Kohler and myself at 

http://www.stata.com/support/faqs/data/multresp.html

which in turn has been revised and expanded 
in Stata Journal 3(1), 81-99 (2003).  

Nick 
[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