Statalist


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

RE: st: appending time periods


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: appending time periods
Date   Sun, 31 May 2009 11:35:11 +0100

I am not sure whether Olexandr's solution is what Max R is seeking, but
I want to clear two marginal issues out of the way. 

-gsort- is not a user-written command and any attempt to install it from
SSC will not work. It is part of official Stata, and has been since it
was written. 

The test 

... if indep3 == "" 

presupposes that -indep3- is a string variable, which seems unlikely.
But more general code that works whether -indep3- is string or numeric
is 

... if missing(indep3) 

Incidentally note that 

sort panelid timevar
by panelid: replace indep3 =  indep3[_n-1] if  missing(indep3) 

can be slimmed to one command 

bysort panelid (timevar): replace indep3 =  indep3[_n-1] if
missing(indep3) 

There's a more detailed and systematic discussion in 

FAQ     . . . . . . . . . . . . . . . . . . . . . . . Replacing missing
values
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
J. Cox
        2/03    How can I replace missing values with previous or
                following nonmissing values?
                http://www.stata.com/support/faqs/data/missing.html

Nick 
[email protected] 

Olexandr O. Vasetsky 

of course, remove the repeating "replace", my mistake

sort panelid timevar
by panelid: replace indep3 =  indep3[_n-1] if  indep3== ""
gsort panelid -timevar
by panelid: replace indep3 =  indep3[_n-1] if  indep3== ""
sort panelid timevar

Olexandr O. Vasetsky

to fill your indep3 dummy you can use something like this (might need to
"ssc install gsort" first):

sort panelid timevar
by panelid:  replace replace indep3 =  indep3[_n-1] if  indep3== ""
gsort panelid -timevar
by panelid:  replace replace indep3 =  indep3[_n-1] if  indep3== ""
sort panelid timevar

this program fills ALL the blanks for the indep3 - i.e those after the
existing observation (like in your example) as well as blanks BEFORE the
existing one, in case you have to fill it in both directions.

Max

> I am working on a panel dataset. Couple of variables varies overtime
> and across panels, others only vary across panels. Got some additonal
> data for time & panel varying variables, for new time periods. I would
> like append the new observations to the panel and then within each
> panel ensure the records for panel only variables are applied to them
> (i.e. no blanks).
>
> example:
>
>               panelid     timevar     dep     indep1 indep2
> indep3(dummy)
>                  1          1/1/2000    321      899     12
>  1
>                  1          2/1/2000    882      980     34
>  1
> add=>        1         3/1/2000     698      898     87    (here I
> want the above value to apply instead of a blank)
>
>                  2         1/1/2000      338      768    90

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



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