Announcement

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

  • Problem with saving graphs with wildcard in names

    Hi there!

    I'm having some difficulties saving and combining graphs that I have created using a foreach loop and the mrgraph command. The problem seems to be related to the fact that I'm using a wildcard with the mrtab command, but the resulting graphs can't be saved with the "*" in their name (because I suppose it is invalid syntax).

    I'm using the following code:

    local Q1a1vars Q1a1_*
    local Q1a2vars Q1a2_*
    local mrvars `Q1a1vars' `Q1a2vars'

    foreach var of local mrvars {
    mrtab `var'
    mrgraph hbar `var' , stat(col) name(`var')
    }

    where the local Q1a1vars, for example, contains Q1a1_a Q1a1_b Q1a1_c etc. (which is why I use the wildcard). mrvars is therefore a local of locals. In the actual dataset, I hope to perform this foreach loop over a total of 33 similarly constructed locals, but I'm just including two here so as to not over-complicate things.

    I'm able to successfully generate the graphs using mrgraph, it's just the naming that is a problem.

    Does anyone have any advice about how I could name the graphs?

    Please let me know if any additional information is needed!
    -Alec


  • #2
    If you simply want to remove "_*", you can define a local

    Code:
    foreach var of local mrvars {
         mrtab `var'
         local name = subinstr("`var'", "_*", "",.)
         mrgraph hbar `var' , stat(col) name(`name')
    }

    Comment


    • #3
      Thanks Andrew-- that worked perfectly (and is now super obvious). Think I just needed a second set of eyes. Cheers!

      Comment


      • #4
        You have a fundamental problem with your code in post #1. The commands
        Code:
        local Q1a1vars Q1a1_* 
        local Q1a2vars Q1a2_*
        do not expand the variable names as you think they do, because variable name expansion only occurs where Stata is expecting a variable list.

        Consider the following example based on your code, and the corrected implementation.
        Code:
        . describe, simple
        a_1  a_2  a_3
        
        . 
        . local avars a_* 
        
        . macro list _avars
        _avars:         a_*
        
        . 
        . foreach var of local avars {
          2. list `var' in 1
          3. }
        
             +-----------------+
             | a_1   a_2   a_3 |
             |-----------------|
          1. |   1     2     3 |
             +-----------------+
        
        . 
        . unab avars : a_*
        
        . macro list _avars
        _avars:         a_1 a_2 a_3
        
        . 
        . foreach var of local avars {
          2. list `var' in 1
          3. }
        
             +-----+
             | a_1 |
             |-----|
          1. |   1 |
             +-----+
        
             +-----+
             | a_2 |
             |-----|
          1. |   2 |
             +-----+
        
             +-----+
             | a_3 |
             |-----|
          1. |   3 |
             +-----+

        Comment


        • #5
          You have a fundamental problem with your code in post #1. The commands
          Code:
          local Q1a1vars Q1a1_* local Q1a2vars Q1a2_*
          do not expand the variable names as you think they do, because variable name expansion only occurs where Stata is expecting a variable list.
          I think Alec wants these to be read literally in the graph command. Consider the following:

          Code:
          sysuse auto, clear
          tab rep78, gen(r78)
          local vars r78*
          gr bar `vars'

          Comment

          Working...
          X