Announcement

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

  • Generate indicator variables for first, second, and third instance of a particular holiday in a dataset

    Hey Everyone,

    I have a dataset that has daily sales data of different sales locations. Here is an example of the dataset

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(date salesloc) float priceperunit double totalsales
    19912 235 21.712767 22993.833332061768
    19912 236 21.137405  54081.72182846069
    19912 243  20.46168 3887.7194595336914
    19912 247  21.65135  4601.851825714111
    19913 235  27.12485   24768.1962556839
    19913 236  19.26099  22003.90766811371
    19913 237  53.81504  42514.22219085693
    19913 243  20.46168  2721.403621673584
    19913 247 30.270655  5046.296199798584
    19914 231  48.48392 1357.5498352050781
    19914 232  22.55149 5850.0943241119385
    19914 235 31.174427 28244.031036376953
    19914 236  27.49371 21441.243675231934
    19914 237  59.26026 11860.103530883789
    19914 238  50.39795  6838.638666152954
    19914 240  18.48429  36.96857833862305
    19914 243  20.46168 143.23176956176758
    19914 247  22.41789   6846.66655921936
    19915 231 25.877256  42233.74929237366
    19915 232  44.99617  4406.660535812378
    end
    format %d date
    I would like to generate indicator variables the count the 1st, 2nd, of third occurrence of a holiday (like Christmas Eve for example) that an establishment experiences (Not all locations are open the entire data set, so some locations may have been open for a holiday period for two times, while others open for 3 or 4 holiday periods, and one location's first Christmas Eve may be different from another location's first Christmas Eve).

    My goal is to see how prices and sales change as firms/establishments gain more experience during a holiday
    Ex:

    reg v1 v2 v3 v4 if FirstChristmasEve==1.

    How can I go about generating these variables, since they are conditional on other appearances of a particular day.

    Thank you in advance for your help!




  • #2
    You have daily dates and sales locations. It's Christmas Eve when it is December 24.

    So first get an indicator

    Code:
    gen is_dec24 = month(date) == 12 & day(date) == 24
    and then you want something like

    Code:
    bysort salesloc (date) : gen count_dec24 = cond(is_dec24, sum(is_dec24), 0) 
    
    gen first_dec24 = count_dec24 == 1 
    
    gen second_dec24 = count_dec24 == 2
    and the third should be easy now.

    You don't need all these variables but the code may be easier to think about approached step by step.

    Comment


    • #3
      Thank you so much Nick! I don't mind extra variables. I like to learn the steps and thought processes, so I can apply later.

      Comment

      Working...
      X