Announcement

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

  • create a variable with date and zeros for csdid command

    Dear Statalist members,

    I am facing an issue when dealing with dates.
    I want to run the csdid command, which requires a first_treat date (the first date at which an individual got the intervention). In my case, individual with id =1 is treated between 2018m3 and 2019m2 while individual with id=2 is treated between 2018m6 and 2019m1.

    So, what I want to obtain is:
    - for id= 1, first_treat=2018m3 within the treatment period and 0 outside of the treatment period.
    - for id=2, first_treat=2018m6 within the treatment period and 0 outside of the treatment period.
    I have used the commands and datasets below for id=1. However, I did not obtain what I want.

    Can anyone be of help here?

    Best,

    This the code I tried:
    Code:
    gen first_treat=0
    replace first_treat=tm(2018m3) if inrange(date, tm(2018m3), tm(2019m2)) & id == 1
    replace first_treat=tm(2018m6) if inrange(date, tm(2018m6), tm(2019m1)) & id == 2
    Below is the result:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id int year byte month float(treated date first_treat)
    1 2017  1 1 684   0
    1 2017  2 1 685   0
    1 2017  3 1 686   0
    1 2017  4 1 687   0
    1 2017  5 1 688   0
    1 2017  6 1 689   0
    1 2017  7 1 690   0
    1 2017  8 1 691   0
    1 2017  9 1 692   0
    1 2017 10 1 693   0
    1 2017 11 1 694   0
    1 2017 12 1 695   0
    1 2018  1 1 696   0
    1 2018  2 1 697   0
    1 2018  3 1 698 698
    1 2018  4 1 699 698
    1 2018  5 1 700 698
    1 2018  6 1 701 698
    1 2018  7 1 702 698
    1 2018  8 1 703 698
    1 2018  9 1 704 698
    1 2018 10 1 705 698
    1 2018 11 1 706 698
    1 2018 12 1 707 698
    1 2019  1 1 708 698
    1 2019  2 1 709 698
    1 2019  3 1 710   0
    1 2019  4 1 711   0
    1 2019  5 1 712   0
    1 2019  6 1 713   0
    1 2019  7 1 714   0
    1 2019  8 1 715   0
    1 2019  9 1 716   0
    1 2019 10 1 717   0
    1 2019 11 1 718   0
    1 2019 12 1 719   0
    2 2017  1 1 684   0
    2 2017  2 1 685   0
    2 2017  3 1 686   0
    2 2017  4 1 687   0
    2 2017  5 1 688   0
    2 2017  6 1 689   0
    2 2017  7 1 690   0
    2 2017  8 1 691   0
    2 2017  9 1 692   0
    2 2017 10 1 693   0
    2 2017 11 1 694   0
    2 2017 12 1 695   0
    2 2018  1 1 696   0
    2 2018  2 1 697   0
    2 2018  3 1 698   0
    2 2018  4 1 699   0
    2 2018  5 1 700   0
    2 2018  6 1 701 701
    2 2018  7 1 702 701
    2 2018  8 1 703 701
    2 2018  9 1 704 701
    2 2018 10 1 705 701
    2 2018 11 1 706 701
    2 2018 12 1 707 701
    2 2019  1 1 708 701
    2 2019  2 1 709   0
    2 2019  3 1 710   0
    2 2019  4 1 711   0
    2 2019  5 1 712   0
    2 2019  6 1 713   0
    2 2019  7 1 714   0
    2 2019  8 1 715   0
    2 2019  9 1 716   0
    2 2019 10 1 717   0
    2 2019 11 1 718   0
    2 2019 12 1 719   0
    3 2017  1 0 684   0
    3 2017  2 0 685   0
    3 2017  3 0 686   0
    3 2017  4 0 687   0
    3 2017  5 0 688   0
    3 2017  6 0 689   0
    3 2017  7 0 690   0
    3 2017  8 0 691   0
    3 2017  9 0 692   0
    3 2017 10 0 693   0
    3 2017 11 0 694   0
    3 2017 12 0 695   0
    3 2018  1 0 696   0
    3 2018  2 0 697   0
    3 2018  3 0 698   0
    3 2018  4 0 699   0
    3 2018  5 0 700   0
    3 2018  6 0 701   0
    3 2018  7 0 702   0
    3 2018  8 0 703   0
    3 2018  9 0 704   0
    3 2018 10 0 705   0
    3 2018 11 0 706   0
    3 2018 12 0 707   0
    3 2019  1 0 708   0
    3 2019  2 0 709   0
    3 2019  3 0 710   0
    3 2019  4 0 711   0
    end

  • #2
    In addition to my previous post, when I try
    Code:
    format first_treat %tm
    the zero values in first_treat change to 1960m1. Can somebody help me out here?

    Best,

    Comment


    • #3
      Indeed. Your zeros don't mean January 1960, so why do that? That is a serious question, not a flippant comment. I get that you want to see monthly dates as such, but in what way? In a table or a graph or some other way?

      Comment


      • #4
        Dear Nick, thank you for the comment. I am just trying to figure out how to get a variable that has both a date (when treatment firstly occurred) and 0 (when not treated or untreated).

        In the example below, taken from csdi help, the authors are dealing with annual data. So the variable first_treat is either 0, 2004, 2006, 2007. I am trying to do the same with monthly data. Belo is a sample of their data.

        Best,


        Code:
         tab first_treat
        
        first.treat |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  0 |      1,545       61.80       61.80
               2004 |        100        4.00       65.80
               2006 |        200        8.00       73.80
               2007 |        655       26.20      100.00
        ------------+-----------------------------------
              Total |      2,500      100.00
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int year long countyreal double(lpop lemp) int first_treat byte treat
        2003  8001  5.896760933305299  8.461469042643875 2007 1
        2004  8001  5.896760933305299  8.336869637284956 2007 1
        2005  8001  5.896760933305299  8.340217320947035 2007 1
        2006  8001  5.896760933305299   8.37816098272068 2007 1
        2007  8001  5.896760933305299  8.487352349405215 2007 1
        2003  8019  2.232377197950549  4.997212273764115 2007 1
        2004  8019  2.232377197950549  5.081404364984463 2007 1
        2005  8019  2.232377197950549  4.787491742782046 2007 1
        2006  8019  2.232377197950549  4.990432586778736 2007 1
        2007  8019  2.232377197950549 5.0369526024136295 2007 1
        2003  8023 1.2982824837966773 2.1972245773362196 2007 1
        2004  8023 1.2982824837966773  2.302585092994046 2007 1
        2005  8023 1.2982824837966773  2.772588722239781 2007 1
        2006  8023 1.2982824837966773 1.9459101490553132 2007 1
        2007  8023 1.2982824837966773  2.995732273553991 2007 1
        2003  8029 3.3262582949976616  5.883322388488279 2007 1
        2004  8029 3.3262582949976616 5.7899601708972535 2007 1
        2005  8029 3.3262582949976616  5.857933154483459 2007 1
        2006  8029 3.3262582949976616  5.998936561946683 2007 1
        2007  8029 3.3262582949976616  6.104793232414985 2007 1
        2003  8041  6.247905534323346  8.968650903374874 2007 1
        2004  8041  6.247905534323346  8.915700818956903 2007 1
        2005  8041  6.247905534323346  8.900957872545062 2007 1
        2006  8041  6.247905534323346  8.902727664035522 2007 1
        2007  8041  6.247905534323346  8.881002624255569 2007 1
        2003  8063   2.08081559723298  4.941642422609304 2007 1
        2004  8063   2.08081559723298  4.867534450455582 2007 1
        2005  8063   2.08081559723298  4.663439094112067 2007 1
        2006  8063   2.08081559723298  4.812184355372417 2007 1
        2007  8063   2.08081559723298  4.919980925828125 2007 1
        2003  8075   3.02061998906177  5.697093486505405 2007 1
        2004  8075   3.02061998906177  5.631211781821365 2007 1
        2005  8075   3.02061998906177  5.720311776607412 2007 1
        2006  8075   3.02061998906177  5.796057750765372 2007 1
        2007  8075   3.02061998906177  5.560681631015528 2007 1
        2003  8103 1.7894234094305073  4.454347296253507 2007 1
        2004  8103 1.7894234094305073  4.584967478670572 2007 1
        2005  8103 1.7894234094305073 4.7535901911063645 2007 1
        2006  8103 1.7894234094305073   4.90527477843843 2007 1
        2007  8103 1.7894234094305073  4.718498871295094 2007 1
        2003  8117  3.159040891166015 6.3784261836515865 2007 1
        2004  8117  3.159040891166015  6.274762021241939 2007 1
        2005  8117  3.159040891166015  6.284134161070802 2007 1
        2006  8117  3.159040891166015 6.3818160174060985 2007 1
        2007  8117  3.159040891166015  6.431331081933479 2007 1
        2003  8123 5.1981433775775105  7.755338812846501 2007 1
        2004  8123 5.1981433775775105  7.687997166393016 2007 1
        2005  8123 5.1981433775775105  7.754052639035757 2007 1
        2006  8123 5.1981433775775105  7.789454566086673 2007 1
        2007  8123 5.1981433775775105  7.803026643632217 2007 1
        2003 12007  3.261475438487018  5.501258210544727 2006 1
        2004 12007  3.261475438487018  5.594711379601839 2006 1
        2005 12007  3.261475438487018  5.247024072160486 2006 1
        2006 12007  3.261475438487018   5.43372200355424 2006 1
        2007 12007  3.261475438487018  5.442417710521793 2006 1
        2003 12019 4.9474398705991085  7.772752716468742 2006 1
        2004 12019 4.9474398705991085  7.811163385025279 2006 1
        2005 12019 4.9474398705991085 7.8013913202914855 2006 1
        2006 12019 4.9474398705991085 7.8632667240095735 2006 1
        2007 12019 4.9474398705991085  7.870165946469845 2006 1
        2003 12023  4.034470700181672   6.99117688712121 2006 1
        2004 12023  4.034470700181672   6.53813982376767 2006 1
        2005 12023  4.034470700181672  6.590301048196686 2006 1
        2006 12023  4.034470700181672  6.551080335043404 2006 1
        2007 12023  4.034470700181672  6.717804695023691 2006 1
        2003 12029  2.626623202406481 3.8501476017100584 2006 1
        2004 12029  2.626623202406481   4.07753744390572 2006 1
        2005 12029  2.626623202406481  4.356708826689592 2006 1
        2006 12029  2.626623202406481   4.31748811353631 2006 1
        2007 12029  2.626623202406481  4.330733340286331 2006 1
        2003 12051   3.58933532377755   6.42648845745769 2006 1
        2004 12051   3.58933532377755  6.423246963533519 2006 1
        2005 12051   3.58933532377755  6.373319789577012 2006 1
        2006 12051   3.58933532377755  6.411818267709897 2006 1
        2007 12051   3.58933532377755  6.373319789577012 2006 1
        2003 12071  6.088790874937479  8.758726607842037 2006 1
        2004 12071  6.088790874937479  8.798454696065095 2006 1
        2005 12071  6.088790874937479   8.99118884193151 2006 1
        2006 12071  6.088790874937479  9.151651375627504 2006 1
        2007 12071  6.088790874937479   9.06843112579349 2006 1
        2003 12087  4.376875892346313  6.723832440821209 2006 1
        2004 12087  4.376875892346313  6.792344427470809 2006 1
        2005 12087  4.376875892346313 6.7580945044277305 2006 1
        2006 12087  4.376875892346313 6.7464121285733745 2006 1
        2007 12087  4.376875892346313  6.747586526829315 2006 1
        2003 12093 3.5810158082379915  5.723585101952381 2006 1
        2004 12093 3.5810158082379915 6.0112671744041615 2006 1
        2005 12093 3.5810158082379915  6.102558594613569 2006 1
        2006 12093 3.5810158082379915  6.091309882077698 2006 1
        2007 12093 3.5810158082379915  6.144185634125646 2006 1
        2003 12097  5.150356655937891  7.590852123688581 2006 1
        2004 12097  5.150356655937891  7.593877844605118 2006 1
        2005 12097  5.150356655937891  7.765569081097317 2006 1
        2006 12097  5.150356655937891  7.873217054862741 2006 1
        2007 12097  5.150356655937891  7.847371836159788 2006 1
        2003 12099  7.031020150770707   9.76864100976636 2006 1
        2004 12099  7.031020150770707  9.629182277125986 2006 1
        2005 12099  7.031020150770707  9.722025626154055 2006 1
        2006 12099  7.031020150770707  9.816239783971849 2006 1
        2007 12099  7.031020150770707  9.893639216481313 2006 1
        end

        Comment


        • #5
          I was asking on behalf of anyone who might work on this kind of problem and specifically with this command, not me on either score So, to be clear, I leave this open for anybody qualifying.

          Comment


          • #6
            Code:
            drop date    // going to fix this
            g date = ym(year, month)
            format date %tm 
            format first_treat %tm
            bys id: g temp = date if first_treat != 0
            replace first_treat = date if mi(temp)    // fix wrong dates
            bys id: egen treatdate = min(temp)
            capture drop temp
            format treatdate %tm
            g time = (date - treatdate) + 1
            replace time = 0 if time <= 0

            Comment


            • #7
              Dear George,

              Thank you for this code. But I still do not have the zeros outside the treatment period (2018m3-2019m2).
              I tried to change your code using the line below, but it didn't work.
              Code:
              replace first_treat= 0 if missing(temp)
              Best,

              Guyle

              Comment


              • #8
                The last line of my code should ensure that if the rest is done correctly

                Watch the data step by step and see if there's a undesirable calculation somewhere.

                Comment

                Working...
                X