Re: st: creating a new variable?

 From Jeph Herrin To statalist@hsphsun2.harvard.edu Subject Re: st: creating a new variable? Date Fri, 17 Aug 2007 13:05:29 -0400

```Correction:

bys birthstate: egen x=total(total)
replace x=x-total
gen y=x
replace y=y-total if state==birthstate

Jeph

Jeph Herrin wrote:
```
```How about

bys birthstate: gen x=sum(total)
replace x=x-total
gen y=x
replace y=y-total if state==birthstate

hth,
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 2 5 3+1+0 1+0
2 2 3 5+1+0 5+1+0 3 2 1 0+5+3 5+0
4 2 0 5+3+1 5+1 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.
Make any sense? I assume I need a do loop, just not very familiar with them. Any help would be greatly appreciated! Thanks,
Marilyn. *
* 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/

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

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