Announcement

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

  • How to delete a subgroup based on two conditions?

    Dear all Statalist experts,

    I have a dataset as below

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float year1 long TYPE2 float(treat1 FAT_w1 post33 post33treat1 indicator)
    2016  2 0         . 0 0 .
    2016  3 .         . 0 . .
    2017  3 . 105.86112 0 . .
    2018  3 . 169.16325 0 . .
    2021  3 .         . 1 . .
    2019  4 1         . 1 1 1
    2020  4 1  6.876883 1 1 .
    2017  6 1         . 0 0 .
    2018  6 1 2.2230647 0 0 .
    2019  6 1 2.0798314 1 1 1
    2020  6 1  1.665593 1 1 .
    2021  6 1 1.4405484 1 1 .
    2016  7 1         . 0 0 .
    2017  7 1  9.328573 0 0 .
    2018  7 1 14.143867 0 0 .
    2019  7 1 16.777643 1 1 1
    2020  7 1 19.836254 1 1 .
    2021  7 1 13.363945 1 1 .
    2016 10 0         . 0 0 .
    2019 10 0         . 1 0 .
    2020 10 0 10.358336 1 0 .
    2016 11 0         . 0 0 .
    2017 11 0  .6820889 0 0 .
    2018 11 0  .8348646 0 0 .
    2019 11 0 1.1720647 1 0 .
    2020 11 0 1.3943592 1 0 .
    2021 11 0  1.464206 1 0 .
    2017 13 0         . 0 0 .
    2018 13 0 14.170573 0 0 .
    2019 13 0 17.122456 1 0 .
    2020 13 0 10.125214 1 0 .
    2016 15 .         . 0 . .
    2019 15 .         . 1 . .
    2020 15 .  5.240753 1 . .
    2021 15 .  3.297071 1 . .
    2016 16 .         . 0 . .
    2017 16 . 1.4334356 0 . .
    2018 16 . 1.8452812 0 . .
    2019 16 . 2.1694336 1 . .
    2020 16 . 2.2726302 1 . .
    2021 16 .  2.469463 1 . .
    2020 18 1         . 1 1 1
    2021 18 1  20.48241 1 1 .
    end
    label values TYPE2 TYPE2
    label def TYPE2 2 "2557XG", modify
    label def TYPE2 3 "2563UY", modify
    label def TYPE2 4 "2563UZ", modify
    label def TYPE2 6 "2580PG", modify
    label def TYPE2 7 "25846A", modify
    label def TYPE2 10 "2621N5", modify
    label def TYPE2 11 "2622UU", modify
    label def TYPE2 13 "2625KH", modify
    label def TYPE2 15 "2631U4", modify
    label def TYPE2 16 "2633HC", modify
    label def TYPE2 18 "2634G2", modify
    Now I want to delete all observations of a TYPE2 if this TYPE2 has (indicator==1 and year1 !=2019). In short, indicator is the first observation for each TYPE2 where post33treat1=1 and year1 is the real year. In specific, for the sample above, the last two observations will be deleted (because indicator==1 when year1=2020). Please let me know if my dataset is insufficient or my description is unclear.

    Thanks in advance.




  • #2
    Code:
    bys TYPE2 (year1): drop if indicator[1]==1 & year[1]!=2019

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      Code:
      bys TYPE2 (year1): drop if indicator[1]==1 & year[1]!=2019
      Thank you, Andrew Musau , I got your point

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        Code:
        bys TYPE2 (year1): drop if indicator[1]==1 & year[1]!=2019
        Dear Andrew Musau , the code above does not work in this case

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float year1 long TYPE2 float(treat1 FAT_w1 post33 post33treat1)
        2017 42 1         . 0 0
        2018 42 1 1.7801197 0 0
        2019 42 1  1.889272 1 1
        2020 42 1  1.994993 1 1
        2021 42 1 1.2203997 1 1
        2014 43 1         . . .
        2015 43 1  5.300401 . .
        2016 43 1   6.44938 0 0
        2016 48 .         . 0 .
        2017 48 .  .4760473 0 .
        2018 48 .  .8327075 0 .
        2019 48 .  .9333832 1 .
        2020 48 .  .9829372 1 .
        2021 48 .  .8865142 1 .
        2011 49 1         . . .
        2012 49 1  6.528414 . .
        2013 49 1  5.831755 . .
        2014 49 1  6.418023 . .
        2015 49 1  7.042492 . .
        2021 49 1         . 1 1
        end
        label values TYPE2 TYPE2
        label def TYPE2 42 "2753V5", modify
        label def TYPE2 43 "2755CL", modify
        label def TYPE2 48 "2780XD", modify
        label def TYPE2 49 "2783RK", modify
        When performing your code, the TYPE2:2783RK data still exist, I deem that should we need to sort the data first then?

        The code I used to generate indicator variable is:

        Code:
        bysort TYPE2 post33treat1 (year): generate indicator=1 if _n==1 & post33treat1==1
        And I deem that it is the reason that the order of observation in each TYPES no longer ascending or descending
        Last edited by Phuc Nguyen; 15 Sep 2022, 18:10.

        Comment


        • #5
          Originally posted by Andrew Musau View Post
          Code:
          bys TYPE2 (year1): drop if indicator[1]==1 & year[1]!=2019
          Dear Andrew Musau , apart from that, I am wondering where the year variable coming from in your code. In my input there is only variable year1 rather than year. Could you please let me know if I miss anything?

          Code:
           
           bys TYPE2 (year1): drop if indicator[1]==1 & year[1]!=2019
          Best regards

          Comment


          • #6
            Originally posted by Phuc Nguyen View Post

            Dear Andrew Musau , apart from that, I am wondering where the year variable coming from in your code. In my input there is only variable year1 rather than year. Could you please let me know if I miss anything?

            Code:
            bys TYPE2 (year1): drop if indicator[1]==1 & year[1]!=2019
            Best regards
            Stata allows variable abbreviation, so I can call the variable "year1" year, provided that there is no other variable in the dataset that has this prefix. See

            Code:
             help set varabbrev

            When performing your code, the TYPE2:2783RK data still exist, I deem that should we need to sort the data first then?

            The code I used to generate indicator variable is:

            Code:
            bysort TYPE2 post33treat1 (year): generate indicator=1 if _n==1 & post33treat1==1
            And I deem that it is the reason that the order of observation in each TYPES no longer ascending or descending
            Sorry, I missed out on the second condition (highlighted):

            Code:
             In short, indicator is the first observation for each TYPE2 where post33treat1=1 and year1 is the real year.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float year1 long TYPE2 float(treat1 FAT_w1 post33 post33treat1 indicator)
            2017 42 1         . 0 0 0
            2018 42 1 1.7801197 0 0 0
            2019 42 1  1.889272 1 1 1
            2020 42 1  1.994993 1 1 1
            2021 42 1 1.2203997 1 1 1
            2014 43 1         . . . .
            2015 43 1  5.300401 . . .
            2016 43 1   6.44938 0 0 .
            2016 48 .         . 0 . .
            2017 48 .  .4760473 0 . .
            2018 48 .  .8327075 0 . .
            2019 48 .  .9333832 1 . .
            2020 48 .  .9829372 1 . .
            2021 48 .  .8865142 1 . .
            2011 49 1         . . . .
            2012 49 1  6.528414 . . 0
            2013 49 1  5.831755 . . 0
            2014 49 1  6.418023 . . 0
            2015 49 1  7.042492 . . 0
            2021 49 1         . 1 1 1
            end
            label values TYPE2 TYPE2
            label def TYPE2 42 "2753V5", modify
            label def TYPE2 43 "2755CL", modify
            label def TYPE2 48 "2780XD", modify
            label def TYPE2 49 "2783RK", modify
            
            bys TYPE2 (year1): g sumindicator= sum(indicator)
            bys TYPE2: egen todrop= total(sumindicator==1 & year!=2019)
            drop if todrop
            Res.:

            Code:
            . l, sepby(TY)
            
                 +---------------------------------------------------------------------------------------+
                 | year1    TYPE2   treat1     FAT_w1   post33   post33~1   indica~r   sumind~r   todrop |
                 |---------------------------------------------------------------------------------------|
              1. |  2017   2753V5        1          .        0          0          0          0        0 |
              2. |  2018   2753V5        1    1.78012        0          0          0          0        0 |
              3. |  2019   2753V5        1   1.889272        1          1          1          1        0 |
              4. |  2020   2753V5        1   1.994993        1          1          1          2        0 |
              5. |  2021   2753V5        1     1.2204        1          1          1          3        0 |
                 |---------------------------------------------------------------------------------------|
              6. |  2014   2755CL        1          .        .          .          .          0        0 |
              7. |  2015   2755CL        1   5.300401        .          .          .          0        0 |
              8. |  2016   2755CL        1    6.44938        0          0          .          0        0 |
                 |---------------------------------------------------------------------------------------|
              9. |  2016   2780XD        .          .        0          .          .          0        0 |
             10. |  2017   2780XD        .   .4760473        0          .          .          0        0 |
             11. |  2018   2780XD        .   .8327075        0          .          .          0        0 |
             12. |  2019   2780XD        .   .9333832        1          .          .          0        0 |
             13. |  2020   2780XD        .   .9829372        1          .          .          0        0 |
             14. |  2021   2780XD        .   .8865142        1          .          .          0        0 |
                 +---------------------------------------------------------------------------------------+
            
            . l, sepby(TYPE2)
            
                 +---------------------------------------------------------------------------------------+
                 | year1    TYPE2   treat1     FAT_w1   post33   post33~1   indica~r   sumind~r   todrop |
                 |---------------------------------------------------------------------------------------|
              1. |  2017   2753V5        1          .        0          0          0          0        0 |
              2. |  2018   2753V5        1    1.78012        0          0          0          0        0 |
              3. |  2019   2753V5        1   1.889272        1          1          1          1        0 |
              4. |  2020   2753V5        1   1.994993        1          1          1          2        0 |
              5. |  2021   2753V5        1     1.2204        1          1          1          3        0 |
                 |---------------------------------------------------------------------------------------|
              6. |  2014   2755CL        1          .        .          .          .          0        0 |
              7. |  2015   2755CL        1   5.300401        .          .          .          0        0 |
              8. |  2016   2755CL        1    6.44938        0          0          .          0        0 |
                 |---------------------------------------------------------------------------------------|
              9. |  2016   2780XD        .          .        0          .          .          0        0 |
             10. |  2017   2780XD        .   .4760473        0          .          .          0        0 |
             11. |  2018   2780XD        .   .8327075        0          .          .          0        0 |
             12. |  2019   2780XD        .   .9333832        1          .          .          0        0 |
             13. |  2020   2780XD        .   .9829372        1          .          .          0        0 |
             14. |  2021   2780XD        .   .8865142        1          .          .          0        0 |
                 +---------------------------------------------------------------------------------------+

            Comment


            • #7
              Originally posted by Andrew Musau View Post

              Stata allows variable abbreviation, so I can call the variable "year1" year, provided that there is no other variable in the dataset that has this prefix. See

              Code:
               help set varabbrev



              Sorry, I missed out on the second condition (highlighted):

              Code:
              In short, indicator is the first observation for each TYPE2 where post33treat1=1 and year1 is the real year.

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float year1 long TYPE2 float(treat1 FAT_w1 post33 post33treat1 indicator)
              2017 42 1 . 0 0 0
              2018 42 1 1.7801197 0 0 0
              2019 42 1 1.889272 1 1 1
              2020 42 1 1.994993 1 1 1
              2021 42 1 1.2203997 1 1 1
              2014 43 1 . . . .
              2015 43 1 5.300401 . . .
              2016 43 1 6.44938 0 0 .
              2016 48 . . 0 . .
              2017 48 . .4760473 0 . .
              2018 48 . .8327075 0 . .
              2019 48 . .9333832 1 . .
              2020 48 . .9829372 1 . .
              2021 48 . .8865142 1 . .
              2011 49 1 . . . .
              2012 49 1 6.528414 . . 0
              2013 49 1 5.831755 . . 0
              2014 49 1 6.418023 . . 0
              2015 49 1 7.042492 . . 0
              2021 49 1 . 1 1 1
              end
              label values TYPE2 TYPE2
              label def TYPE2 42 "2753V5", modify
              label def TYPE2 43 "2755CL", modify
              label def TYPE2 48 "2780XD", modify
              label def TYPE2 49 "2783RK", modify
              
              bys TYPE2 (year1): g sumindicator= sum(indicator)
              bys TYPE2: egen todrop= total(sumindicator==1 & year!=2019)
              drop if todrop
              Res.:

              Code:
              . l, sepby(TY)
              
              +---------------------------------------------------------------------------------------+
              | year1 TYPE2 treat1 FAT_w1 post33 post33~1 indica~r sumind~r todrop |
              |---------------------------------------------------------------------------------------|
              1. | 2017 2753V5 1 . 0 0 0 0 0 |
              2. | 2018 2753V5 1 1.78012 0 0 0 0 0 |
              3. | 2019 2753V5 1 1.889272 1 1 1 1 0 |
              4. | 2020 2753V5 1 1.994993 1 1 1 2 0 |
              5. | 2021 2753V5 1 1.2204 1 1 1 3 0 |
              |---------------------------------------------------------------------------------------|
              6. | 2014 2755CL 1 . . . . 0 0 |
              7. | 2015 2755CL 1 5.300401 . . . 0 0 |
              8. | 2016 2755CL 1 6.44938 0 0 . 0 0 |
              |---------------------------------------------------------------------------------------|
              9. | 2016 2780XD . . 0 . . 0 0 |
              10. | 2017 2780XD . .4760473 0 . . 0 0 |
              11. | 2018 2780XD . .8327075 0 . . 0 0 |
              12. | 2019 2780XD . .9333832 1 . . 0 0 |
              13. | 2020 2780XD . .9829372 1 . . 0 0 |
              14. | 2021 2780XD . .8865142 1 . . 0 0 |
              +---------------------------------------------------------------------------------------+
              
              . l, sepby(TYPE2)
              
              +---------------------------------------------------------------------------------------+
              | year1 TYPE2 treat1 FAT_w1 post33 post33~1 indica~r sumind~r todrop |
              |---------------------------------------------------------------------------------------|
              1. | 2017 2753V5 1 . 0 0 0 0 0 |
              2. | 2018 2753V5 1 1.78012 0 0 0 0 0 |
              3. | 2019 2753V5 1 1.889272 1 1 1 1 0 |
              4. | 2020 2753V5 1 1.994993 1 1 1 2 0 |
              5. | 2021 2753V5 1 1.2204 1 1 1 3 0 |
              |---------------------------------------------------------------------------------------|
              6. | 2014 2755CL 1 . . . . 0 0 |
              7. | 2015 2755CL 1 5.300401 . . . 0 0 |
              8. | 2016 2755CL 1 6.44938 0 0 . 0 0 |
              |---------------------------------------------------------------------------------------|
              9. | 2016 2780XD . . 0 . . 0 0 |
              10. | 2017 2780XD . .4760473 0 . . 0 0 |
              11. | 2018 2780XD . .8327075 0 . . 0 0 |
              12. | 2019 2780XD . .9333832 1 . . 0 0 |
              13. | 2020 2780XD . .9829372 1 . . 0 0 |
              14. | 2021 2780XD . .8865142 1 . . 0 0 |
              +---------------------------------------------------------------------------------------+
              Dear Andrew Musau

              Thank you for your big help. I learnt a lot from your help.

              When recoding myself, I conduct two lines of code that I think it is logically equally to your code:
              Code:
                   bysort TYPE2 post33treat1 (year1): generate indicator=1 if _n==1 & post33treat1==1
                  egen todrop= total(indicator==1 & year1!=2019) , by(TYPE2)
                   drop if todrop
              compared to yours

              Code:
               bys TYPE2 (year1): g sumindicator= sum(indicator) bys TYPE2: egen todrop= total(sumindicator==1 & year!=2019) drop if todrop
              I am wondering if my code is an acceptable alternative.
              Best regards.
              Last edited by Phuc Nguyen; 16 Sep 2022, 04:23. Reason: adjust the var name

              Comment


              • #8
                Originally posted by Andrew Musau View Post

                Stata allows variable abbreviation, so I can call the variable "year1" year, provided that there is no other variable in the dataset that has this prefix. See

                Code:
                 help set varabbrev



                Sorry, I missed out on the second condition (highlighted):

                Code:
                In short, indicator is the first observation for each TYPE2 where post33treat1=1 and year1 is the real year.

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input float year1 long TYPE2 float(treat1 FAT_w1 post33 post33treat1 indicator)
                2017 42 1 . 0 0 0
                2018 42 1 1.7801197 0 0 0
                2019 42 1 1.889272 1 1 1
                2020 42 1 1.994993 1 1 1
                2021 42 1 1.2203997 1 1 1
                2014 43 1 . . . .
                2015 43 1 5.300401 . . .
                2016 43 1 6.44938 0 0 .
                2016 48 . . 0 . .
                2017 48 . .4760473 0 . .
                2018 48 . .8327075 0 . .
                2019 48 . .9333832 1 . .
                2020 48 . .9829372 1 . .
                2021 48 . .8865142 1 . .
                2011 49 1 . . . .
                2012 49 1 6.528414 . . 0
                2013 49 1 5.831755 . . 0
                2014 49 1 6.418023 . . 0
                2015 49 1 7.042492 . . 0
                2021 49 1 . 1 1 1
                end
                label values TYPE2 TYPE2
                label def TYPE2 42 "2753V5", modify
                label def TYPE2 43 "2755CL", modify
                label def TYPE2 48 "2780XD", modify
                label def TYPE2 49 "2783RK", modify
                
                bys TYPE2 (year1): g sumindicator= sum(indicator)
                bys TYPE2: egen todrop= total(sumindicator==1 & year!=2019)
                drop if todrop
                Res.:

                Code:
                . l, sepby(TY)
                
                +---------------------------------------------------------------------------------------+
                | year1 TYPE2 treat1 FAT_w1 post33 post33~1 indica~r sumind~r todrop |
                |---------------------------------------------------------------------------------------|
                1. | 2017 2753V5 1 . 0 0 0 0 0 |
                2. | 2018 2753V5 1 1.78012 0 0 0 0 0 |
                3. | 2019 2753V5 1 1.889272 1 1 1 1 0 |
                4. | 2020 2753V5 1 1.994993 1 1 1 2 0 |
                5. | 2021 2753V5 1 1.2204 1 1 1 3 0 |
                |---------------------------------------------------------------------------------------|
                6. | 2014 2755CL 1 . . . . 0 0 |
                7. | 2015 2755CL 1 5.300401 . . . 0 0 |
                8. | 2016 2755CL 1 6.44938 0 0 . 0 0 |
                |---------------------------------------------------------------------------------------|
                9. | 2016 2780XD . . 0 . . 0 0 |
                10. | 2017 2780XD . .4760473 0 . . 0 0 |
                11. | 2018 2780XD . .8327075 0 . . 0 0 |
                12. | 2019 2780XD . .9333832 1 . . 0 0 |
                13. | 2020 2780XD . .9829372 1 . . 0 0 |
                14. | 2021 2780XD . .8865142 1 . . 0 0 |
                +---------------------------------------------------------------------------------------+
                
                . l, sepby(TYPE2)
                
                +---------------------------------------------------------------------------------------+
                | year1 TYPE2 treat1 FAT_w1 post33 post33~1 indica~r sumind~r todrop |
                |---------------------------------------------------------------------------------------|
                1. | 2017 2753V5 1 . 0 0 0 0 0 |
                2. | 2018 2753V5 1 1.78012 0 0 0 0 0 |
                3. | 2019 2753V5 1 1.889272 1 1 1 1 0 |
                4. | 2020 2753V5 1 1.994993 1 1 1 2 0 |
                5. | 2021 2753V5 1 1.2204 1 1 1 3 0 |
                |---------------------------------------------------------------------------------------|
                6. | 2014 2755CL 1 . . . . 0 0 |
                7. | 2015 2755CL 1 5.300401 . . . 0 0 |
                8. | 2016 2755CL 1 6.44938 0 0 . 0 0 |
                |---------------------------------------------------------------------------------------|
                9. | 2016 2780XD . . 0 . . 0 0 |
                10. | 2017 2780XD . .4760473 0 . . 0 0 |
                11. | 2018 2780XD . .8327075 0 . . 0 0 |
                12. | 2019 2780XD . .9333832 1 . . 0 0 |
                13. | 2020 2780XD . .9829372 1 . . 0 0 |
                14. | 2021 2780XD . .8865142 1 . . 0 0 |
                +---------------------------------------------------------------------------------------+
                Apart from that, it seesm that your dataset input is different from mine, I tried to import your dataset and see that
                Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	24.4 KB
ID:	1682226


                As mentioned, the indicator equal to 1 for the first observation of a TYPE2 that has post33treat1=1. In your case, 2753V5 has the indicator=1 for three continuous years and it seems to different from mine

                Apart from that, when I used my dataset at #4:

                Code:
                 
                 * Example generated by -dataex-. For more info, type help dataex clear input float year1 long TYPE2 float(treat1 FAT_w1 post33 post33treat1) 2017 42 1         . 0 0 2018 42 1 1.7801197 0 0 2019 42 1  1.889272 1 1 2020 42 1  1.994993 1 1 2021 42 1 1.2203997 1 1 2014 43 1         . . . 2015 43 1  5.300401 . . 2016 43 1   6.44938 0 0 2016 48 .         . 0 . 2017 48 .  .4760473 0 . 2018 48 .  .8327075 0 . 2019 48 .  .9333832 1 . 2020 48 .  .9829372 1 . 2021 48 .  .8865142 1 . 2011 49 1         . . . 2012 49 1  6.528414 . . 2013 49 1  5.831755 . . 2014 49 1  6.418023 . . 2015 49 1  7.042492 . . 2021 49 1         . 1 1 end label values TYPE2 TYPE2 label def TYPE2 42 "2753V5", modify label def TYPE2 43 "2755CL", modify label def TYPE2 48 "2780XD", modify label def TYPE2 49 "2783RK", modify
                After applying your code

                Code:
                bysort TYPE2 post33treat1 (year1): generate indicator=1 if _n==1 & post33treat1==1
                bys TYPE2 (year1): g sumindicator= sum(indicator)
                bys TYPE2: egen todrop= total(sumindicator==1 & year!=2019)
                drop if todrop
                Then all observations having post33treat1 just gone away

                Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	13.7 KB
ID:	1682227


                Could you please help me to sort it out?

                Many thanks in advance and best regards.

                Comment


                • #9
                  The problem with sorting on "post33treat1" is that this variable takes on the values 0, 1 and missing. Zeros are sorted first, then ones, then missings. As ones are sorted in the middle, any code that relies on the position of the ones (either sorted first or last) is bound to fail. If it were a 0/1 indicator, your idea would work perfectly. My code in #6 relies on the fact that you have created the variable "indicator" properly. If it does not achieve what you want, then it is because you had some errors when creating this variable. I would suggest that you provide a clean version of the data without having created some indicators for a complete solution. Use dataex as usual.

                  Comment


                  • #10
                    Originally posted by Andrew Musau View Post
                    The problem with sorting on "post33treat1" is that this variable takes on the values 0, 1 and missing. Zeros are sorted first, then ones, then missings. As ones are sorted in the middle, any code that relies on the position of the ones (either sorted first or last) is bound to fail. If it were a 0/1 indicator, your idea would work perfectly. My code in #6 relies on the fact that you have created the variable "indicator" properly. If it does not achieve what you want, then it is because you had some errors when creating this variable. I would suggest that you provide a clean version of the data without having created some indicators for a complete solution. Use dataex as usual.
                    Dear Andrew Musau
                    Thank you so much for your kind help, I am providing the whole dataset as attached.
                    A sample in the dataset attached is


                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input float year1 long TYPE2 float(treat1 FAT_w1 post33 post33treat1)
                    2017 42 1         . 0 0
                    2018 42 1 1.7801197 0 0
                    2019 42 1  1.889272 1 1
                    2020 42 1  1.994993 1 1
                    2021 42 1 1.2203997 1 1
                    2014 43 1         . . .
                    2015 43 1  5.300401 . .
                    2016 43 1   6.44938 0 0
                    2016 48 .         . 0 .
                    2017 48 .  .4760473 0 .
                    2018 48 .  .8327075 0 .
                    2019 48 .  .9333832 1 .
                    2020 48 .  .9829372 1 .
                    2021 48 .  .8865142 1 .
                    2011 49 1         . . .
                    2012 49 1  6.528414 . .
                    2013 49 1  5.831755 . .
                    2014 49 1  6.418023 . .
                    2015 49 1  7.042492 . .
                    2021 49 1         . 1 1
                    end
                    label values TYPE2 TYPE2
                    label def TYPE2 42 "2753V5", modify
                    label def TYPE2 43 "2755CL", modify
                    label def TYPE2 48 "2780XD", modify
                    label def TYPE2 49 "2783RK", modify

                    Many thanks and warmest regards.
                    Attached Files
                    Last edited by Phuc Nguyen; 16 Sep 2022, 07:33.

                    Comment


                    • #11
                      With a 0/1 indicator instead of a 0/1/missing variable, you can do the following:

                      Code:
                      * Example generated by -dataex-. For more info, type help dataex
                      clear
                      input float year1 long TYPE2 float(treat1 FAT_w1 post33 post33treat1)
                      2017 42 1         . 0 0
                      2018 42 1 1.7801197 0 0
                      2019 42 1  1.889272 1 1
                      2020 42 1  1.994993 1 1
                      2021 42 1 1.2203997 1 1
                      2014 43 1         . . .
                      2015 43 1  5.300401 . .
                      2016 43 1   6.44938 0 0
                      2016 48 .         . 0 .
                      2017 48 .  .4760473 0 .
                      2018 48 .  .8327075 0 .
                      2019 48 .  .9333832 1 .
                      2020 48 .  .9829372 1 .
                      2021 48 .  .8865142 1 .
                      2011 49 1         . . .
                      2012 49 1  6.528414 . .
                      2013 49 1  5.831755 . .
                      2014 49 1  6.418023 . .
                      2015 49 1  7.042492 . .
                      2021 49 1         . 1 1
                      end
                      label values TYPE2 TYPE2
                      label def TYPE2 42 "2753V5", modify
                      label def TYPE2 43 "2755CL", modify
                      label def TYPE2 48 "2780XD", modify
                      label def TYPE2 49 "2783RK", modify
                      
                      replace post33treat1=!post33treat1 if missing(post33treat1)
                      gsort TYPE2 -post33treat1 year1
                      by TYPE2: drop if post33treat1[1] & year1[1]!=2019


                      Res.:

                      Code:
                      . sort TYPE2 year1
                      
                      . l, sepby(TYPE2)
                      
                           +--------------------------------------------------------+
                           | year1    TYPE2   treat1     FAT_w1   post33   post33~1 |
                           |--------------------------------------------------------|
                        1. |  2017   2753V5        1          .        0          0 |
                        2. |  2018   2753V5        1    1.78012        0          0 |
                        3. |  2019   2753V5        1   1.889272        1          1 |
                        4. |  2020   2753V5        1   1.994993        1          1 |
                        5. |  2021   2753V5        1     1.2204        1          1 |
                           |--------------------------------------------------------|
                        6. |  2014   2755CL        1          .        .          0 |
                        7. |  2015   2755CL        1   5.300401        .          0 |
                        8. |  2016   2755CL        1    6.44938        0          0 |
                           |--------------------------------------------------------|
                        9. |  2016   2780XD        .          .        0          0 |
                       10. |  2017   2780XD        .   .4760473        0          0 |
                       11. |  2018   2780XD        .   .8327075        0          0 |
                       12. |  2019   2780XD        .   .9333832        1          0 |
                       13. |  2020   2780XD        .   .9829372        1          0 |
                       14. |  2021   2780XD        .   .8865142        1          0 |
                           +--------------------------------------------------------+
                      
                      .

                      Comment


                      • #12
                        Dear Andrew Musau

                        Thank you for helping me. The problem is sorted now.

                        Best regards.

                        Comment

                        Working...
                        X