Announcement

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

  • Using foreach for two set of groups

    Dear All,

    I have two groups of countries and pairwise data- importers and exporters. Eg. Importers= ITALY, FRANCE, GERMANY etc. and exporters=ITALY, FRANCE, GERMANY, CHINA, UK, USA etc. Let EMU countries be ITALY, FRANCE, GERMANY

    I want to use the "foreach" command or any command to create a new variable say EMU which takes 1 for any pair of EMU countries and 0 otherwise.

    Thank you.

  • #2
    I can't see that this is a loop problem at all. How about (some variation on) this?

    Code:
    gen EMU = inlist(importer, "ITALY", "FRANCE", "GERMANY") & inlist(exporter, "ITALY", "FRANCE", "GERMANY")

    Comment


    • #3
      Thank you, Prof. Cox. Interestingly, an error reads "too long expression". I have total 15 countries on both sides. I don't seem to understand this error.

      gen CET = inlist(Imp_ISO, "BEN", "GHA", "NGA","BFA", "SEN", "CIV","CPV", "GMB", "GIN","GNB", "MLI", "NER", "TGO", "LBR", "SLE") & /*
      */ inlist(Exp_ISO, "BEN", "GHA", "NGA","BFA", "SEN", "CIV","CPV", "GMB", "GIN","GNB", "MLI", "NER", "TGO", "LBR", "SLE")

      Comment


      • #4
        So, you changed the question to 15 African countries. OK, but if you change the question, the answer might change too. Now you need to read the help

        Code:
        help inlist()
        inlist(z,a,b,...)
        Description: 1 if z is a member of the remaining arguments; otherwise, 0

        All arguments must be reals or all must be strings. The number of arguments is between 2
        and 250 for reals and between 2 and 10 for strings.
        Domain: all reals or all strings
        Range: 0 or 1
        and indeed your expression is too long. Try something like

        Code:
        gen CET = (inlist(Imp_ISO, "BEN", "GHA", "NGA","BFA", "SEN", "CIV","CPV", "GMB") | inlist(Imp_ISO, "GIN","GNB", "MLI", "NER", "TGO", "LBR", "SLE")) 
        & (inlist(Exp_ISO, "BEN", "GHA", "NGA","BFA", "SEN", "CIV","CPV", "GMB") | inlist(Exp_ISO, "GIN","GNB", "MLI", "NER", "TGO", "LBR", "SLE"))

        Comment


        • #5
          Perfect! Works correctly. Thanks, Prof. Cox

          Comment

          Working...
          X