Announcement

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

  • How to drop countries from a database ?

    Hello,



    How can I drop certain countries from a stata database?

    I have written:
    drop if country !=12 & country !=32 & country !=76

    But stata tells me:
    country not found
    r(111);

    12, 32, 76 correspond to the country code number, but it didn't work !




  • #2
    Welcome to the Forum, Lola. Please use "CODE" delimiters to report exactly what you typed and exactly what you got back (see the FAQ -- hit the black strip at top of the page). On the basis of what you've shown us, it's hard to diagnose what is going on. What happens if you run the following code? (Copy it to the do-file editor and then run it, and give us everything shown in the Results window.)

    Code:
    describe country
    summarize country
    drop if country !=12 & country !=32 & country !=76

    Comment


    • #3
      seems to me that you simply mis-named your variable. Perhaps its label is "country" but Stata is telling you it can't find a variable called "country." Remember, it is case-sensitive, so Country is different from COUNTRY is different from country.

      Comment


      • #4
        Lola:
        as Others have already suggested, I would recommend you to check if your -country- variable starts with an upper-case letter (as Stata is case-sensitive).
        Otherwise, following FAQ guidelines, you may want to post what you typed and what Stata gave you back (with some further details) using Code delimiters (# button among the Advanced editor options, A-button).

        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Hello. In regard to my below database, I want to drop some countries for range of period such as Africa (total), Kosovo, Jersey, Isle Of Man, Guernsey, Europe (total), Eswatini, EU/EEA (total), Cote Divoire, British Virgin Islands, Montserrat, Monaco, North Macedonia, Northern Mariana Islands, United States Virgin Islands, Trinidad And Tobago.

          I ran this code directly, and the result was an error.

          drop country if country !=20 & country !=63 & country !=179

          Would you please guide me in this step?

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str33 country float mdate double(n_cases n_deaths) str3 country_code
          "Afghanistan"     720        0      0 "AFG"
          "Afghanistan"     721        1      0 "AFG"
          "Afghanistan"     722      113      4 "AFG"
          "Afghanistan"     723     2590     81 "AFG"
          "Afghanistan"     724    12501    172 "AFG"
          "Afghanistan"     725    15762    464 "AFG"
          "Afghanistan"     726     5743    563 "AFG"
          "Afghanistan"     727     1452    118 "AFG"
          "Afghanistan"     728     1179     60 "AFG"
          "Afghanistan"     729     2292     79 "AFG"
          "Afghanistan"     730     4211    222 "AFG"
          "Afghanistan"     731     7065    467 "AFG"
          "Afghanistan"     732     2450    183 "AFG"
          "Afghanistan"     733      517     38 "AFG"
          "Afghanistan"     734      903     61 "AFG"
          "Afghanistan"     735     5063    174 "AFG"
          "Africa (total)"  720        0      0 ""   
          "Africa (total)"  721        6      0 ""   
          "Africa (total)"  722     4623    143 ""   
          "Africa (total)"  723    39806   1656 ""   
          "Africa (total)"  724   102561   2423 ""   
          "Africa (total)"  725   235734   5438 ""   
          "Africa (total)"  726   575100  10611 ""   
          "Africa (total)"  727   288355   9314 ""   
          "Africa (total)"  728   270721   6922 ""   
          "Africa (total)"  729   279842   6695 ""   
          "Africa (total)"  730   367415   8524 ""   
          "Africa (total)"  731   668590  15554 ""   
          "Africa (total)"  732   840428  27848 ""   
          "Africa (total)"  733   297189  10660 ""   
          "Africa (total)"  734   298318   8010 ""   
          "Africa (total)"  735   366131  10629 ""   
          "Albania"         722      212     10 "ALB"
          "Albania"         723      583     21 "ALB"
          "Albania"         724      342      2 "ALB"
          "Albania"         725     1265     22 "ALB"
          "Albania"         726     3117    111 "ALB"
          "Albania"         727     3861    114 "ALB"
          "Albania"         728     4886    116 "ALB"
          "Albania"         729     6936    122 "ALB"
          "Albania"         730    16423    280 "ALB"
          "Albania"         731    21813    395 "ALB"
          "Albania"         732    25898    267 "ALB"
          "Albania"         733    27561    479 "ALB"
          "Albania"         734    13634    326 "ALB"
          "Albania"         735     5192    147 "ALB"
          "Algeria"         720        0      0 "DZA"
          "Algeria"         721        3      0 "DZA"
          "Algeria"         722      508     31 "DZA"
          "Algeria"         723     3963    432 "DZA"
          "Algeria"         724     4920    190 "DZA"
          "Algeria"         725     3879    244 "DZA"
          "Algeria"         726    18192    334 "DZA"
          "Algeria"         727    12681    270 "DZA"
          "Algeria"         728     7990    259 "DZA"
          "Algeria"         729     6136    213 "DZA"
          "Algeria"         730    23949    437 "DZA"
          "Algeria"         731    18187    362 "DZA"
          "Algeria"         732     8680    142 "DZA"
          "Algeria"         733     5294    104 "DZA"
          "Algeria"         734     3357     90 "DZA"
          "Algeria"         735     6365    220 "DZA"
          "America (total)" 720       17      0 ""   
          "America (total)" 721      120      2 ""   
          "America (total)" 722   165815   3041 ""   
          "America (total)" 723  1319861  87393 ""   
          "America (total)" 724  1443698  89631 ""   
          "America (total)" 725  2243103  94643 ""   
          "America (total)" 726  4630298 129494 ""   
          "America (total)" 727  3623472 108691 ""   
          "America (total)" 728  3823823 111132 ""   
          "America (total)" 729  3529362  72709 ""   
          "America (total)" 730  6005330  83967 ""   
          "America (total)" 731 10302162 156619 ""   
          "America (total)" 732 10461529 236289 ""   
          "America (total)" 733  4600047 156329 ""   
          "America (total)" 734  5066572 144829 ""   
          "America (total)" 735  6967170 201575 ""   
          "Andorra"         722      334      6 "AND"
          "Andorra"         723      414     39 "AND"
          "Andorra"         724       16      6 "AND"
          "Andorra"         725       91      1 "AND"
          "Andorra"         726       70      0 "AND"
          "Andorra"         727      199      1 "AND"
          "Andorra"         728      986      0 "AND"
          "Andorra"         729     2715     22 "AND"
          "Andorra"         730     1887      1 "AND"
          "Andorra"         731     1480      8 "AND"
          "Andorra"         732     2059     22 "AND"
          "Andorra"         733      818      6 "AND"
          "Andorra"         734     1162      5 "AND"
          "Andorra"         735     1192     10 "AND"
          "Angola"          722        7      2 "AGO"
          "Angola"          723       28      0 "AGO"
          "Angola"          724       51      2 "AGO"
          "Angola"          725      181      7 "AGO"
          "Angola"          726      881     41 "AGO"
          "Angola"          727     1476     55 "AGO"
          "Angola"          728     2746     86 "AGO"
          "Angola"          729     5435     91 "AGO"
          end
          format %tm mdate
          Furthermore, I want to set up zero for all period of 2019. Could you also help me for that?

          Comment


          • #6
            Rana:
            your code should be fed with numerical values, whereas it seems that country ID is in -string- format.
            That's why Stata throws the error mesage you complain about.
            You should code:
            Code:
            encode country,gen(country_id)
            Please remember, quoting part of the Description paragraph of the -encode- entry, Stata .pdf manual:
            Do not use encode if varname contains numbers that merely happen to be stored as string
            And then you can -drop- what you like to.
            That said, it is way safer to flag the countries you are not interested in via an -if- qualifier:
            Code:
            gen wanted=1 if country !=20 & country !=63 & country !=179
            Last edited by Carlo Lazzaro; 01 Oct 2021, 05:19.
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Dear Carlo thanks for your text. I ran this code, as you typed
              gen wanted=1 if country !=20 & country !=63 & country !=179 But I got this error!!! type mismatch r(109);

              Comment


              • #8
                Rana:
                have you previously run the following code?
                Code:
                encode country,gen(country_id)
                Kind regards,
                Carlo
                (Stata 19.0)

                Comment


                • #9
                  Carlo:
                  Yes, firstly I ran the code you mentioned in #8, then I ran the next. Also I did not figure out why we run the code gen wanted=1 ?

                  Comment


                  • #10
                    Rana:
                    my mistake indeed. Sorry for that.
                    Run instead:
                    Code:
                    gen wanted=1 if country_code!=20 & country_code!=63 & country_code!=179
                    Kind regards,
                    Carlo
                    (Stata 19.0)

                    Comment


                    • #11
                      Dear Carlo:
                      Again I got this error: type mismatch
                      r(109);
                      But when I changed country_code to country_id in Data Editor created one more column with number 1. Now, my question is why should I run "gen wanted=1 if..." while I want to drop those countries?

                      Comment


                      • #12
                        There was a slight typo in the Carlo's code but you were able to spot it in #11. The code to generated the -wanted- variable is called an indicator variable. Here it is meant to flag countries you want to drop. There are good reasons for doing this:
                        1) checking to make sure you have identified the correct countries
                        2) keeping the entire dataset, but having a variable that can be used to operate on a subset
                        3) an idicator can then be used to drop a subset, for example -drop if wanted-

                        Unless you are very confident in what you are doing, it's is risky to simply drop data without verifying it is what was desired.

                        Comment


                        • #13
                          Thanks Leonardo:
                          I want to drop the countries for whole range. Not only for one period. You can see it in #5 . So I think I should use another code for that, should not I?

                          Comment


                          • #14
                            Yes, you can drop it as suggested in #12.

                            Code:
                            drop if wanted  // <-  drop the subset of countries
                            drop waned     // <-- drop the indicator of that subset

                            Comment


                            • #15
                              According to my data when I run the mention code. It dose not work and in the result window I got error.

                              I am running these code:
                              encode country,gen(country_id)
                              gen wanted=1 if country_code!=20 & country_code!=63 & country_code!=179



                              Code:
                              * Example generated by -dataex-. To install: ssc install dataex
                              clear
                              input str44 country float mdate double(n_cases n_deaths) str3 country_code long country_id
                              "Afghanistan"     720        0      0 "AFG" 1
                              "Afghanistan"     721        1      0 "AFG" 1
                              "Afghanistan"     722      113      4 "AFG" 1
                              "Afghanistan"     723     2590     81 "AFG" 1
                              "Afghanistan"     724    12501    172 "AFG" 1
                              "Afghanistan"     725    15762    464 "AFG" 1
                              "Afghanistan"     726     5743    563 "AFG" 1
                              "Afghanistan"     727     1452    118 "AFG" 1
                              "Afghanistan"     728     1179     60 "AFG" 1
                              "Afghanistan"     729     2292     79 "AFG" 1
                              "Afghanistan"     730     4211    222 "AFG" 1
                              "Afghanistan"     731     7065    467 "AFG" 1
                              "Afghanistan"     732     2450    183 "AFG" 1
                              "Afghanistan"     733      517     38 "AFG" 1
                              "Afghanistan"     734      903     61 "AFG" 1
                              "Afghanistan"     735     5063    174 "AFG" 1
                              "Africa (total)"  720        0      0 ""    2
                              "Africa (total)"  721        6      0 ""    2
                              "Africa (total)"  722     4623    143 ""    2
                              "Africa (total)"  723    39806   1656 ""    2
                              "Africa (total)"  724   102561   2423 ""    2
                              "Africa (total)"  725   235734   5438 ""    2
                              "Africa (total)"  726   575100  10611 ""    2
                              "Africa (total)"  727   288355   9314 ""    2
                              "Africa (total)"  728   270721   6922 ""    2
                              "Africa (total)"  729   279842   6695 ""    2
                              "Africa (total)"  730   367415   8524 ""    2
                              "Africa (total)"  731   668590  15554 ""    2
                              "Africa (total)"  732   840428  27848 ""    2
                              "Africa (total)"  733   297189  10660 ""    2
                              "Africa (total)"  734   298318   8010 ""    2
                              "Africa (total)"  735   366131  10629 ""    2
                              "Albania"         722      212     10 "ALB" 3
                              "Albania"         723      583     21 "ALB" 3
                              "Albania"         724      342      2 "ALB" 3
                              "Albania"         725     1265     22 "ALB" 3
                              "Albania"         726     3117    111 "ALB" 3
                              "Albania"         727     3861    114 "ALB" 3
                              "Albania"         728     4886    116 "ALB" 3
                              "Albania"         729     6936    122 "ALB" 3
                              "Albania"         730    16423    280 "ALB" 3
                              "Albania"         731    21813    395 "ALB" 3
                              "Albania"         732    25898    267 "ALB" 3
                              "Albania"         733    27561    479 "ALB" 3
                              "Albania"         734    13634    326 "ALB" 3
                              "Albania"         735     5192    147 "ALB" 3
                              "Algeria"         720        0      0 "DZA" 4
                              "Algeria"         721        3      0 "DZA" 4
                              "Algeria"         722      508     31 "DZA" 4
                              "Algeria"         723     3963    432 "DZA" 4
                              "Algeria"         724     4920    190 "DZA" 4
                              "Algeria"         725     3879    244 "DZA" 4
                              "Algeria"         726    18192    334 "DZA" 4
                              "Algeria"         727    12681    270 "DZA" 4
                              "Algeria"         728     7990    259 "DZA" 4
                              "Algeria"         729     6136    213 "DZA" 4
                              "Algeria"         730    23949    437 "DZA" 4
                              "Algeria"         731    18187    362 "DZA" 4
                              "Algeria"         732     8680    142 "DZA" 4
                              "Algeria"         733     5294    104 "DZA" 4
                              "Algeria"         734     3357     90 "DZA" 4
                              "Algeria"         735     6365    220 "DZA" 4
                              "America (total)" 720       17      0 ""    5
                              "America (total)" 721      120      2 ""    5
                              "America (total)" 722   165815   3041 ""    5
                              "America (total)" 723  1319861  87393 ""    5
                              "America (total)" 724  1443698  89631 ""    5
                              "America (total)" 725  2243103  94643 ""    5
                              "America (total)" 726  4630298 129494 ""    5
                              "America (total)" 727  3623472 108691 ""    5
                              "America (total)" 728  3823823 111132 ""    5
                              "America (total)" 729  3529362  72709 ""    5
                              "America (total)" 730  6005330  83967 ""    5
                              "America (total)" 731 10302162 156619 ""    5
                              "America (total)" 732 10461529 236289 ""    5
                              "America (total)" 733  4600047 156329 ""    5
                              "America (total)" 734  5066572 144829 ""    5
                              "America (total)" 735  6967170 201575 ""    5
                              "Andorra"         722      334      6 "AND" 6
                              "Andorra"         723      414     39 "AND" 6
                              "Andorra"         724       16      6 "AND" 6
                              "Andorra"         725       91      1 "AND" 6
                              "Andorra"         726       70      0 "AND" 6
                              "Andorra"         727      199      1 "AND" 6
                              "Andorra"         728      986      0 "AND" 6
                              "Andorra"         729     2715     22 "AND" 6
                              "Andorra"         730     1887      1 "AND" 6
                              "Andorra"         731     1480      8 "AND" 6
                              "Andorra"         732     2059     22 "AND" 6
                              "Andorra"         733      818      6 "AND" 6
                              "Andorra"         734     1162      5 "AND" 6
                              "Andorra"         735     1192     10 "AND" 6
                              "Angola"          722        7      2 "AGO" 7
                              "Angola"          723       28      0 "AGO" 7
                              "Angola"          724       51      2 "AGO" 7
                              "Angola"          725      181      7 "AGO" 7
                              "Angola"          726      881     41 "AGO" 7
                              "Angola"          727     1476     55 "AGO" 7
                              "Angola"          728     2746     86 "AGO" 7
                              "Angola"          729     5435     91 "AGO" 7
                              end
                              format %tm mdate
                              label values country_id country_id
                              label def country_id 1 "Afghanistan", modify
                              label def country_id 2 "Africa (total)", modify
                              label def country_id 3 "Albania", modify
                              label def country_id 4 "Algeria", modify
                              label def country_id 5 "America (total)", modify
                              label def country_id 6 "Andorra", modify
                              label def country_id 7 "Angola", modify

                              Comment

                              Working...
                              X