Announcement

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

  • How to keep specific rows based on a list of strings

    Hello

    I am currently in a dataset with a country variable containing all countries in the world since 1960. The problem is, that I want to keep only 50 of these countries. I have tried with enlist, but this is limited to only 10 strings. So currently I am stuck. I need it to be a code which can be used on 2 other datasets with the exact same problem, and I don't want to manually delete possible 15000 rows.

    Do any of you have an idea to for instance keep if country == (the list below with country names)
    Angola
    Burundi
    Benin
    Burkina Faso
    Botswana
    Central African Republic
    Côte d'Ivoire
    Cameroon
    D.R. of the Congo
    Congo
    Comoros
    Cabo Verde
    Djibouti
    Algeria
    Egypt
    Ethiopia
    Gabon
    Ghana
    Guinea
    Gambia
    Guinea-Bissau
    Equatorial Guinea
    Kenya
    Liberia
    Lesotho
    Morocco
    Madagascar
    Mali
    Mozambique
    Mauritania
    Mauritius
    Malawi
    Namibia
    Niger
    Nigeria
    Rwanda
    Sudan
    Senegal
    Sierra Leone
    Sao Tome and Principe
    Eswatini
    Seychelles
    Chad
    Togo
    Tunisia
    U.R. of Tanzania: Mainland
    Uganda
    South Africa
    Zambia
    Zimbabwe

    Best regards Louise

  • #2
    Create a dataset with these names, then merge it to the big dataset, and then keep if _merge==3.

    Comment


    • #3
      Just to add, the current list may need to be tidied up. For example United Republic of Tanzania and Democratic Republic of the Congo, etc. were broken into two rows and can cause data merging error.

      And as a reminder, when you create this data set, make sure the country in this data and the country in the long data are of the same data type. For instance, if one is a character-based string variable, and one is numeric variable with labels, then the merging would not work.

      Comment


      • #4
        local countries Angola Burundi Benin Burkina Faso Botswana Central African Republic Cameroon Congo Congo Comoros Cabo Verde Djibouti Algeria Egypt Ethiopia Gabon Ghana Guinea Gambia Guinea-Bissau
        disp "`countries'"

        gen tokeep = .
        local n : word count `countries'
        forvalues i = 1(1)`n' {
        local str = word("`countries'", `i')
        replace tokeep = 1 if country=="`str'"
        }

        keep if tokeep==1
        cap drop tokeep

        Comment

        Working...
        X