Announcement

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

  • Crrating dummy variables for months and replacing their values for ea

    Hello,

    I have a duration data where I observe the date an ad was started (first_date) and the date it was removed (end_date). My dataset covers the period between January 2017 and Apr 2022. I would like to create a separate dummy variable for each month (in total 64 dummy variables for 64 months). Initially all dummy variables should equal 0. Then, I'd like to change the values of these dummies from 0 to 1 if the ad was live on that month. That is, if an add's first_date is 23mar2017 and last date is 18feb2018, then I would like to change the values of dummy variables from March 2017 to Feb 2018 to 1 for this observation. Can you help me doing this? Thank you very much in advance for your attention!

  • #2
    You do not show example data, so I cannot know whether your first_date and end_date variables are proper Stata date variables, or if they are other things that look like dates to the human eye but are not usable as date information in Stata. The code below assumes you have proper Stata date variables. If you do not, the code will produce nonsense, if it produces anything at all.

    Code:
    local start = tm(2017m1)
    local finish = tm(2022m4)
    
    forvalues m = `start'/`finish' {
        local v: display %tmmonCCYY `m'
        gen active_`v' = inrange(`m', mofd(first_date), mofd(end_date))
    }
    If my assumption that you have proper Stata date variables is wrong, then we have both wasted our time. To avoid such problems, in the future whenever you want help with code, be sure to show example data. And to assure that that data is usable by those who try to help you, use the -dataex- command to do that. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Hello Clyde,

      Thank you very much for helping me, I had proper Stata date variables and your code did exactly what I needed. In the future, I will make sure to show example data and write better questions. I wasn't aware of it but now I tried the dataex command. In case it will be helpful to readers, below is an example data generated by dataex.

      Kind regards,
      Selcen

      Code: datex first_date end_date
      Output:

      input float(first_date end_date)
      20850 20879
      20901 20960
      21078 21137
      21067 21126
      20956 21015
      21031 21090
      21042 21101
      20858 20917
      21033 21092
      20954 21013
      21136 21196
      end
      format %td firstdate
      format %td enddate

      Comment

      Working...
      X