Announcement

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

  • Generate a Dummy Variable for Observations in Different Years

    Hi Statalists,

    I want to generate a match dummy variable (M_n) for everyone born in year n. That is if for an individual born in year n, group = 1 then M_n = 1 (where n is the year that the individual is born). I have data of observations born from 1959 - 2002, so it is kind of messy if I create a dummy for people born in every year without using a loop or something. Any help about how to code the loop would be appreciated. Thank you very much!

    Best,
    Yuxiao

  • #2
    The best way to help you is to see a sample of your data (use the -dataex- command). Pay special attention to section 12 of the FAQ: https://www.statalist.org/forums/help#stata
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Thank you, Carole! I am new to the forum and thanks for the guide!

      I've attached the datset below. I want to generate a dummy variable for every observation in every year (n). For instance, when n= 1952, if eth = 3 then M_1952 = 1.

      Also, I would like to use it as the independent variable in regressions. Can I code it as below?

      Code:
      reg y M_n i.region i.SurveyYear
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int n byte eth
      1952 3
      1967 3
      1957 3
      1964 3
      1958 3
      1944 3
      1952 3
      1972 3
      1959 3
      1955 3
      1950 3
      1959 3
      1962 3
      1954 3
      1948 3
      1952 4
      1941 3
      1946 3
      1972 6
      1953 3
      end
      label values eth ethnicity
      label def ethnicity 3 "k", modify
      label def ethnicity 4 "l", modify
      label def ethnicity 6 "o", modify
      label def
      Thanks!
      Last edited by Yuxiao Hu; 25 Jul 2018, 12:02.

      Comment


      • #4
        What should M_1952 be if eth=4?
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          HI Carole, M_n is a dummy that equals to 1 if eth =3 and equals to 0 if otherwise

          Comment


          • #6
            In that is the case, then the basic code is:

            Code:
            gen M_1952=0
            replace M_1952=1 if eth==3 and n==1952
            You can generalize this and create a loop to go through all years:

            Code:
            forvalues i=1959/2002 {
                gen M_`i' = 0
                replace M_`i'=1 if eth==3 & n==`i'
                }
            These codes will result in a dummy variable that is 1 if a person is of ethnicity "k" and is born in year n, 0 if the person is born in another year OR is of another ethnicity.


            However, you say you want to use these dummies in a regression. The easiest way to do this is to use factor notation (see help fvvarlist). You can more easily get what you want with the following:

            Code:
            gen year_eth3=n
            replace year_eth3=0 if eth!=3
            replace year_eth3=0 if mi(eth) | mi(n)
            reg dep_var i.year_eth3 other_var1 other_var2...
            You can compare the regression output of these two approaches using this very artificial example where I have replaced year (n) with rep78 and eth with foreign:


            Code:
            sysuse auto, clear
            
            forvalues i=1/5 {
                gen M_`i' = 0
                replace M_`i'=1 if foreign==1 & rep78==`i'
                }
            
            gen rep78_for1=rep78
            replace rep78_for1=0 if foreign!=1
            replace rep78_for1=0 if mi(foreign) | mi(rep78)
            
            reg price M_1 M_2 M_3 M_4 M_5 turn
            
            reg price i.rep78_for1 turn
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              Thank you very much, Carole! It is super helpful! Thanks!

              Comment

              Working...
              X