Announcement

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

  • Creating new dta using foreach loop

    Hi everyone!

    I'm just starting to learn Stata and I have a problem with an activity, I have a database of some imports of white meat and there is a variable called ORIGIN that contains the kind of meat (chicken, pig, turkey), and then there is another variable called PRODUCT, where we can find the products obtained from the kind of meat (leg 1/4, ribs, chicken wings, etc). Also, there are another variables like CIF, TOTALIMPORT and FOBUNIT, but the one I need for the exercise is the ORIGIN var.

    What I have to do is create new databases, one for each kind of white meat, using the foreach loop (more like separate the database into three), and the name of each dta has to be the kind of white meat.

    I've been trying for hours but I'm out of ideas, so I think I need some help.

    Thanks!

    PS: Sorry if my english isn't good, I'm also learning haha.

  • #2
    Muéstrame tus datos usando dataex. Cuando me despertaré, me voy a mirar.

    Looks like you're working with COMTRADE data.

    Comment


    • #3
      Originally posted by Jared Greathouse View Post
      Muéstrame tus datos usando dataex. Cuando me despertaré, me voy a mirar.

      Looks like you're working with COMTRADE data.
      Thanks!

      My data looks like this,

      Code:
      * Example generated by -dataex-.
      clear
      input str21 PRODUCTO str29 ORIGEN double(CIF CANTIDAD) float CIFUNIT double FOBUNIT
      "Jamón"      "Elaborado Cerdo"  15.35     1     15.35               13.3
      "Jamón"      "Elaborado Cerdo"  17.41     1     17.41              15.33
      "Pechuga c/h" "Pollo"             22.1    15 1.4733334 1.0713333333333332
      "Jamón"      "Elaborado Cerdo"  19.95   1.2    16.625              14.55
      "Leg 1/4"     "Pollo"            20.97 18.16 1.1547357  .9983480176211453
      "Jamón"      "Elaborado Cerdo"   33.4   2.5     13.36 11.331999999999999
      "Apanado"     "Elaborado Pollo"   31.3  4.53  6.909492    6.2560706401766
      "Ala"         "Pollo"            36.12    36 1.0033333  .9288888888888889
      "Filete"      "Pollo"            36.12    36 1.0033333  .9288888888888889
      "Jamón"      "Elaborado Cerdo"  58.51  5.02 11.655378  9.647410358565738
      "Pate"        "Elaborado Cerdo"  76.27    12  6.355834               4.21
      "Jamón"      "Elaborado Cerdo"  60.81     4   15.2025              13.15
      "Pate"        "Elaborado Cerdo"  79.66    12  6.638333  4.486666666666667
      "Leg 1/4"     "Pollo"            63.43 72.57  .8740526  .7600937026319416
      "Jamón"      "Elaborado Cerdo"   65.8  6.12 10.751634  9.016339869281046
      "Chorizo"     "Elaborado Cerdo"  97.24    12  8.103333  5.927499999999999
      "Salchichas"  "Elaborado Cerdo"  82.19  7.56 10.871693  9.473544973544975
      "Chorizo"     "Elaborado Cerdo"  82.19  7.56 10.871693  9.473544973544975
      "Jamón"      "Elaborado Pollo" 101.24  19.2  5.272917             3.9375
      "Pate"        "Elaborado Cerdo" 107.62  16.8  6.405952  5.083928571428571
      end
      And my code for selecting one ORIGEN is,

      Code:
      preserve
      keep if ORIGEN == "Pollo"
      save "Pollo", replace
      restore
      So I thought it would be as easy as putting that code into a foreach loop like this,

      Code:
      foreach x in ORIGEN {
      preserve 
      keep if ORIGEN == `x'
      save "`x'", replace 
      restore
      }
      But it didn't work, so there are obviously some rules that I don't know, and I think I need to add other conditions for it to work, but I couldn't find any information about this in particular.

      Comment


      • #4
        So before I do anything, I wanna know what Stata did. What did it do that you didn't want it to do? What did you want for it to do, that it didn't do?

        Comment

        Working...
        X