Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Carryforward/backward

    Dear all,

    I have this panel data (I'm giving the example only about one company)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long gvkey float(modate trn) byte(ffind48 tic_newind) float(event_time mean_trn treated treat_matching) int matching
    29517 432   .08366858 42 0  .         . . .  .
    29517 433    .1074952 42 0  .         . . .  .
    29517 434   .04250479 42 0  .         . . .  .
    29517 435   .10434986 42 0  .         . . .  .
    29517 436   .11772685 42 0  .         . . .  .
    29517 437   .05512161 42 0  .         . . .  .
    29517 438   .05692236 42 0  .         . . .  .
    29517 439  .072848454 42 0 -5 .09932967 1 1  .
    29517 440   .07011225 42 0 -4 .09870233 1 1 13
    29517 441   .05203461 42 0 -3 .12980506 1 1  .
    29517 442  .072427504 42 0 -2 .12488867 1 1  .
    29517 443   .06281572 42 0 -1  .1348747 1 1  .
    29517 444   .07724509 13 1  0 .12799968 1 .  .
    29517 445   .03720767 13 0  1 .12288648 1 .  .
    29517 446  .023175865 13 0  2 .14432235 1 .  .
    29517 447    .0207203 13 0  3  .1344712 1 .  .
    29517 448   .04368569 13 0  4  .1244448 1 .  .
    29517 449   .04155753 13 0  .         . . .  .
    29517 450   .04167446 13 0  .         . . .  .
    29517 451   .04698316 13 0 -5 .13748688 0 0  .
    29517 452   .05114593 13 0 -4 .13370831 0 0  .
    29517 453   .01690909 13 0 -3 .15079726 0 0  .
    29517 454  .024613636 13 0 -2 .13852969 0 0  .
    29517 455   .05386364 13 0 -1 .14454024 0 0  .
    29517 456   .01256818 13 0  0  .1533158 0 .  .
    29517 457  .026136363 13 0  1 .13952337 0 .  .
    29517 458   .04420454 13 0  2  .1593396 0 .  .
    29517 459   .03952273 13 0  3 .14585368 0 .  .
    29517 460   .08820455 13 0  4 .14730963 0 .  .
    29517 461   .07636364 13 0  .         . . .  .
    29517 462   .14918615 13 0  .         . . .  .
    29517 463   .04667914 13 0 -5 .09932967 1 1  .
    29517 464  .033657625 13 0 -4 .09870233 1 1  .
    29517 465   .04432392 13 0 -3 .12980506 1 1  .
    29517 466   .08251356 13 0 -2 .12488867 1 1  .
    29517 467   .04787732 13 0 -1  .1348747 1 1  .
    29517 468   .04398241  2 1  0 .12799968 1 .  .
    29517 469   .04447701  2 0  1 .12288648 1 .  .
    29517 470    .1022898  2 0  2 .14432235 1 .  .
    29517 471   .05145524  2 0  3  .1344712 1 .  .
    29517 472  .010287388  2 0  4  .1244448 1 .  .
    29517 473   .05271829  2 0  .         . . .  .
    29517 474    .0877647  2 0  .         . . .  .
    29517 475   .02890498  2 0 -5 .13748688 0 0  .
    29517 476    .0880543  2 0 -4 .13370831 0 0  .
    29517 477   .04041629  2 0 -3 .15079726 0 0  .
    29517 478  .008470588  2 0 -2 .13852969 0 0  .
    29517 479   .10133937  2 0 -1 .14454024 0 0  .
    29517 480    .1401086  2 0  0  .1533158 0 .  .
    29517 481    .1040181  2 0  1 .13952337 0 .  .
    29517 482   .14177376  2 0  2  .1593396 0 .  .
    29517 483  .063040726  2 0  3 .14585368 0 .  .
    29517 484   .02276923  2 0  4 .14730963 0 .  .
    29517 485  .029085973  2 0  .         . . .  .
    29517 486  .014914027  2 0  .         . . .  .
    29517 487  .026244344  2 0 -5 .13748688 0 0  .
    29517 488   .03799095  2 0 -4 .13370831 0 0  .
    29517 489   .01383908  2 0 -3 .15079726 0 0  .
    29517 490   .02289655  2 0 -2 .13852969 0 0  .
    29517 491   .06082759  2 0 -1 .14454024 0 0  .
    29517 492  .013210728  2 0  0  .1533158 0 .  .
    29517 493  .027417624  2 0  1 .13952337 0 .  .
    29517 494  .024934866  2 0  2  .1593396 0 .  .
    29517 495  .015632184  2 0  3 .14585368 0 .  .
    29517 496  .019846743  2 0  4 .14730963 0 .  .
    29517 497  .009655172  2 0  .         . . .  .
    29517 498 .0012873564  2 0  .         . . .  .
    29517 499 .0011340997  2 0 -5 .13748688 0 0  .
    29517 500  .012291187  2 0 -4 .13370831 0 0  .
    29517 501  .009230769  2 0 -3 .15079726 0 0  .
    29517 502   .01276466  2 0 -2 .13852969 0 0  .
    29517 503   .07029703  2 0 -1 .14454024 0 0  .
    29517 504   .08819497  2 0  0  .1533158 0 .  .
    29517 505    .1081188  2 0  1 .13952337 0 .  .
    29517 506   .13154608  2 0  2  .1593396 0 .  .
    29517 507    .0531297  2 0  3 .14585368 0 .  .
    29517 508   .03775203  2 0  4 .14730963 0 .  .
    29517 509  .033689436  2 0  .         . . .  .
    29517 510   .11933265  2 0  .         . . .  .
    29517 511   .16224205  2 0 -5 .13748688 0 0  .
    29517 512   .10275135  2 0 -4 .13370831 0 0  .
    29517 513   .23134495  2 0 -3 .15079726 0 0  .
    29517 514   .21160975  2 0 -2 .13852969 0 0  .
    29517 515    .2663972  2 0 -1 .14454024 0 0  .
    29517 516    .3348346  2 0  0  .1533158 0 .  .
    29517 517   .13191105  2 0  1 .13952337 0 .  .
    29517 518   .20818377  2 0  2  .1593396 0 .  .
    29517 519    .2983787  2 0  3 .14585368 0 .  .
    29517 520    .6756593  2 0  4 .14730963 0 .  .
    29517 521    .7332361  2 0  .         . . .  .
    29517 522    .9892002  2 0  .         . . .  .
    29517 523    .5725882  2 0 -5 .13748688 0 0  .
    29517 524    .3594635  2 0 -4 .13370831 0 0  .
    29517 525    .6906328  2 0 -3 .15079726 0 0  .
    29517 526    .8219657  2 0 -2 .13852969 0 0  .
    29517 527   .33211195  2 0 -1 .14454024 0 0  .
    29517 528    .5735751  2 0  0  .1533158 0 .  .
    29517 529    .3689737  2 0  1 .13952337 0 .  .
    29517 530    .3584927  2 0  2  .1593396 0 .  .
    29517 531    .2158612  2 0  3 .14585368 0 .  .
    end
    format %tm modate
    What I need to do is to carryforward the values on variable "matching" as long as the variable treated is either 1 or 0 (no missing). As you can see from the example, I also have to carrybackward the same value (again ,as long as the variable treated is either 1 or 0)
    So i know, that carryforward command is on ssc and sometimes it might be a misleading option. Neverthless, I don't see other solutions in coding it.

    So far, what I've done is the following

    Code:
    bys gvkey (modate): carryforward matching if treated==0 | treated==1, gen(match1)
    But, what about to carrybackward? I tried with the option "back" without valuable results.

    Do you have other options? (I'm happy also without using carryforward command )

    Many thanks in advance
    Last edited by Marco Errico; 14 Nov 2020, 08:27.

  • #2
    The carryforward help file provides instructions on how to do this. You need to generate a negative date variable:
    Code:
    gen int negmodate = neg(modate)
    Then:
    Code:
    bys gvkey (negmodate): carryforward matching if treated==0 | treated==1, gen(match2)
    And you can combine the two variables using
    Code:
    replace match1 = match2 if match1 == .
    drop match2

    Comment


    • #3
      I don't see a complete specification of rules here. This may be a start. I

      Code:
      clonevar wanted = matching 
      bysort gvkey (modate) : replace wanted = wanted[_n-1] if missing(wanted) & inlist(treated, 0, 1)    
      gsort gvkey -modate 
      by gvkey : replace wanted = wanted[_n-1] if missing(wanted) & inlist(treated, 0, 1)
      sort gvkey modate

      Comment


      • #4
        Originally posted by Ali Atia View Post
        The carryforward help file provides instructions on how to do this. You need to generate a negative date variable:
        Code:
        gen int negmodate = neg(modate)
        Then:
        Code:
        bys gvkey (negmodate): carryforward matching if treated==0 | treated==1, gen(match2)
        And you can combine the two variables using
        Code:
        replace match1 = match2 if match1 == .
        drop match2
        Thanks Ali Atia . Nevertheless, when I try with:
        Code:
        Code:
        gen int negmodate = neg(modate)
        "unknown function neg()
        r(133)"

        I also tried with egen, but same thing. Maybe is it a specific package? I'm using STATA 15.1

        Thanks

        Comment


        • #5
          Thanks Nick Cox

          Your codes really work properly without using carryfroward. Apologizes if I wasn't clear in the explanation: I only wanted to extend the variable matching as long as there is a value (1-0) in the variable treated.
          I tried many days in many ways. I didn't know that the option "inlist" can be really helpful in this case.

          Thanks again for your time.

          Marco

          Comment


          • #6
            Thanks for the thanks. To avoid confusion, note that inlist() is a function, not an option. In this case


            Code:
            (treated == 0 | treatment == 1)
            would be fine too.

            Comment


            • #7
              Thanks! I'm already studying it.

              Sorry for a quick question.
              Regarding your codes, when you suggest
              Code:
              bysort gvkey (modate) : replace wanted = wanted[_n-1] if missing(wanted) & inlist(treated, 0, 1)
              would lead to the same outcome using
              Code:
              bysort gvkey (modate) : replace wanted = wanted[_n-1] if missing(wanted) & treated==0 | treated==1
              ?

              EDIT: didn't know you already answered to this question Apologize

              Comment


              • #8
                Code:
                 
                 missing(wanted) & (treated==0 | treated==1)
                spells out what you want more explicitly. I can never remember the precedence rules, so tend just to parenthesise aggressively.

                Comment


                • #9
                  Sorry about that Marco Errico - the proper command is
                  Code:
                  -modate
                  , not
                  Code:
                  neg(modate)
                  . Either way, it looks like you were able to figure it out with Nick's help.

                  Comment


                  • #10
                    Yes thanks Ali Atia and Nick Cox .

                    Finally solved, also without using carryforward command that sometimes lead to puzzling results

                    Comment

                    Working...
                    X