Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: creating a new variable?


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: creating a new variable?
Date   Fri, 17 Aug 2007 14:42:34 -0400

Marilyn--
There is a relevant FAQ:
http://www.stata.com/support/faqs/data/members.html

I think Jeph's solution does not get the Y you wanted, and will not
work if there is more than one observation per state/birthstate
combination (I'm not clear on whether this is true of your actual data
or not).

You might try this type of approach instead:

clear
input state birthstate total  X  Y
       1        2        5    4  1
       2        2        3    6  6
       3        2        1    8  5
       4        2        0    9  6
       1        3        6    22 13
       2        3        7    21 12
       3        3        9    19 19
       4        3        6    22 13
end
egen bstot=sum(total), by(birthstate)
egen sbstot=sum(total), by(state birthstate)
g x=bstot-sbstot
g teq=total if state==birthstate
egen sametot=sum(teq), by(birthstate)
replace sametot=0 if state==birthstate
g y=bstot-sbstot-sametot
drop bstot sbstot teq sametot
li, noo clean


On 8/17/07, Jeph Herrin <[email protected]> wrote:
> Correction:
>
>   bys birthstate: egen x=total(total)
>   replace x=x-total
>   gen y=x
>   replace y=y-total if state==birthstate
>
>
> Jeph
>
> > Ms. Marilyn Ibarra wrote:
> >> Any help would be greatly appreciated!  Thanks!
> >> I have the following variables: state, born, and total.  I would like
> >> to create two new variables: x and y.
> >> I would like "X" and "Y" to be the sum of Total for certain
> >> observations.  Such as this:
> >> state    birthstate total     "X"          "Y"
> >> 1         3           6      7+9+6        7+6
> >> 2         3           7      6+9+6        6+6
> >> 3         3           9      6+7+6        6+7+6
> >> 4         3           6      6+7+9        6+7
> >>
> >> State is the state they live in now.  Birthstate is the state they
> >> were born in.  For anyone born in state 2, I want "X" to be the sum of
> >> Total for the other states they do not live in.  For example, for
> >> person born in state 2, now living in state 1, I want to sum the Total
> >> for other people born in state 2 who live in state 1, state 3, and
> >> state 4.
> >> To create "Y", for example, for person born in state 2, living in
> >> state 1, I want to sum Total for other persons born in the same state
> >> who now live in state 3,and 4.  I don't want to include for this
> >> person's sum of Total, the combination of state 2 and born in state
> >> 2.  And the same for born in state 3, living in state 1, I want to
> >> exclude born in state 3, living in 3, but include in the sum of the
> >> Total born in state 3, living in state 2 and 4.  For the sum of Total,
> >> for the  observation, born in state 3 living in state 3, I want to
> >> include the sum of Total for anyone else living in another state that
> >> is not state 3, so state 1, 2, and 4 would be included in the sum of
> >> Total for this observation.
>
*
*   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