Announcement

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

  • Generating dummy variable using a range of dates

    Hi,

    I have a dataset of president's and their approval ratings. I would like to control for the entity fixed effects of every administration when I analyze the data. So, I need to divide the data into different administrations using a dummy variable. For example, President Obama's approval ratings need to be divided between his 2 terms: the 1st one from 2009q1 to 2012q4 and the 2nd one from 2013q1 to 2016q4. My date variable is in the format %tq (displayed as 2008q1). I am trying to create a dummy variable that is equal to 1 for the duration of every president's term.

    Thanks a lot,
    Kanishk

  • #2
    Hi,

    I assume that the maximum number of quarters per term that a President holds office is 16.
    Code:
    bysort president_var (timeperiod): gen first_term = _n < = 16
    gen second_term = 1 - first_term
    Last edited by Josh Clothiaux; 02 May 2017, 07:43.

    Comment


    • #3
      All quarterly dates that start 4 year terms have remainder 4 on division by 16:

      Code:
      . mata : mod(yq((2001, 2005, 2009, 2013), (1,1,1,1)), 16)
             1   2   3   4
          +-----------------+
        1 |  4   4   4   4  |
          +-----------------+
      Therefore

      Code:
       
      . webuse turksales, clear
      
      . gen spell = sum(mod(t, 16) == 4)
      
      . tab spell, gen(spell)
      
            spell |      Freq.     Percent        Cum.
      ------------+-----------------------------------
                0 |         12       30.00       30.00
                1 |         16       40.00       70.00
                2 |         12       30.00      100.00
      ------------+-----------------------------------
            Total |         40      100.00
      
      . l , sepby(spell)
      
           +------------------------------------------------------+
           |      t      sales   spell   spell1   spell2   spell3 |
           |------------------------------------------------------|
        1. | 1990q1        100       0        1        0        0 |
        2. | 1990q2   97.84603       0        1        0        0 |
        3. | 1990q3   98.84029       0        1        0        0 |
        4. | 1990q4   100.8275       0        1        0        0 |
        5. | 1991q1   98.90981       0        1        0        0 |
        6. | 1991q2   100.9992       0        1        0        0 |
        7. | 1991q3   101.9653       0        1        0        0 |
        8. | 1991q4   104.1229       0        1        0        0 |
        9. | 1992q1   99.74297       0        1        0        0 |
       10. | 1992q2   102.3116       0        1        0        0 |
       11. | 1992q3   104.2382       0        1        0        0 |
       12. | 1992q4   105.8506       0        1        0        0 |
           |------------------------------------------------------|
       13. | 1993q1   102.8379       1        0        1        0 |
       14. | 1993q2    104.604       1        0        1        0 |
       15. | 1993q3   106.5201       1        0        1        0 |
       16. | 1993q4   106.9322       1        0        1        0 |
       17. | 1994q1   101.7508       1        0        1        0 |
       18. | 1994q2    107.551       1        0        1        0 |
       19. | 1994q3   106.7199       1        0        1        0 |
       20. | 1994q4   108.2395       1        0        1        0 |
       21. | 1995q1    103.035       1        0        1        0 |
       22. | 1995q2   107.8659       1        0        1        0 |
       23. | 1995q3   107.6514       1        0        1        0 |
       24. | 1995q4   107.3684       1        0        1        0 |
       25. | 1996q1   102.7467       1        0        1        0 |
       26. | 1996q2   107.6782       1        0        1        0 |
       27. | 1996q3   111.0517       1        0        1        0 |
       28. | 1996q4   112.0229       1        0        1        0 |
           |------------------------------------------------------|
       29. | 1997q1   103.7079       2        0        0        1 |
       30. | 1997q2   104.9966       2        0        0        1 |
       31. | 1997q3   110.0057       2        0        0        1 |
       32. | 1997q4   109.8376       2        0        0        1 |
       33. | 1998q1   104.5891       2        0        0        1 |
       34. | 1998q2   107.5621       2        0        0        1 |
       35. | 1998q3   109.6842       2        0        0        1 |
       36. | 1998q4   111.4588       2        0        0        1 |
       37. | 1999q1   108.9672       2        0        0        1 |
       38. | 1999q2   108.7656       2        0        0        1 |
       39. | 1999q3   112.9617       2        0        0        1 |
       40. | 1999q4   111.9473       2        0        0        1 |
           +------------------------------------------------------+
      or just use factor variable notation on the spell variable.

      Comment


      • #4
        Sorry to hijack the thread, but I have a related (yet silly) question.
        Assume my data has a variable which is stored as a string with values such as: "1998q4", "2003q2", etc.
        How can I transform it to a proper quarterly-time variable for stata? I tried using the date() function, but the generated variable is missing on all observations.
        Last edited by Ariel Karlinsky; 02 May 2017, 08:30.

        Comment


        • #5
          Ariel: No; please start a new thread.

          Comment

          Working...
          X