Announcement

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

  • Inlist Expression too long

    Hi guys,

    I am a Stata beginner and have a problem in generating a dummy called aggregate.
    I tried it with inlist(), but as my list is very long I get an error message "Expression too long".

    This is my code:

    #delimit ;
    gen aggregate = inlist(countryname,"Arab World", "Caribbean small states",
    "Central Europe and the Baltics",
    "East Asia & Pacific (all income levels)",
    "East Asia & Pacific (developing only)",
    "Euro area", "Europe & Central Asia (all income levels)",
    "Europe & Central Asia (developing only)",
    "Europe and Central Asia (IFC classification)",
    "European Union", "Fragile and conflict affected situations",
    "Heavily indebted poor countries (HIPC)",
    "High income", "High income: nonOECD", "High income: OECD",
    "Latin America & Caribbean (all income levels)",
    "Latin America & Caribbean (developing only)",
    "Latin America and the Caribbean (IFC classification)",
    "Least developed countries: UN classification",
    "Low & middle income", "Low income", "Lower middle income",
    "Middle East & North Africa (all income levels)",
    "Middle East & North Africa (developing only)",
    "Middle income", "North America", "OECD members",
    "Other small states", "Pacific island small states",
    "Small states", "South Asia",
    "Sub-Saharan Africa (all income levels)",
    "Sub-Saharan Africa (developing only)",
    "Upper middle income", "World"
    ),;
    #delimit cr

    Is there any smarter way to do that?
    Last edited by Yasin Kantar; 28 Sep 2019, 06:40.

  • #2
    break your "inlist" into several with "or" (|) between them; as the documentation says, when your items are strings, the total number of elements per inlist can be no more than 10

    Comment


    • #3
      Put the acceptable names in a different dataset as a string variable. Then merge with your main dataset. In Stata

      Code:
      search baum, author faq


      to find an FAQ explaining.

      Comment


      • #4
        Thank you very much!

        Comment


        • #5
          To do what Rich Goldstein suggests, I would first split the list of strings into lists of less than 10 elements:

          Code:
          // local for the first group
          local colist1 `" "Arab World", "Caribbean small states", "Central Europe and the Baltics", "East Asia & Pacific (all income levels)", "East Asia & Pacific (developing only)", "Euro area", "Europe & Central Asia (all income levels)", "Europe & Central Asia (developing only)", "Europe and Central Asia (IFC classification)", "European Union", "Fragile and conflict affected situations" "'
          // local for the second group
          local colist2 `" "Heavily indebted poor countries (HIPC)", "High income", "High income: nonOECD", "High income: OECD", "Latin America & Caribbean (all income levels)", "Latin America & Caribbean (developing only)", "Latin America and the Caribbean (IFC classification)", "Least developed countries: UN classification", "Low & middle income" "'
          // local for the third group
          local colist3 `" "Low income", "Lower middle income", "Middle East & North Africa (all income levels)", "Middle East & North Africa (developing only)", "Middle income", "North America", "OECD members", "Other small states", "Pacific island small states" "'
          local colist4 `" "Small states", "South Asia", "Sub-Saharan Africa (all income levels)", "Sub-Saharan Africa (developing only)", "Upper middle income", "World" "'
          // now pass each list to a separate inlist as Rich suggested.
          gen aggregate = inlist(countryname,`colist1') | inlist(countryname,`colist2') | inlist(countryname,`colist3') | inlist(countryname,`colist4')
          Note that the error Expression too long means that you've passed too many arguments to the function, not (as I first assumed) that any of the individual arguments is too long.
          Last edited by Arthur Morris; 29 Apr 2020, 21:19. Reason: added comments

          Comment


          • #6
            Arthur Morris thanks for you solution.
            Last edited by Bernardo Forbes Costa; 12 Nov 2021, 04:51.

            Comment


            • #7
              Matteo Pinna wrote a command -inlist2- which can create an inlist() dummy and without inlist() limitations. A link: https://ideas.repec.org/c/boc/bocode/s458920.html
              https://www.statalist.org/forums/for...st2-in-my-code
              Last edited by Chen Samulsion; 12 Nov 2021, 07:40. Reason: resource link is wrong

              Comment

              Working...
              X