Announcement

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

  • Generate dummy without looping?

    Dear all,

    I have a time series (say 12 observations, one for each month). I would like to create 12 different dummies that will be 1 (for all 12 observations) when the value is above the value in a given month.

    So for example, the dummy-january will be one for all the 12 months in which the value is above the value in january. Then the dummy-february repeats this, but comparing to the value in february. And so on, until I have 12 dummies in the end.

    My question is: Can I do this without looping over the observations?

    The problem is that I have a lot more than 12 observations, and they dont start always at the same point (for example not all start at _n=1)...

    Thanks in advance if you can help!
    Thiago

  • #2
    Welcome to the Stata Forum / Statalist,

    Please read the FAQ. There you wil find information about share data/output/command. This is the best approach to reap a helpful reply. It can be a toy example.

    In short, you may use - dataex - for that matter, or code delimiters.

    That said, if I understood right, considering the data is - wide - shaped, you could start by - egen + rowmax - , then - reshape long - and - generate - the binary variable accordingly.
    Best regards,

    Marcos

    Comment


    • #3
      To expand a little on Marcos' response, you'll increase your chances of a useful answer by following the FAQ on asking questions - provide Stata code in code delimiters, readable Stata output (fixed spacing fonts help), and sample data using dataex.

      Almost always, it is better to use this kind of data in long form rather than wide form - see the documentation on reshape. Once it is long form, you don't need to loop over observations. Something like would work:

      forvalues mo=1/12 {
      g dum`mo'=(value>0)
      }

      Comment

      Working...
      X