Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: on construction of variables in Stata


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: on construction of variables in Stata
Date   Mon, 28 Mar 2011 14:17:46 +0100

If we knew these id-specific dates from a separate variable, this would be

gen VAR = date > specific

As it is, it seems you need

clonevar VAR2 = VAR
bysort id (date) : replace VAR2 = sum(VAR2)

as missings are ignored by -sum()-, and this sum is bumped up by 1
when you hit 1.

Another way to do it

bysort id (date) : replace VAR2 = VAR2[_n-1] if missing(VAR2)
replace VAR2 = 0 if missing(VAR2)

That approach is documented

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

I do this on a clone in case I have misunderstood you.

Nick

On Mon, Mar 28, 2011 at 2:06 PM,  <[email protected]> wrote:

> I have a large data-set sorted by id and date over a period of three years (at
> the daily frequency).
>
>
> For each panel defined by id,  I need to build a variable that takes a value of
> one at a  specific date that is different for each id. Consequently, the
> variable (VAR) must take a  value of 1 till end of the sample period and it must
> take a value of zero at any date before this specific day.
>
>
> Currently, VAR has values of 1 only assigned to the specific days for each id
> and missing observations elsewhere.
>
>
> Could you please let me know if I could ask stata with some command to fill in
> VAR with zeros preceding these specific days and to fill it in with ones
> afterwards (ie after the occurrence of the specific date).

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index