Hej Statalist,

I have a problem, as I want to aggregate my panel data under certrain conditions. I have trade data that conisits of the variables year, Exporter(partner), Importer(reporter), commoditycodes (#97 different one) and trade Values. Now i want to sum up the Trade Valus for Exporter, Importer and Year if for different groups of the commiditycode. For example i want a groups of commoditycodes: 1-24, 25-40, 40-99).
My data set looks like this:
Code:
input int year str32 reporter str6 partner long tradevalueus byte commoditycode
1992 "So. African Customs Union"        "Benin"    159277 52
1992 "Netherlands"                      "Benin"      2868 69
1992 "Germany"                          "Benin"      1000 33
1992 "Indonesia"                        "Benin"   2388430 52
1992 "Thailand"                         "Benin"   4483692 52
1992 "Netherlands"                      "Benin"     49024 52
1992 "Spain"                            "Benin"      8473 41
1992 "Germany"                          "Benin"      2000  1
1992 "Mexico"                           "Benin"      1000 69
1992 "Malaysia"                         "Benin"     83042 52
1992 "Ecuador"                          "Benin"     23824 12
1992 "Trinidad and Tobago"              "Benin"     10322 76
1992 "Switzerland"                      "Benin"       582 32
1992 "Germany"                          "Benin"      1000 34
1992 "Netherlands"                      "Benin"     36683  9
1992 "Spain"                            "Benin"    186340 23
1992 "Trinidad and Tobago"              "Benin"     17290 85
1992 "Germany"                          "Benin"    649000 87
1992 "Netherlands"                      "Benin"   2045656 15
1992 "Germany"                          "Benin"      6000 97
1992 "Colombia"                         "Benin"     29585 33
1992 "USA"                              "Benin"   9610061 27
1992 "Germany"                          "Benin"    591000 52
1992 "Denmark"                          "Benin"      2865 36
1992 "Colombia"                         "Benin"     14957 12
1992 "Germany"                          "Benin"    121000 15
1992 "Spain"                            "Benin"     16689 94
1992 "Germany"                          "Benin"     65000  9
1992 "USA"                              "Benin"      2163 62
1992 "USA"                              "Benin"      2928 44
1992 "Germany"                          "Benin"     58000 44
1992 "Japan"                            "Benin"   2015714 15
1992 "Spain"                            "Benin"    189207 52
1992 "Netherlands"                      "Benin"       752 14
1992 "USA"                              "Benin"      3100 61
1992 "Denmark"                          "Benin"    152183 23
1992 "Germany"                          "Benin"     26000 71
1992 "Bolivia (Plurinational State of)" "Benin"       602 82
1992 "USA"                              "Benin"    197539 99
1992 "Finland"                          "Benin"     10292 44
1992 "Portugal"                         "Benin"     13208 87
1992 "Germany"                          "Benin"    102000 85
1992 "Saudi Arabia"                     "Benin"   1339431 12
1992 "Portugal"                         "Benin"  18827816 52
1992 "Bolivia (Plurinational State of)" "Benin"     12169 87
1992 "Rep. of Korea"                    "Benin"   2803639 52
1992 "Bolivia (Plurinational State of)" "Benin"       555 63
1992 "India"                            "Benin"   3829808  8
1992 "Spain"                            "Benin"      8912 44
1992 "Germany"                          "Benin"     10000 84
1992 "Switzerland"                      "Benin"       569 63
1992 "Myanmar"                          "Benin"       755 85
1992 "USA"                              "Benin"    210000 23
1992 "USA"                              "Benin"     35024 97
1992 "Germany"                          "Benin"      1000 95
1992 "Japan"                            "Benin"    252558 12
1992 "Germany"                          "Benin"     15000 74
1992 "Germany"                          "Benin"      8000  3
1992 "USA"                              "Benin"    197210  1
1992 "Sweden"                           "Benin"      1031 84
1992 "China"                            "Benin"    545868 52
1992 "Netherlands"                      "Benin"       528 44
1992 "Spain"                            "Benin"   3785615 12
1992 "Spain"                            "Benin"     12929  3
1992 "Germany"                          "Benin"      1000 83
1992 "Germany"                          "Benin"      1000 61
1992 "Bolivia (Plurinational State of)" "Benin"       926 49
1992 "Spain"                            "Benin"      1875 48
1992 "USA"                              "Guinea"  6194042 71
1992 "Germany"                          "Guinea"    97000 71
1992 "Netherlands"                      "Guinea"    16361  6
1992 "Japan"                            "Guinea"   142066 52
1992 "Brunei Darussalam"                "Guinea"      504  7
1992 "Rep. of Korea"                    "Guinea"     7829 39
1992 "Germany"                          "Guinea"   142000  8
1992 "Rep. of Korea"                    "Guinea"   628147 44
1992 "Portugal"                         "Guinea"    35244  8
1992 "Germany"                          "Guinea"     6000 92
1992 "Turkey"                           "Guinea"     5373 64
1992 "Netherlands"                      "Guinea"     2789 90
1992 "So. African Customs Union"        "Guinea"      896 73
1992 "Madagascar"                       "Guinea"     1812 90
1992 "Canada"                           "Guinea"     8297 18
1992 "So. African Customs Union"        "Guinea"     3502 99
1992 "Peru"                             "Guinea"    42371 44
1992 "Portugal"                         "Guinea"    58693  9
1992 "Germany"                          "Guinea"  1533000 18
1992 "Peru"                             "Guinea"      819 84
1992 "Netherlands"                      "Guinea"   157806  1
1992 "USA"                              "Guinea"    70075 44
1992 "Netherlands"                      "Guinea"      540 84
1992 "Japan"                            "Guinea"    63299 99
1992 "China"                            "Guinea"   119286 44
1992 "Indonesia"                        "Guinea"   186683 84
1992 "Brunei Darussalam"                "Guinea"     2147 84
1992 "Finland"                          "Guinea"    15758  9
1992 "Paraguay"                         "Guinea"     1863 63
1992 "Portugal"                         "Guinea"    66395  3
1992 "Netherlands"                      "Guinea"     3818  7
1992 "USA"                              "Guinea"   378997 99
I tried to use the collapse command, but I can't make it work for the different groups of commoditycodes.

Code:
collapse (sum) commoditycode  if commoditycode <= 24, by(year partner reporter tradevalueus)
This code is just deleting all the observations if the commoditycode is bigger than 24.

Anyone has an idea how to solve it? Thank you very much in advance!!

Martin