Announcement

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

  • Probem with using global macro in a string before an underscore

    Hi statalisters,

    I want to use global macro's to refer to folder names, but ran into some unexpected behaviour when using them before the underscore "_" symbol.

    Stata seems to ignore the macro that goes before the underscore.

    *For example when i do this:

    global year "2019"
    global month "10"

    di "$year_$month"


    The console prints: "10" and ignores my call for year.

    I want to get to "2019_10".

    Could someone explain to me why the first macro gets ignored, and how i could solve it?
    Thanks in advance!




  • #2
    I think the problem may be that the name of the first macro is interpreted as "year_" and there is no such macro, so it displays as blank. You can get around the problem using braces around the macro name:

    Code:
    . global year "2019"
    
    . global month "10"
    
    . di "${year}_${month}"
    2019_10

    Comment


    • #3

      That is,

      1. Underscore is certainly allowed in Stata names.

      2. Unless otherwise prompted, Stata keeps on going when it thinks it is reading a name and looks for the longest possible legal name.

      I see no reason here not to use local macros, as in


      Code:
      local  year "2019"
      local  month "10"
      
      di "`year'_`month'"

      Comment

      Working...
      X