Announcement

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

  • Foreach loop breaks with local that has a dash

    Hi,

    I am having issues with a foreach loop, and I suspect it's either a dash character or an inadequate use of double quotes! Unfortunately, I cannot rename my local to avoid the dash, as this is part of a larger system of files and conventions. And I'm a bit confused with the quotes.

    Here is a simplified version of my code, with what I've tried so far. The 1st loop displays the output I want, but doesn't work when I add BBB-b. The 2nd and 3rd, don't display what I intended:

    Code:
    * Parameters for the loop
    local AAA_years   = "2001 2002"
    local BBB_years   = "2001 2002"
    local BBB-b_years = "2001 2002"
    
    * This works as I intended for only AAA and BBB,
    * but it breaks when I add "BBB-b"
    foreach country in AAA BBB {
      foreach year of local `country'_years {
        noi disp "Working on `country' `year'"
      }
    }    
    
    * Not quite what I want
    foreach country in AAA BBB BBB-b {
      foreach year in ``country'_years' {
        noi disp "Working on `country' `year'"
      }
    }    
    
    * Not quite what I want either
    foreach country in AAA BBB BBB-b {
      foreach year in "``country'_years'" {
        noi disp "Working on `country' `year'"
      }
    }
    Any advice is very much appreciated,

    Thank you!

  • #2
    This problem is nothing to do with looping as such.

    The problem is that minus signs (hyphens, dashes) are not allowed in Stata names. So why did your assignment appear to work?


    Code:
    local BBB-b_years = "2001 2002"
    is parsed like this

    local -- the user wants a local macro

    B -- part of the name of the macro, so keep going
    B -- another part of the name, ditto
    B -- another part of the name, ditto

    - not allowed in names, so (1) the name of the macro is BBB (2) this minus sign is the first character of the macro contents.

    If you look carefully you will see that the result of all that is a local macro name BBB and contents -b_years = "2001 2002"

    That is not what you want,

    So, how do I know this and more importantly how can you work this out for yourself?

    1. From reading the manual I know what characters are allowed in Stata names,

    2. A simple little trick often useful for finding out what has happened is

    Code:
    macro list
    which can be abbreviated to

    Code:
    mac li
    which dumps all the macros Stata knows at that point.

    3. Harder, but comes slowly with experience, is seeing things from Stata's point of view. It is a machine and is useless at reading your mind independently of what you say.

    The upshot of all this is use an underscore _ not a minus sign - in your macro name.




    Comment


    • #3
      Thank you for the clarification!

      There was a reason for not using underscore_. AAA and BBB were actually something like A_A_A and B_B_B and adding other underscores would create issues, because I parse those names later on.

      Solved my problem bypassing creating locals AAA_years, and instead looping though all countries and querying what years existed.

      Very useful to learn what Stata had interpreted from my error, though. Thankful for all learning I get from this forum!!!

      Comment

      Working...
      X