Announcement

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

  • Counting specific observations satisfying conditions

    Hi,
    I have a dataset with 4122 observations, and I have to count the number of commodities and markets in every year (2014, 2015 and 2016).
    To explain better the issue, a question is "How many observations of "Cowpeas" are in 2015? And in which market?"

    Is it possible to do it?
    Thanks.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str39 market str46 commodity int year float price
    "Alto Molócuè" "Sugar (brown local) "             2016      50
    "Alto Molócuè" "Cowpeas"                          2014      15
    "Alto Molócuè" "Cowpeas"                          2015      15
    "Alto Molócuè" "Rice (local)"                     2014       .
    "Alto Molócuè" "Rice (imported)"                  2014      25
    "Alto Molócuè" "Beans (magnum)"                   2016      70
    "Alto Molócuè" "Maize meal (white without bran) " 2015       .
    "Alto Molócuè" "Cassava flour"                    2015      10
    "Alto Molócuè" "Groundnuts (small shelled) "      2014 26.6667
    "Alto Molócuè" "Oil (vegetable imported) "        2016       .
    "Alto Molócuè" "Groundnuts (small shelled) "      2014      25
    "Alto Molócuè" "Groundnuts (large shelled) "      2016       .
    "Alto Molócuè" "Beans (red)"                      2015    37.5
    "Alto Molócuè" "Maize (white)"                    2015       9
    "Alto Molócuè" "Rice (imported)"                  2015    27.5
    "Alto Molócuè" "Rice (imported)"                  2015   25.25
    "Alto Molócuè" "Maize meal (white first grade) "  2016 66.6667
    "Alto Molócuè" "Maize meal (white without bran) " 2014    22.5
    "Alto Molócuè" "Wheat flour (local)"              2016 40.6667
    "Alto Molócuè" "Maize meal (white with bran) "    2016 21.6667
    "Alto Molócuè" "Beans (red)"                      2015       .
    "Alto Molócuè" "Sugar (brown local) "             2015       .
    "Alto Molócuè" "Maize meal (white with bran) "    2015       .
    "Alto Molócuè" "Sugar (brown local) "             2014      39
    "Alto Molócuè" "Beans (catarino)"                 2016       .
    "Alto Molócuè" "Sugar (brown local) "             2016 63.3333
    "Alto Molócuè" "Groundnuts (large shelled) "      2015       .
    "Alto Molócuè" "Cassava flour"                    2016      12
    "Alto Molócuè" "Beans (magnum)"                   2016       .
    "Alto Molócuè" "Sugar (brown imported) "          2016       .
    "Alto Molócuè" "Cowpeas"                          2016       .
    "Alto Molócuè" "Sugar (brown local) "             2015       .
    "Alto Molócuè" "Rice (local)"                     2016      35
    "Alto Molócuè" "Beans (magnum)"                   2016       .
    "Alto Molócuè" "Maize meal (white with bran) "    2014       .
    "Alto Molócuè" "Cassava flour"                    2015  9.3333
    "Alto Molócuè" "Rice (imported)"                  2016 51.3333
    "Alto Molócuè" "Cowpeas"                          2016      30
    "Alto Molócuè" "Oil (vegetable imported) "        2015       .
    "Alto Molócuè" "Oil (vegetable imported) "        2016       .
    "Alto Molócuè" "Sugar (brown imported) "          2016       .
    "Alto Molócuè" "Groundnuts (small shelled) "      2016     125
    "Alto Molócuè" "Maize meal (white without bran) " 2016      50
    "Alto Molócuè" "Maize meal (white without bran) " 2016      35
    "Alto Molócuè" "Beans (dry)"                      2014    32.5
    "Alto Molócuè" "Rice (imported)"                  2014      25
    "Alto Molócuè" "Cowpeas"                          2014   16.25
    "Alto Molócuè" "Maize (white)"                    2015  8.3429
    "Alto Molócuè" "Cassava flour"                    2014       .
    "Alto Molócuè" "Cowpeas"                          2015       .
    "Alto Molócuè" "Maize meal (white first grade) "  2015       .
    "Alto Molócuè" "Groundnuts (large shelled) "      2015    27.5
    "Alto Molócuè" "Oil (vegetable local) "           2016 81.6667
    "Alto Molócuè" "Beans (catarino)"                 2016      60
    "Alto Molócuè" "Oil (vegetable imported) "        2016       .
    "Alto Molócuè" "Maize meal (white with bran) "    2015      12
    "Alto Molócuè" "Maize meal (white first grade) "  2015      40
    "Alto Molócuè" "Wheat flour (local)"              2014      22
    "Alto Molócuè" "Cassava flour"                    2015      10
    "Alto Molócuè" "Sugar (brown local) "             2015       .
    "Alto Molócuè" "Sugar (brown local) "             2015       .
    "Alto Molócuè" "Groundnuts (small shelled) "      2014      50
    "Alto Molócuè" "Groundnuts (large shelled) "      2015      20
    "Alto Molócuè" "Oil (vegetable local) "           2015    67.5
    "Alto Molócuè" "Groundnuts (large shelled) "      2014    42.5
    "Alto Molócuè" "Sugar (brown imported) "          2015      39
    "Alto Molócuè" "Maize meal (white with bran) "    2014   12.75
    "Alto Molócuè" "Cowpeas"                          2014 23.3333
    "Alto Molócuè" "Wheat flour (local)"              2014 21.3333
    "Alto Molócuè" "Rice (imported)"                  2015      25
    "Alto Molócuè" "Maize meal (white first grade) "  2015      40
    "Alto Molócuè" "Maize meal (white without bran) " 2014 15.6667
    "Alto Molócuè" "Rice (local)"                     2015      31
    "Alto Molócuè" "Maize (white)"                    2015 14.8571
    "Alto Molócuè" "Sugar (brown local) "             2014      38
    "Alto Molócuè" "Groundnuts (large shelled) "      2015    37.5
    "Alto Molócuè" "Maize meal (white first grade) "  2014   36.67
    "Alto Molócuè" "Rice (local)"                     2015    29.4
    "Alto Molócuè" "Maize meal (white first grade) "  2016      35
    "Alto Molócuè" "Sugar (brown imported) "          2016       .
    "Alto Molócuè" "Maize meal (white without bran) " 2014      20
    "Alto Molócuè" "Oil (vegetable local) "           2016     140
    "Alto Molócuè" "Maize meal (white with bran) "    2016    24.5
    "Alto Molócuè" "Sugar (brown local) "             2014   35.67
    "Alto Molócuè" "Beans (catarino)"                 2016      80
    "Alto Molócuè" "Rice (local)"                     2016      30
    "Alto Molócuè" "Maize meal (white first grade) "  2014 36.1111
    "Alto Molócuè" "Groundnuts (small shelled) "      2016       .
    "Alto Molócuè" "Maize meal (white without bran) " 2016       .
    "Alto Molócuè" "Cassava flour"                    2015      11
    "Alto Molócuè" "Cowpeas"                          2014      15
    "Alto Molócuè" "Oil (vegetable imported) "        2014       .
    "Alto Molócuè" "Groundnuts (large shelled) "      2016     130
    "Alto Molócuè" "Maize (white)"                    2015  9.5714
    "Alto Molócuè" "Rice (local)"                     2015    33.5
    "Alto Molócuè" "Maize meal (white first grade) "  2016      80
    "Alto Molócuè" "Cassava flour"                    2014       .
    "Alto Molócuè" "Groundnuts (large shelled) "      2015      31
    "Alto Molócuè" "Oil (vegetable local) "           2016      80
    "Alto Molócuè" "Beans (butter)"                   2016      70
    end

  • #2
    Fabb:
    welcome to this forum.
    Something like:
    Code:
    bysort year market: egen wanted=total(commodity) if commodity=="oneofyourcommodities"
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Unfortunately. @Carlo Lazzaro's suggestion in #2 won't work as total() can't digest a string variable.

      You can do something like

      Code:
      table (commodity) (market) (year)
      or with groups from the Stata Journal

      Code:
      . groups market commodity year 
      
        +--------------------------------------------------------------------------+
        |       market                          commodity   year   Freq.   Percent |
        |--------------------------------------------------------------------------|
        | Alto Molócuè                     Beans (butter)   2016       1      1.00 |
        | Alto Molócuè                   Beans (catarino)   2016       3      3.00 |
        | Alto Molócuè                        Beans (dry)   2014       1      1.00 |
        | Alto Molócuè                     Beans (magnum)   2016       3      3.00 |
        | Alto Molócuè                        Beans (red)   2015       2      2.00 |
        |--------------------------------------------------------------------------|
        | Alto Molócuè                      Cassava flour   2014       2      2.00 |
        | Alto Molócuè                      Cassava flour   2015       4      4.00 |
        | Alto Molócuè                      Cassava flour   2016       1      1.00 |
        | Alto Molócuè                            Cowpeas   2014       4      4.00 |
        | Alto Molócuè                            Cowpeas   2015       2      2.00 |
        |--------------------------------------------------------------------------|
        | Alto Molócuè                            Cowpeas   2016       2      2.00 |
        | Alto Molócuè        Groundnuts (large shelled)    2014       1      1.00 |
        | Alto Molócuè        Groundnuts (large shelled)    2015       5      5.00 |
        | Alto Molócuè        Groundnuts (large shelled)    2016       2      2.00 |
        | Alto Molócuè        Groundnuts (small shelled)    2014       3      3.00 |
        |--------------------------------------------------------------------------|
        | Alto Molócuè        Groundnuts (small shelled)    2016       2      2.00 |
        | Alto Molócuè                      Maize (white)   2015       4      4.00 |
        | Alto Molócuè    Maize meal (white first grade)    2014       2      2.00 |
        | Alto Molócuè    Maize meal (white first grade)    2015       3      3.00 |
        | Alto Molócuè    Maize meal (white first grade)    2016       3      3.00 |
        |--------------------------------------------------------------------------|
        | Alto Molócuè      Maize meal (white with bran)    2014       2      2.00 |
        | Alto Molócuè      Maize meal (white with bran)    2015       2      2.00 |
        | Alto Molócuè      Maize meal (white with bran)    2016       2      2.00 |
        | Alto Molócuè   Maize meal (white without bran)    2014       3      3.00 |
        | Alto Molócuè   Maize meal (white without bran)    2015       1      1.00 |
        |--------------------------------------------------------------------------|
        | Alto Molócuè   Maize meal (white without bran)    2016       3      3.00 |
        | Alto Molócuè          Oil (vegetable imported)    2014       1      1.00 |
        | Alto Molócuè          Oil (vegetable imported)    2015       1      1.00 |
        | Alto Molócuè          Oil (vegetable imported)    2016       3      3.00 |
        | Alto Molócuè             Oil (vegetable local)    2015       1      1.00 |
        |--------------------------------------------------------------------------|
        | Alto Molócuè             Oil (vegetable local)    2016       3      3.00 |
        | Alto Molócuè                    Rice (imported)   2014       2      2.00 |
        | Alto Molócuè                    Rice (imported)   2015       3      3.00 |
        | Alto Molócuè                    Rice (imported)   2016       1      1.00 |
        | Alto Molócuè                       Rice (local)   2014       1      1.00 |
        |--------------------------------------------------------------------------|
        | Alto Molócuè                       Rice (local)   2015       3      3.00 |
        | Alto Molócuè                       Rice (local)   2016       2      2.00 |
        | Alto Molócuè            Sugar (brown imported)    2015       1      1.00 |
        | Alto Molócuè            Sugar (brown imported)    2016       3      3.00 |
        | Alto Molócuè               Sugar (brown local)    2014       3      3.00 |
        |--------------------------------------------------------------------------|
        | Alto Molócuè               Sugar (brown local)    2015       4      4.00 |
        | Alto Molócuè               Sugar (brown local)    2016       2      2.00 |
        | Alto Molócuè                Wheat flour (local)   2014       2      2.00 |
        | Alto Molócuè                Wheat flour (local)   2016       1      1.00 |
        +--------------------------------------------------------------------------+
      The frequencies themselves can come from

      Code:
      bysort commodity market year : gen count = _N 

      Comment


      • #4
        Thank to both of you for the answers. In fact, @Carlo Lazzaro's code gives me a "type mismatch" error.
        The "table" code seems perfect for my analysis.

        Comment

        Working...
        X