Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6058723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:37:50+00:00 2026-05-23T08:37:50+00:00

I have a manual list I created in a macro in stata, something like

  • 0

I have a manual list I created in a macro in stata, something like

global list1 "a b c d"

which I later iterate through with something like

foreach name in $list1 {
action
}

I am trying to change this to a DB driven list because the list is getting big and changing quickly, I create a new $list1 with the following commands

odbc load listitems=items, exec("SELECT items  from my_table")  
levelsof listitems
global list1=r(levels)

The items on each are the same, but this list seems to be different and when I have too many items it break on the for loop with the error

{ required
r(100);

Also, when I run only levelsof listitems I get the output

`"a"' `"b"' `"c"' `"d"' 

Which looks a little bit different than the other macros.

I’ve been stuck in this for a while. Again, it only fails when the number of items becomes large (over 15), any help would be very appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:37:50+00:00Added an answer on May 23, 2026 at 8:37 am

    Solution 1:

    levelsof listitems, clean local(list1)
    foreach name of local list1 {
        ...action with `name'...
    }
    

    Solution 2:

    levelsof listitems, clean
    global list1 `r(levels)'
    foreach name of global list1 {
        ...action with `name'...
    }
    

    Explanation:

    When you type

    foreach name in $list1 {
    

    then whatever is in $list1 gets substituted inline before Stata ever sees it. If global macro list1 contains a very long list of things, then Stata will see

    foreach name in a b c d e .... very long list of things here ... {
    

    It is more efficient to tell Stata that you have a list of things in a global or local macro, and that you want to loop over those things. You don’t have to expand them out on the command line. That is what

    foreach name of local list1 {
    

    and

    foreach name of global list1 {
    

    are for. You can read about other capabilities of foreach in -help foreach-.

    Also, you originally coded

    levelsof listitems
    global list1=r(levels)
    

    and you noted that you saw

    `"a"' `"b"' `"c"' ...
    

    as a result. Those are what Stata calls “compound quoted” strings. A compound quoted string lets you effectively nest quoted things. So, you can have something like

    `"This is a string with `"another quoted string"' inside it"'
    

    You said you don’t need that, so you can use the “clean” option of levelsof to not quote up the results. (See -help levelsof- for more info on this option.) Also, you were assigning the returned result of levelsof (which is in r(levels)) to a global macro afterward. It turns out -levelsof- actually has an option named -local()- where you can specify the name of a local (not global) macro to directly put the results in. Thus, you can just type

    levelsof listitems, clean local(list1)
    

    to both omit the compound quotes and to directly put the results in a local macro named list1.

    Finally, if you for some reason don’t want to use that local() option and want to stick with putting your list in a global macro, you should code

    global list1 `r(levels)'
    

    rather than

    global list1=r(levels)
    

    The distinction is that the latter treats r(levels) as a function and runs it through Stata’s string expression parser. In Stata, strings (strings, not macros containing strings) have a limit of 244 characters. Macros containing strings on the other hand can have thousands of characters in them. So, if r(levels) had more than 244 characters in it, then

    global list1=r(levels)
    

    would end up truncating the result stored in list1 at 244 characters.

    When you instead code

    global list1 `r(levels)'
    

    then the contents of r(levels) are expanded in-line before the command is executed. So, Stata sees

    global list1 a b c d e ... very long list ... x y z
    

    and everything after the macro name (list1) is copied into that macro name, no matter how long it is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have gone through this page... http://www.php.net/manual/en/function.hash.php MD5 is 32 characters long while sha1
MVC 3 razor VB.NET project. I have resorted to manual building a list for
I have a generic list of custom objects and would like to reduce that
I need to compute ICMPv6 checksum. I have found correct manual in RFC 2460
I have read the PHP manual , but couldn't find any proper explanation regarding
Manual Unref I have an issue with Boost's intrusive pointer. It's boolean conversion operator
I have following Enum in my project public enum CameraAccessMethod { Manual = 0,
I have the following piece of code taken from the PHP manual on the
I have a confusion between two B keywords. The official B reference manual uses
You have an error in your SQL syntax; check the manual that corresponds to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.