Stata. How to split the observation?

Some observations in my dataset need to be divided into two or three differential observations. For example, the following observation:

region income gdp other North 120 450 50 

I need to break it down into three observations with the same values for all variables, except for an area 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 this possible with Stata?

+4
source share
1 answer

It is difficult to solve the general problem here from your example. note that

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

not working as you expect as it is equivalent

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

and is a branch of a U-turn. This is described at http://www.stata.com/support/faqs/programming/if-command-versus-if-qualifier/

It is legal:

 expand 3 if region == "North" 

but you will need to follow the replacement one by one.

(LATER) The wild hunch is that you are following Stata. How to compare the values โ€‹โ€‹in the ratio 1: m? and try to reinvent the merge . All I can say is 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 
+4
source

Source: https://habr.com/ru/post/1485259/


All Articles