# Re: st: AW: RE: AW: RE: Re: Generate Variable

 From Erick Guerrero To statalist@hsphsun2.harvard.edu Subject Re: st: AW: RE: AW: RE: Re: Generate Variable Date Thu, 12 Mar 2009 09:34:32 -0500

Thanks Martin and Nick for your polished response - It was very helpful conceptually and practically.
```
```
Indeed, there are some missing values, so I appreciate you anticipating this issue.
```

Erick

Martin Weiss wrote:
```
<>
```" The -bys id- does no harm, but is unnecessary"

Very, very true: I wanted to make sure that the process happens for each id
separately, bu the -egen- statements see to it that the income vectors are
id-specific to start with. Thanks for pointing that out.

So Erick, as you can see from our exchanges, missings in the income or year
variable can spoil your fun in this exercise...

HTH
Martin

-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Nick Cox
Gesendet: Donnerstag, 12. März 2009 14:26
An: statalist@hsphsun2.harvard.edu
Betreff: st: RE: AW: RE: Re: Generate Variable

```
In Martin's second last line:
```bys id: g byte higher83=`inc83'>`inc80'

```
The -bys id- does no harm, but is unnecessary.
```More importantly, he raises a good question about missings. Here's a
```
better approach if missings are present:
```egen numProp4 = mean(numProp) if wavenum == 4, by(id)
```
bysort id (numProp4) : replace numProp4 = numProp4[1]
```egen numInsArrng6 = mean(numInsArrng) if wavenum == 6, by(id)
```
bysort id (numInsArrng6) : replace numInsArrng6 = numInsArrng6[1]
```gen myindicator = numProp4 < numInsArrng6 if !missing(numProp4,
```
numInsArrng6)
```(Note incidentally that Martin's original code assumes that each year of
interest is represented in each panel, and will give incorrect results
```
if either year is omitted.) Nick n.j.cox@durham.ac.uk
```Martin Weiss

That is a superior solution to mine, and I recommend that Erick should
use
this one. Here is an adaption to my dataset, checking for an income gain
between 80 and 83:

*************
clear*
inp id year sex inc
1 80 0 5000
1 81 0 5500
1 82 0 6000
1 83 0 7000
2 80 1 2000
2 81 1 2200
2 82 1 3300
2 83 1 3500
3 80 0 3000
3 81 0 2000
3 82 0 1000
3 83 0 1000
4 80 0 3000
4 81 0 4000
4 82 0 1000
4 83 0 10000
end

tempvar inc80 inc83
```
egen `inc80' = total((year == 80) * inc) , by(id) egen `inc83' = total((year == 83) * inc) , by(id) bys id: g byte higher83=`inc83'>`inc80'
```l, noo sepby(id)

As always, be advised that missings count as arbitrarily large, so you
want
to be extra careful and double-check any results...

Nick Cox

```
Another approach:
```I infer panel structure with an identifier, and at most one measurement
```
for each -wavenum-. If that is so egen numProp4 = total((wavenum == 4) * numProp) , by(id) and
```egen numInsArrng6 = total((wavenum == 6) * numInsArrng) , by(id)

spread values in particular wave numbers to all observations with the
```
same identifier. For example, wavenum == 4
```is 1 if wavenum is 4 and 0 otherwise, so the ensuing total is just the
```
value of numProp for wavenum == 4. The required indicator variable follows directly.
```Note that even Erick's -if- condition had been correct (it isn't), his
expression for indicator (dummy in his terminology) would have yielded 1
```
and missing, not 1 and 0.
```Martin Weiss

More Income in 1982 than in 1980? Let`s see...

*********
clear*
inp id year sex inc
1 80 0 5000
1 81 0 5500
1 82 0 6000
1 83 0 7000
2 80 1 2000
2 81 1 2200
2 82 1 3300
2 83 1 3500
3 80 0 3000
3 81 0 2000
3 82 0 1000
3 83 0 1000
4 80 0 3000
4 81 0 4000
4 82 0 1000
4 83 0 10000
end
*mark years to compare
tempvar compare
g byte `compare'=year==80 | year==82
```
*sort them to the end, within id and respecting years, and generate indicators
```bys id (`compare' year) : g byte hi82=inc[_N]>inc[_N-1]
*resort
sort id year
drop `compare'
*let`s see
l, noo sepby(id)

```
Erick Guerrero, M.A. (Ph.D. Candidate)
```I am trying to create a dummy
variable that is equal to 1 when organizations have more
properties in wave 6 compared to wave 4. My data is in long
form.

Logically, it would be something like this, but this does not
work, I am missing some steps.

gen HiPropertyw6= 1 if (numProp==wavenum==4 <
numInsArrng==wavenum==6).
```
```

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```
```
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```