Announcement

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

  • Counting how many times a variable takes on a set of values

    Hello,

    tab c1_export_destination_vtcs

    Code:
     tab c1_export_destination_vtcs
    
        Country |
       code for |
        largest |
         export |
    destination |      Freq.     Percent        Cum.
    ------------+-----------------------------------
           1101 |         10        0.05        0.05
           1102 |        829        3.88        3.92
           1103 |        216        1.01        4.93
           1104 |        207        0.97        5.90
           1105 |        447        2.09        7.99
           1106 |        100        0.47        8.46
           1107 |        241        1.13        9.58
           1108 |        423        1.98       11.56
           1109 |        603        2.82       14.38
           1110 |         65        0.30       14.69
           1201 |          4        0.02       14.70
           1202 |         16        0.07       14.78
           1203 |         14        0.07       14.84
           1204 |         13        0.06       14.91
           1206 |          7        0.03       14.94
           1207 |          3        0.01       14.95
           1209 |          2        0.01       14.96
           1210 |          5        0.02       14.99
           1211 |         26        0.12       15.11
           1212 |          1        0.00       15.11
           1213 |         33        0.15       15.27
           1214 |          2        0.01       15.27
           1301 |         35        0.16       15.44
           1302 |         57        0.27       15.71
           1303 |          1        0.00       15.71
           1304 |      1,749        8.18       23.89
           1305 |        497        2.32       26.21
           1306 |        173        0.81       27.02
           1307 |      3,297       15.42       42.44
           1308 |          3        0.01       42.45
           1309 |          3        0.01       42.46
           1310 |          9        0.04       42.51
           1311 |      2,067        9.66       52.17
           1313 |          2        0.01       52.18
           1315 |          2        0.01       52.19
           1316 |          6        0.03       52.22
           1317 |         71        0.33       52.55
           1318 |         10        0.05       52.59
           1320 |      2,861       13.38       65.97
           1321 |          3        0.01       65.99
           1322 |          1        0.00       65.99
           1399 |         14        0.07       66.06
           2101 |          1        0.00       66.06
           2102 |          1        0.00       66.07
           2103 |          1        0.00       66.07
           2104 |          6        0.03       66.10
           2105 |          1        0.00       66.10
           2106 |         54        0.25       66.35
           2108 |          3        0.01       66.37
           2109 |         19        0.09       66.46
           2110 |          1        0.00       66.46
           2113 |         42        0.20       66.66
           2114 |          6        0.03       66.69
           2115 |        194        0.91       67.59
           2116 |         25        0.12       67.71
           2117 |          4        0.02       67.73
           2118 |         21        0.10       67.83
           2119 |          1        0.00       67.83
           2201 |         17        0.08       67.91
           2202 |        183        0.86       68.77
           2203 |        374        1.75       70.52
           2204 |          1        0.00       70.52
           2205 |        146        0.68       71.20
           2206 |         30        0.14       71.34
           2207 |        415        1.94       73.28
           2208 |        676        3.16       76.44
           2209 |          1        0.00       76.45
           2210 |         22        0.10       76.55
           2211 |        235        1.10       77.65
           2212 |          1        0.00       77.66
           2213 |          3        0.01       77.67
           2215 |          3        0.01       77.68
           2216 |        215        1.01       78.69
           2217 |         15        0.07       78.76
           2218 |         19        0.09       78.85
           2220 |        143        0.67       79.52
           2221 |         77        0.36       79.88
           2222 |         62        0.29       80.17
           2301 |          7        0.03       80.20
           2304 |          3        0.01       80.21
           2305 |          7        0.03       80.25
           2307 |         67        0.31       80.56
           2399 |         44        0.21       80.76
           3001 |          1        0.00       80.77
           3002 |          6        0.03       80.80
           3006 |          3        0.01       80.81
           3007 |          3        0.01       80.83
           3009 |          1        0.00       80.83
           3010 |          2        0.01       80.84
           3012 |          3        0.01       80.85
           3014 |          2        0.01       80.86
           3015 |         26        0.12       80.98
           3018 |          3        0.01       81.00
           3019 |          9        0.04       81.04
           3020 |          1        0.00       81.05
           3021 |          1        0.00       81.05
           3022 |          1        0.00       81.05
           3023 |          1        0.00       81.06
           3025 |          1        0.00       81.06
           3028 |          5        0.02       81.09
           3031 |          2        0.01       81.10
           3032 |          1        0.00       81.10
           3034 |          1        0.00       81.11
           3036 |          2        0.01       81.12
           3037 |         24        0.11       81.23
           3042 |          1        0.00       81.23
           3043 |          3        0.01       81.25
           3047 |         21        0.10       81.34
           3050 |          1        0.00       81.35
           3053 |          3        0.01       81.36
           3058 |          4        0.02       81.38
           3059 |          1        0.00       81.39
           3060 |          1        0.00       81.39
           3099 |          5        0.02       81.41
           4101 |      3,266       15.27       96.69
           4103 |        129        0.60       97.29
           4104 |          6        0.03       97.32
           4105 |         14        0.07       97.38
           4106 |          4        0.02       97.40
           4201 |          3        0.01       97.41
           4202 |          1        0.00       97.42
           4203 |          1        0.00       97.42
           4204 |          6        0.03       97.45
           4207 |          5        0.02       97.48
           4209 |         47        0.22       97.69
           4210 |          2        0.01       97.70
           4212 |          4        0.02       97.72
           4213 |         19        0.09       97.81
           4214 |          1        0.00       97.82
           4215 |          1        0.00       97.82
           4216 |         14        0.07       97.89
           4219 |          3        0.01       97.90
           4222 |          2        0.01       97.91
           4225 |          1        0.00       97.91
           4231 |          4        0.02       97.93
           4233 |          5        0.02       97.96
           4234 |          2        0.01       97.97
           4238 |          2        0.01       97.98
           4239 |          2        0.01       97.98
           4240 |          1        0.00       97.99
           4243 |          2        0.01       98.00
           4244 |          2        0.01       98.01
           4245 |          3        0.01       98.02
           4299 |          6        0.03       98.05
           5001 |         11        0.05       98.10
           5002 |        367        1.72       99.82
           5015 |         31        0.14       99.96
           5020 |          1        0.00       99.97
           5099 |          7        0.03      100.00
    ------------+-----------------------------------
          Total |     21,388      100.00
    
    . count if c1_export_destination_vtcs== 5099 | 5020 | 5015
      4,515,749
    
    . count if c1_export_destination_vtcs== 5099 | c1_export_destination_vtcs== 5020 | c1_export_destination_vtcs=
    > = 5015
      39
    I want to perform the above count, but without having to restate which variable I'm looking at, so something like "count if c1_export_destination_vtcs== 5099 | 5020 | 5015", as opposed to the subsequent line, which is giving the correct result based on the tabulation of this variable. How can I implement something like this?

    Thanks,
    Jad

  • #2
    Code:
    count if inlist(c1_export_destination_vtcs, 5099, 5020, 5015)
    The syntax -count if c1_export_destination_vtcs== 5099 | 5020 | 5015-, as you have observed, does not do what you want. Stata syntax, like most computer programming languages, is highly structured and not very flexible. In particular, it does not allow the kind of ellipsis that is common in natural languages to indicate repetition of something mentioned earlier. But what you wrote is not a syntax error either: it is valid Stata syntax, but it has a different meaning. Stata evaluates logical expressions (things that get combined with & or | or !) numerically: 0 is false, and non-zero is true. Moreover, the order of operator precedence is such that the command you tried is interpreted as -count if (c1_export_destination_vtcs== 5099) | (5020) | (5015). Now, since anything non-zero is true, 5015 and 5020 are both treated as true. And, of course, true | anything becomes true. So regardless of the value of c1_export_destination_vtcs, the whole expression evaluates to true. So Stata gives you a count of all the observations in the data set.

    Comment


    • #3
      See https://journals.sagepub.com/doi/pdf...6867X231162009 for a more extended discussion of Clyde Schechter's point.

      Comment

      Working...
      X