Announcement

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

  • Keep multiple observations in a variable

    Dear Statalist

    I have a dataset that include 179 countries. However, i only want to keep 51 of them (Subsaharan Africa). When i run the "keep if var == ..." im not able to run it with all the countries at once. Is there an efficient way to only keep the 51 countries?
    Dataex:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str32 country_name double(country_id year v2csreprss v2csantimv)
    "Mexico"                     3 2020   1.09 -1.512
    "Suriname"                   4 2020  1.935    -.6
    "Sweden"                     5 2020  3.085  -.769
    "Switzerland"                6 2020  2.812 -2.439
    "Ghana"                      7 2020  2.012  -.387
    "South Africa"               8 2020  1.241  -.306
    "Japan"                      9 2020   2.92 -1.899
    "Burma/Myanmar"             10 2020  1.199   .294
    "Russia"                    11 2020  -.562  -.157
    "Albania"                   12 2020  1.359   .343
    "Egypt"                     13 2020 -1.355  -.233
    "Yemen"                     14 2020   -.87   .753
    "Colombia"                  15 2020  1.171   .559
    "Poland"                    17 2020   .775      0
    "Brazil"                    19 2020  1.351  -.334
    "United States of America"  20 2020  1.841   .016
    "Portugal"                  21 2020  2.821  -.498
    "El Salvador"               22 2020   .539 -1.337
    "Bangladesh"                24 2020  -.233   -1.3
    "Bolivia"                   25 2020   .375  1.197
    "Haiti"                     26 2020   .109  1.404
    "Honduras"                  27 2020  1.798   .028
    "Mali"                      28 2020   .943  1.579
    "Pakistan"                  29 2020  -.201  -.079
    "Peru"                      30 2020  1.649  -.763
    "Senegal"                   31 2020  1.696   .202
    "South Sudan"               32 2020  -.814   .817
    "Sudan"                     33 2020   .765  -.301
    "Vietnam"                   34 2020  -.737  -.605
    "Afghanistan"               36 2020  1.358   .975
    "Argentina"                 37 2020   1.89  -.465
    "Ethiopia"                  38 2020   .517  -.366
    "India"                     39 2020 -1.026   .048
    "Kenya"                     40 2020   .352 -1.189
    "North Korea"               41 2020 -3.705  -2.65
    "South Korea"               42 2020  2.537 -1.308
    "Kosovo"                    43 2020  1.509   -.33
    "Lebanon"                   44 2020   .737  1.381
    "Nigeria"                   45 2020   .556  1.969
    "Philippines"               46 2020   .227   .734
    "Tanzania"                  47 2020   .396  -.807
    "Taiwan"                    48 2020  2.251  -.362
    "Thailand"                  49 2020 -1.462   2.75
    "Uganda"                    50 2020 -1.079   -.36
    "Venezuela"                 51 2020 -1.045   .814
    "Benin"                     52 2020    .86  -.135
    "Bhutan"                    53 2020  1.187 -2.214
    "Burkina Faso"              54 2020  1.619   .653
    "Cambodia"                  55 2020  -1.14 -1.056
    "Indonesia"                 56 2020   .934   .059
    "Mozambique"                57 2020   .356  -.205
    "Nepal"                     58 2020  1.207  -.419
    "Nicaragua"                 59 2020 -1.571   .113
    "Niger"                     60 2020   .739   .712
    "Zambia"                    61 2020   .276  -.321
    "Zimbabwe"                  62 2020 -1.035   .958
    "Guinea"                    63 2020   .153  2.336
    "Ivory Coast"               64 2020   .544   .647
    "Mauritania"                65 2020   .202   .619
    "Canada"                    66 2020  2.048   -.56
    "Australia"                 67 2020  1.864 -2.251
    "Botswana"                  68 2020  2.159  -.888
    "Burundi"                   69 2020 -1.849  -.277
    "Cape Verde"                70 2020  2.886   -1.6
    "Central African Republic"  71 2020  -.505  1.088
    "Chile"                     72 2020  1.469  -.713
    "Costa Rica"                73 2020  2.468 -1.278
    "Timor-Leste"               74 2020  2.323   .095
    "Ecuador"                   75 2020   .348  -.593
    "France"                    76 2020  1.422  -.306
    "Germany"                   77 2020  3.013  -.911
    "Guatemala"                 78 2020   .477  -.682
    "Iran"                      79 2020 -1.574   .186
    "Iraq"                      80 2020  -.099  1.069
    "Ireland"                   81 2020  2.977 -1.236
    "Italy"                     82 2020  2.755 -1.263
    "Jordan"                    83 2020   .183 -1.207
    "Latvia"                    84 2020   2.64  -.722
    "Lesotho"                   85 2020  1.443   -1.2
    "Liberia"                   86 2020  1.911  -.459
    "Malawi"                    87 2020  1.363  -.523
    "Maldives"                  88 2020  1.405    -.4
    "Mongolia"                  89 2020  1.333 -2.206
    "Morocco"                   90 2020  -.528   .475
    "Netherlands"               91 2020   1.99  -.699
    "Panama"                    92 2020  1.716 -1.086
    "Papua New Guinea"          93 2020  1.971 -1.476
    "Qatar"                     94 2020  -.508 -3.244
    "Sierra Leone"              95 2020  2.166  -.955
    "Spain"                     96 2020  2.923   .014
    "Syria"                     97 2020 -1.961   .792
    "Tunisia"                   98 2020  1.955   .394
    "Turkey"                    99 2020 -1.521   .369
    "Ukraine"                  100 2020  1.744  -.093
    "United Kingdom"           101 2020  2.222  -.924
    "Uruguay"                  102 2020  1.261 -2.131
    "Algeria"                  103 2020 -1.149  1.321
    "Angola"                   104 2020   -.19  -.001
    "Armenia"                  105 2020  2.324  -.527
    "Azerbaijan"               106 2020 -1.271  -.534
    end

    Kind Regards
    Carlos

  • #2
    Just to clarify, i am using the variable "country_id" when i use the keep command. This is the command i am running without success: keep if country_id == 108 71 109 111 160 116 112 196 69 153 113 115 38 40 87 180 129 199 130 139 32 33 47 50 236 104 68 85 125 127 57 8 132 61 62 52 54 70 64 7 63 119 86 28 65 60 45 31 95 117 134
    Last edited by Carlos Ingeberg; 20 Dec 2022, 13:32.

    Comment


    • #3
      there are numerous syntax errors in your suggested code in #2, suggesting that you are still very much a beginner and need to study the manual (esp the getting started with manual but also the user's guide); here is some code:
      Code:
       keep if inlist(country_id,108, 71, 109, 111, 160, 116, 112, 196, 69, 153, 113, 115, 38, 40, 87, 180, 129, 199, 130, 139, 32, 33, 47, 50, 236, 104, 68, 85, 125, 127, 57, 8, 132, 61, 62, 52, 54, 70, 64, 7, 63, 119, 86, 28, 65, 60, 45, 31, 95, 117, 134)

      Comment

      Working...
      X