Announcement

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

  • Tag events that occurred the same day in panel data

    Hi all!
    I'm working with a panel data such as the following (it's my first time with dataex and the preview didn't convince me at all, sorry about that).

    Code:
    * Example generated by -dataex-. To install: ssc install dataex 
    clear
    input double t float(panelid price_mode mode_delta)
    18995 160   41 0
    18995 142   44 0
    18995  93 35.8 0
    18995  68 39.9 0
    18995  45 39.9 0
    18996  68 39.9 0
    18996  45 39.9 0
    18996  93 35.8 0
    18996 142   44 0
    18996 160   41 0
    end
    format %td t
    label values panelid panelid
    label def panelid 45 "Devoto", modify
    label def panelid 68 "Disco", modify
    label def panelid 93 "Macromercado Mayorista", modify
    label def panelid 142 "Ta - Ta", modify
    label def panelid 160 "Tienda Inglesa", modify
    label var panelid "group(id_chain product brand)" 
    label var price_mode "(first) price_mode"
    The fact is that I'm trying to identify days on which the -mode_delta- variable (daily chainge of -price_mode-) moved in the same way (that is negative or positive) in every supermarket (-panelid-). In the example there weren't changes in the mode but I expect that along the dataset there are days that satisfy that condition. I was looking forward to tag those days with a 1 when changes are positives and a -1 when they are negatives.

    Thanks in advance. Greatings,

    Diego

  • #2
    I'm not sure I understand what you want, but if I do have it correct, then the following will do it:

    Code:
    gen up_or_down = sign(mode_delta)
    
    by t (up_or_down), sort: gen wanted = up_or_down if up_or_down[1] == up_or_down[_N]
    replace wanted = 0 if !missing(wanted) & up_or_down == 0
    If that's not correct, please post back with example data that fully illustrates your problem, not just some days where there are no changes taking place, and explain what the results should be for several instances in the example data.

    Thank you for using -dataex-.

    Comment


    • #3
      Thanks for your response Clyde. the -sign- function is definetly part of the solution.
      I will rewrite the problem here.

      In the example below there are price modes (and their daily change) of a product in different supermarket's chains. The example includes observations corresponding to all the chains in two different days.
      Now, on the first day, mode_delta was negative in only two chains of supermarket, I do not want to tag that event. I wan't to tag the observations if and only if the negative (or positive) value of mode_delta is repeated among all the chains. i.e The events I want are: price_mode change in the same way (same sign), in every SM chain and on the same day.
      Hope I was a little bit more clear this time. Regards,

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double t long id_chain float(price_mode mode_delta)
      19815 3    41 -.10869565
      19815 2    41 -.10869565
      19815 4 38.75          0
      19815 7    46          0
      19815 6    45          0
      19816 3    41          0
      19816 2    41          0
      19816 6    45          0
      19816 7    46          0
      19816 4 38.75          0
      end
      format %td t
      label values id_chain id_chain
      label def id_chain 2 "Devoto", modify
      label def id_chain 3 "Disco", modify
      label def id_chain 4 "Macromercado Mayorista", modify
      label def id_chain 6 "Ta - Ta", modify
      label def id_chain 7 "Tienda Inglesa", modify
      label var price_mode "(first) price_mode"

      Comment


      • #4
        Your explanation in #3 seems clearer. And it is what I thought you wanted when you wrote #2. Have you tried the code in #2? I believe it is what you want. If it isn't, please post back with an example from your data where it gives incorrect results.

        Comment


        • #5
          Clyde, the code you gave me worked just fine. The fact that no events were tagged took me for suprise so I cheked it with an fake example and the results were ok.
          I leave it below.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double t byte id_chain double mode_delta
          21815 3 -.33
          21815 4 -.44
          21815 2 -.56
          21815 1 -.32
          21816 2    0
          21816 3  .36
          21816 1  .36
          21816 4  .32
          end
          format %td t
          
          gen up_or_down = sign(mode_delta)
          by t (up_or_down), sort: gen wanted = up_or_down if up_or_down[1] == up_or_down[_N]
          replace wanted = 0 if !missing(wanted) & up_or_down == 0
          
          clear
          input double t byte id_chain double mode_delta float(up_or_down wanted)
          21815 3 -.33 -1 -1
          21815 4 -.44 -1 -1
          21815 2 -.56 -1 -1
          21815 1 -.32 -1 -1
          21816 2    0  0  .
          21816 3  .36  1  .
          21816 1  .36  1  .
          21816 4  .32  1  .
          end
          format %td t
          Thanks for all.
          Btw, how do I get the datetime format shown in the table with dataex? (rather than just specified in a line below)
          Last edited by Diego Gutierrez; 19 Sep 2019, 12:39.

          Comment


          • #6
            Btw, how do I get the datetime format shown in the table with dataex? (rather than just specified in a line below)
            I'm not sure I understand the question. If you mean how do I get the -dataex- output to show "23sep2019" instead of 21815, the answer is that you can't and you shouldn't want to. -dataex- output is not meant for human eyes to read. Its purpose is to provide code that enables someone to quickly replicate your data in Stata. If -dataex- were to output "23sep2019" for your date variable, then the user would now have to first convert that string variable back to the numeric date variable in order to do any calculations with it. String date variables that human eyes can read are useless for calculations.

            Comment


            • #7
              That was exactly my question. Thanks a lot Clyde for your very useful code and advice.

              Comment

              Working...
              X