Announcement

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

  • tab year, gen(year_dummy)

    Dear All, In the following example
    Code:
    webuse grunfeld, clear
    
    tab year, gen(year_dummy)
    we can generate year_dummy1 for the year 1935, year_dumm2 for the year 1936, and so on. Is it possible to generate year_dummy1935 for the year 1935, year_dummy1936 for the year 1936, and so on? Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    If there are no missing years:

    Code:
    tab year, gen(year_dummy)
    ren year_dummy# year_dummy#, renumber(1935)
    If there are missing years (e.g., a jump from 1935 to 1937):

    Code:
    tab year, gen(year_dummy)
    foreach year of varlist year_dummy*{
        gsort -`year'
        rename `year' year_dummy`=year[1]'
    }

    Comment


    • #3
      Dear Ali, Thanks again for these suggestions.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        See also recent posts within https://www.statalist.org/forums/for...-dummy-by-year

        Comment


        • #5
          Dear Nick, I saw your reply in the post. Thanks.
          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment


          • #6
            Another way to go:
            Code:
            webuse grunfeld, clear
            
            separate year, by(year) gen(Dum)
            recode Dum* (.=0) (* = 1)

            Comment


            • #7
              Dear Romalpa, Thanks for this interesting suggestion.
              Ho-Chuan (River) Huang
              Stata 19.0, MP(4)

              Comment

              Working...
              X