Announcement

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

  • Generate Dummy Variables From Date and Month

    Hi there,

    I have a list of dates in a .csv and I'm trying to generate dummy variables from these dates under the assumption:

    I need to have a dummy variable for each month from September 2009 onwards
    The dummy variable should be "1" for the month in which the date in my table occurs and then continue to be "1" for every month thereafter

    Any help would be much appreciated (I'm very new to Stata!)

  • #2
    Hello Jamie,

    I'm not sure whether I understood your query. You could make it easier if you give an example from your data. This way, we could check type and format, for example.

    That said, you may think about using the factor notation "i." before the variable so as to produce dummies.
    Best regards,

    Marcos

    Comment


    • #3
      You don't tell us about your variable names, etc. There is no data example here. Please do read and act on http://www.statalist.org/forums/help#stata

      But let's suppose you have a Stata monthly variable mdate

      Code:
      su mdate, meanonly
      
      forval d = `r(min)'/`r(max)' {
              local suffix : di %tmNNYY `d'
              gen index`suffix' = mdate >= `d'
      }
      Marcos: I don't think factor notation will help here. I'd be pleased to see how it does.

      Comment


      • #4
        Hi Marcos,

        Thanks for the help.

        So I have a list of dates ranging from September 2009 to October 2016

        Say the date was 09/05/2011 I would need to generate a dummy variable in "month/year" fields of "0" up to "05/11" and from then onward generate a "1" in fields up to "10/16"

        I'm guessing its not too difficult of an operation but I'm very stuck.

        Thanks again

        Comment


        • #5
          Would be most helpful if you could show us what your data look like. And I'm still not sure how the .csv comes in - are you merging in the .csv file and creating a series of dummies? But if your date is the variable -mdate- I think you want:

          Code:
          gen byte dummy=mdate>=mofd(mdy(9,5,2011))

          This will be 0 for any months prior to 9/5/11 and 1 for any months thereafter.

          hth,
          Jeph

          Comment


          • #6
            Nick, your are absolutely correct, as always. By reading #1 (and still without much information on the data, lately presented), I initially thought the variable "month" could be taken as just plain integers.
            Best regards,

            Marcos

            Comment

            Working...
            X