CHEBURASHKA
CHEBURASHKA

Reputation: 1713

Stata. How to split an observation?

Some observations in my data set need to be split into two or three differenet observations. For example the following observation:

region  income   gdp   other
North   120      450   50

I need to split it into three observation with the same values for all variables except for the region like this:

region  income   gdp   other
IL      120      450   50
MI      120      450   50
IN      120      450   50

I need something like:

if (region == "North") {
//create three new observations and delete the old one
}

Is it possible with Stata?

Upvotes: 4

Views: 4939

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

It is difficult to work out the general problem here from your example. Note that

if region == "North" { 
      <code>
} 

does not work as you seem to expect, as it is equivalent to

if region[1] == "North" { 
      <code>
} 

and is a once-only branch. This is documented at http://www.stata.com/support/faqs/programming/if-command-versus-if-qualifier/

This is legal:

expand 3 if region == "North" 

but you would need to follow with one-by-one replacements.

(LATER) A wild guess is that you are following on from Stata. How to match values in 1:m relationship? and trying to re-invent merge. All I can say is that would be a major project for an experienced Stata programmer.

(STILL LATER)

 gen long obsid = _n 
 gen state = "" 
 gen isnorth = region == "North" 
 expand 3 if isnorth 
 bysort obsid : replace state = "IL" if isnorth & _n == 1 
 by obsid : replace state = "MI" if isnorth & _n == 2
 by obsid : replace state = "IN" if isnorth & _n == 3  

Upvotes: 4

Related Questions