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: Carry forward


From   David Kantor <[email protected]>
To   [email protected]
Subject   Re: st: Carry forward
Date   Sun, 06 Oct 2013 09:41:27 -0400

At 12:21 AM 10/4/2013, Tina wrote:
Hi All,

I am having some problems with carryforward and I wanted to see if =
anyone else knows of any problems with this command.

I have a panel dataset that I have created and I want to use the =
carryforward command to fill in missing values. =20

. tsset id enroll

      panel variable:  id (unbalanced)

       time variable:  enroll, 1 to 3, but with gaps

               delta:  1 unit

. tsfill,full

. replace nvisits_year =3D 0 if nvisits_year =3D=3D .

(1517200 real changes made)

. **fill in missing data**

. bysort id: carryforward age, gen(age_1)

age_1:  (1517200 real changes made)


. bysort id: carryforward pay, gen (pay_cf)

pay_cf:  (1516900 real changes made)


. replace age_1 =3D age_1 + 1 if age=3D=3D.

(1517200 real changes made)



I have reloaded the raw data files and ran my exact same do file, =
however I get different results every time I run the code:


. tsset id enroll

      panel variable:  id (unbalanced)

       time variable:  enroll, 1 to 3, but with gaps

               delta:  1 unit

. tsfill,full

. replace nvisits_year =3D 0 if nvisits_year =3D=3D .

(1517200 real changes made)

. **fill in missing data**

. bysort id: carryforward age, gen(age_1)

age_1:  (1517200 real changes made)

. bysort id: carryforward pay, gen (pay_cf)

pay_cf:  (1516901 real changes made)

. replace age_1 =3D age_1 + 1 if age=3D=3D.

(1517200 real changes made)

=20
And the third time I run the code:

. bysort id: carryforward pay, gen (pay_cf)
pay_cf:  (1516894 real changes made)


Any help on what might be happening to the datafile and why the changes =
are not the same each time I run the code?

Thanks

Tina

Does -id- uniquely identify the observations? (Does sorting on it yield a unique order?) I don't think so. Based on what you have written, you need to combine it with -enroll-.
Thus,
bysort id (enroll): carryforward pay...

Actually, you need to be sure that the combination of the two variables uniquely identifies observations. For that, you can use -assertky- (from SSC) or -isid- (standard Stata). Thus,
assertky id enroll
or
isid id enroll, sort

--followed by
by id (enroll): carryforward pay...

Note that in this situation, you can just use -by-; no need for -bysort-.

Generally, -carryforward- requires a "secondary" set of sorting variables to obtain consistent and meaningful results. This is stated in the help. But your inquiry has prompted me to add a warning feature into -carryforward-.
HTH
--David

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index