Announcement

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

  • Generating Dummy variables based on Monthly period

    Hello everyone i am really new to stata and i have an econometrics paper due in a couple days and i have to perform some time series analysis on the data i have.
    I am using Monthly data from 2007-12-01 to 2019-10-01 in a "YMD" format, which i have formatted and declared as a time series data
    I am trying to create an "Embargo" variable which takes the value of 1 from 2019-01-01 to 2019-10-01 and 0 for all months before that. So i would really appreciate it if someone could provide me with some help in how to go about this dummy variable generating process using a date/period argument.
    I have tried several commands but none have seemed to work thus far so any input would be welcomed.

  • #2
    I don't know what you mean when you say you have "formatted" your date variable. The critical thing is that your date variable must be a valid Stata internal format numeric date variable. If that is what you have, then what you want is very simple:
    Code:
    gen byte embargo = inrange(date, td(1jan2019), td(1oct2019))
    where date is the name of your date variable.

    If your date variable is strings that look like "2007-12-01" then you have to convert it to a Stata internal format numeric date variable first.:
    Code:
    gen date = daily(string_date, "YMD")
    format date %td
    where string_date is the name of your string variable that looks like a date to human eyes.

    Comment


    • #3
      thank you for your response Clyde, i have already converted the date/month string of 2017-12-01 to Stata internal format using the gen date_d = daily(date, "YMD") then gen monthly = mofd(date_d) and i formatted it to 2007m12 to 2019m10 to signify 2007-12-01 to 2019-10-01. The reason i used the monthly variable was to declare that my dataset was a time series data in monthly time periods

      What i wanted to achieve was to create a dummy variable "Embargo" = 1 from 2019m1 to 2019m10 and 0 from 2007m12 to 2018m12 using the monthly date variable i have formatted.

      i tried using the gen byte embargo =inrage(date_d, td(2019-01-01), td(2019-10-01)) and the gen byte US_embargo =inrage(monthly, td(2019m1), td(2019m10)) to create the dummy variables but i was met with a response of unknown function inrange().

      Please is there any other command i could use to fix this issue. Again thank you for your help. I have attached a screenshot of my dataset.
      Click image for larger version

Name:	Screen Shot 2019-12-03 at 12.22.56 PM.png
Views:	1
Size:	43.4 KB
ID:	1527347

      Click image for larger version

Name:	Screen Shot 2019-12-03 at 12.25.28 PM.png
Views:	1
Size:	22.0 KB
ID:	1527348

      Comment


      • #4
        if you look more closely at Clyde's code you will see that the function is "inrange" while you have "inrage"

        Comment


        • #5
          In addition to #4 I think you need

          Code:
          inrange(date_d, td(1jan2019), td(1oct2019))

          Comment


          • #6
            Thank you all for your help and i apologize for my mistake earlier

            Comment

            Working...
            X