Announcement

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

  • Invalid name in loop

    Hello everyone,

    I have a part of short code that generate invalid name but I couldn't figure out why. My goal is to generate a long varlist to use in subsequent. Any help or suggestions would be appreciated. Thanks a lot!

    Code:
    set more off, permanently
    
    use unbalanced.1--16.dta, clear
    
    local VarsLabor LegalEntityCode PersonnelEducationExpenses Employment TotalProfit IndustrySalesValue_currentpr ///
            LaborUnionPersons LaborUnionExpense
    foreach x of local VarsLabor{
        foreach i of numlist 1/16{
        local `x'l "`x'l" "`x'`i'"
        }
        dis "``x'l'" ,
        local VarsLaborFull "`VarsLaborFull'" "``x'l'"
    }
    
    display "`VarsLaborFull'"
    keep `VarsLaborFull'
    The error return is:
    Code:
    "  invalid name

  • #2
    The beginning and end " will be stripped as delimiters. That's not what you want. The full machinery for this includes compound double quotes, but I think this may be what you want:

    Code:
    local VarsLabor LegalEntityCode PersonnelEducationExpenses Employment TotalProfit IndustrySalesValue_currentpr ///
            LaborUnionPersons LaborUnionExpense
      
    foreach x of local VarsLabor {
        forval i = 1/16 {
            local `x'l `x'l `x'`i'
        }
        local VarsLaborFull `VarsLaborFull' ``x'l'
    }
    
    display "`VarsLaborFull'"

    Comment


    • #3
      Thank you so much! I did not fully understand the role of " " in the macro because oftentimes it is not necessary. Am I wrong about this?

      btw: I misst a ` ' in the

      Code:
       local `x'l ``x'l' `x'`i'

      Comment

      Working...
      X