Announcement

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

  • Please help with compound quotes--locals

    I have read probably a dozen different people's answers on this topic and tried a dozen different versions of this code but for whatever reason I cannot make the compound quotes work properly.

    The goal is to create a global variable (could be a local variable, it doesn't matter that much to me) that I can sub in nicely in front of label var. Let me give you the full code first:

    Code:
    sysuse auto, clear
    describe, replace
    
    
    forval i=1/7 {
        local j=name in `i'
        di "`j'"
        local k=varlab in `i'
        local kq=`"""' "`k'" `"""'
        di `kq'
        
        global vlab`i' "`j'" "`kq'"
    
    }
    
    sysuse auto, clear
    
    forval i=1/7 {
    
        label var "${vlab`i'}"
        
    }
    The key is that I want the code near the bottom

    Code:
    label var "${vlab`i'}"
    To read this on the first loop:
    Code:
    label var "${vlab`i'}"

    I may have done the last part wrong too, but it's irrelevant right now because:

    Code:
    sysuse auto, clear
    describe, replace
    
    
    forval i=1/7 {
        local j=name in `i'
        di "`j'"
        local k=varlab in `i'
        local kq=`"""' "`k'" `"""'
        di `kq'
        
        global vlab`i' "`j'" "`kq'"
    
    }
    Returns the following error:
    invalid '"Make and Model'
    The first compound quote seems to work, but the second one doesn't. I am completely stumped. How do I get it so I get quotations surrounding the value of varlab?

    Thanks,
    Jonathan

  • #2
    In Stata a variable is, and is only, in other terms a column or field in a dataset. Local and global macros are best referred to using their proper names, as macros.

    That out of the way, there are are various problems here.

    The qualifier in has no role in defining either kind of macro. Use so-called subscripts instead.

    In your code you separate one pair of compound double quotes

    Code:
    `" "'
    into

    Code:
    ` " " '
    which is likely to be a problem.

    This works, but leaves unclear to me what you are trying to do and why.

    Code:
    describe, replace
    
    forval i=1/7 {
        local j = name[`i']
        local k = varlab[`i']
        global vlab`i' "`j' `k'"
    }
    
    macro list
    Perhaps you want something more like

    Code:
    global name_vlab`i'   `j' "`k'"
    That doesn't need any use of compound double quotes, which may be needed for your real problem.

    Last edited by Nick Cox; 21 Dec 2025, 05:12.

    Comment


    • #3
      Thanks! That gets me a lot closer. For some reason I still can't get it to work, though, probably because of the code near the end.

      This is what I have now.

      Code:
      sysuse auto, clear
      describe, replace
      
      
      forval i=1/7 {
          local j=name in `i'
          di "`j'"
          local k=varlab in `i'
          global name_vlab`i'   `j' "`k'"
      }
      
      macro list
      
      
      sysuse auto, clear
      
      foreach v of varlist * {
          label variable `v' ""    
      }
      
      forval i=1/7 {
          label var "${name_vlab`i'}"
      }
          
      }
      This gives me the error:

      "make invalid name
      What I need is to get an equivalent to the following when we get to the line starting with label var:

      Code:
      label var make "Make and Model"
      label var price "Price"
      label var mpg "Mileage (mpg)"
      For reference, this does work:

      HTML Code:
      label var $name_vlab1
      So probably something is wrong with the brackets around name_vlab`i'?

      (The purpose is to label a new dataset with the same variable labels as the old dataset--same variables, but no labels)

      Comment


      • #4
        Of course, I posted the reply and then immediately got it.

        This is the correct code in case someone else needs it:

        Code:
        sysuse auto, clear
        describe, replace
        
        
        forval i=1/7 {
            local j=name in `i'
            di "`j'"
            local k=varlab in `i'
            global name_vlab`i'   `j' "`k'"
        }
        
        macro list
        
        
        
        sysuse auto, clear
        
        foreach v of varlist * {
            label variable `v' ""
            
            
        }
        
        forval i=1/7 {
        
            label var ${name_vlab`i'}
            
        }
        Thanks for your help.

        Comment


        • #5
          It was a surprise to me that your syntax

          Code:
           
           local j=name in `i'
          works, but it does. I'd still recommend use of subscripts. at least on style grounds.

          Comment

          Working...
          X