[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From   "Nick Cox" <>
To   <>
Subject   st: RE: RE: AW: RE: Re: Generate Variable
Date   Fri, 13 Mar 2009 14:23:44 -0000

In fact, the -egen- call should go altogether. For example, 

gen numProp4 = numProp if wavenum == 4, by(id)
bysort id (numProp4) : replace numProp4 = numProp4[1]

is as much as is to be needed. -egen- was invoked because 

egen numProp4 = total(numProp * (wavenum == 4)), by(id)

gets you there in one line whenever there is precisely one non-missing
value for wavenum 4 in each panel -- at the expense of lots of extra
lines off-stage. But once you are realistic about missings, retreating
to a first principles approach becomes a much better strategy. 


Nick Cox

[Martin] 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,

Martin Weiss

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) 


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. 

*   For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index