Announcement

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

  • Identify first year of a firm in the panel

    Hi everyone,
    How can I create a dummy variable for the first year of a firm in the panel data?

    Thanks in advance

  • #2
    Miral:
    you may want to consider the following toy-example:
    Code:
    use "https://www.stata-press.com/data/r16/nlswork.dta"
    . bysort idcode (year): gen wanted=1 if idcode==_n
    . replace wanted=0 if wanted==. & idcode!=. & year!=.
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      I think that in Carlo's example the generate command should be stated as
      Code:
      bysort idcode (year): gen wanted=1 if _n==1
      to obtain a 1 for the first year within each idcode.

      Comment


      • #4
        Dear Carlo Lazzaro ,
        I used the code. It generated a variable with all 0 values.

        Comment


        • #5
          Dear William Lisowski, the code worked perfectly. Thank you so much.

          Comment


          • #6
            Miral:
            I obviously agreed with William about the first line of the code (my typo indeed. Thanks William for pointing this out).
            But you asked for a two-level categorical variable, too.
            Code:
            . bysort idcode (year): gen wanted=1 if _n==1
            
            . replace wanted=0 if wanted==. & idcode!=. & year!=.
            
            
            . list wanted if idcode==1
            
                   +--------+
                   | wanted |
                   |--------|
                1. |      1 |
                2. |      0 |
                3. |      0 |
                4. |      0 |
                5. |      0 |
                   |--------|
                6. |      0 |
                7. |      0 |
                8. |      0 |
                9. |      0 |
               10. |      0 |
                   |--------|
               11. |      0 |
               12. |      0 |
                   +--------+
            
            .
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Dummies (better: indicators) are most useful when they have distinct values of 0 and 1. That being so, a direct alternative to answers so far is


              Code:
              bysort id (year) : gen first = _n == 1
              and another that is a little more general is


              Code:
              bysort id (year) : gen first = year == year[1]
              which allows for panels in which repeated values of the time variable are possible.

              EDIT: Carlo Lazzaro's #6 was not visible as I first typed this. Note that his #6 is in spirit a two-line version of my first suggestion..
              Last edited by Nick Cox; 06 Dec 2021, 09:11.

              Comment


              • #8
                ...and, as such, less efficient!!
                Great, Nick!
                Kind regards,
                Carlo
                (Stata 19.0)

                Comment


                • #9
                  Dear Carlo Lazzaro ,
                  Thank you. Can you correct me if it requires a dummy variable for the following statement?

                  "To further minimize reverse causality, we perform an instrumental-variable analysis. We identify the earliest year when each firm appears in our sample. TW co-option in the earliest year could not have resulted from the firm’s risk-taking in any of the subsequent years, making reverse causality unlikely"




                  Comment


                  • #10
                    Miral:
                    provided that your clarification seems to classify your query as one belonging to the https://www.statalist.org/forums/help#adviceextras #4 Chapter, your trick should reduce the risk of reverse causality-led endogeneity.
                    Kind regards,
                    Carlo
                    (Stata 19.0)

                    Comment


                    • #11
                      Let me clarify, for the benefit of anyone who should read this topic at a later date, that my advice in post #3 is intended to apply to only the generate command in post #2, leaving the replace command in post #2 as it was given.

                      I wasn't trying to rewrite or improve on Carlo's solution, just point out the typo that Carlo had overlooked.

                      Comment


                      • #12
                        William:
                        your appreciated advice reached me the right way.
                        I was really grateful for your pointing out my typo in such a kind way.
                        Molte grazie davvero/Many thanks indeed
                        Kind regards,
                        Carlo
                        (Stata 19.0)

                        Comment


                        • #13
                          Thanks to all wonderful STATA community

                          Comment

                          Working...
                          X