Announcement

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

  • Appending strings with spaces to a macro in a loop (for use in estout mlabels)

    I'm trying to append quoted strings that contain spaces to a local macro, which I then pass into the -mlabels- option of -estout-. This code
    Code:
    sysuse auto, clear
    
    local estim_names
    foreach v of varlist mpg headroom trunk weight {
        regress price `v', robust
        estimates store estim_`v'
        local estim_name `""robust `v'""'
        local estim_names `estim_names' `estim_name'
    }
    display `"`estim_names'"'
    displays
    Code:
    robust mpg "robust headroom" "robust trunk" "robust weight"
    where for some reason, the quotes on the first quoted string in the loop are omitted. How do I fix this? The actual code is much more complicated, but basically, a local macro named -estim_name- is created at some point, and then at the end of the loop, that local macro is append to the local macro -estim_names-. These are then passed into -estout-:
    Code:
    estout estim_mpg estim_headroom estim_trunk estim_weight ///
        using "table.txt", style(fixed) replace mlabels(`estim_names')

    This is example code only, but I post it as a minimal working example of the larger code base to demonstrate the problem. To be very clear, since the actual code is much more complicated, a solution that just appends some value to -estim_names- directly WILL NOT WORK, e.g. this:
    Code:
    local estim_names `estim_names' "robust `v'"
    The complete code base loops over numerous different models, and -estim_names- is created by a bunch of branching statements, so I can't condense this all into one line.
    Last edited by Michael Anbar; 11 May 2016, 10:08.

  • #2
    It hurts my pride to post such an obvious hack as the one below, but it does seem to accomplish what you need.
    Code:
    sysuse auto, clear
    
    local estim_names dummy
    foreach v of varlist mpg headroom trunk weight {
        regress price `v', robust
        estimates store estim_`v'
        local estim_name `""robust `v'""'
        local estim_names `estim_names' `estim_name'
    }
    display `"`estim_names'"'
    local dummy dummy
    local estim_names : list estim_names - dummy
    display `"`estim_names'"'

    Comment


    • #3
      William Lisowski I forgot about using macro lists for this, but that hack does work. Thank you! Surely there's a better way to do this? I'm convinced that I'm just "a quote away" from balancing all the quotes and getting this right.

      Comment


      • #4
        Code:
        sysuse auto, clear
        foreach v of varlist mpg headroom trunk weight {
        regress price `v', robust
        estimates store estim_`v'
        local estim_names " `estim_names' `"robust `v'"' "
        local estim_names2: list retokenize estim_names //remove extra spaces
        local estim_names3: list clean estim_names //reomove extra space & minimal adornment
        }
        display `"`estim_names'"'
        display `"`estim_names2'"'
        display `"`estim_names3'"'
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          I think this is what you are looking for:

          Code:
          local estim_names
          sysuse auto, clear
          foreach v of varlist mpg headroom trunk weight {
              regress price `v', robust
              estimates store estim_`v'
              local estim_names `"`estim_names' "robust `v'""'
          }
          display `"`estim_names'"'

          Comment


          • #6
            Carole J. Wilson That code doesn't work. It displays
            Code:
                  `"robust mpg"'  `"robust mpg"'  `"robust headroom"'  `"robust trunk"'  `"robust weight"'
            `"robust mpg"' `"robust mpg"' `"robust headroom"' `"robust trunk"' `"robust weight"'
            "robust mpg" "robust mpg" "robust headroom" "robust trunk" "robust weight"
            which is incorrect.
            Last edited by Michael Anbar; 11 May 2016, 12:10.

            Comment


            • #7
              Robert Picard Thank you; this is the code I ended up using:
              Code:
              sysuse auto, clear
              
              local estim_names
              foreach v of varlist mpg headroom trunk weight {
                  regress price `v', robust
                  estimates store estim_`v'
                  local estim_name "robust `v'"
                  local estim_names `"`estim_names' "`estim_name'""'
              }
              Per my original post (see the bold/underlined section and the last code sample...), I can't use a solution that does everything in one line because the code that actually creates the title string is fairly complicated, but I got the hint I needed from your use of double quotes. Thanks.

              Comment


              • #8
                Michael, I'm not sure what version you are using or if you have made some change to the code, but the code in #4 produces identical results in version 13.1 and 14.1

                Code:
                . sysuse auto, clear
                (1978 Automobile Data)
                r; t=0.00 13:16:25
                
                . foreach v of varlist mpg headroom trunk weight {
                  2. regress price `v', robust
                  3. estimates store estim_`v'
                  4. local estim_names " `estim_names' `"robust `v'"' "
                  5. local estim_names2: list retokenize estim_names //remove extra spaces
                  6. local estim_names3: list clean estim_names //reomove extra space & minimal ador
                > nment
                  7. }
                
                Linear regression                                      Number of obs =      74
                                                                       F(  1,    72) =   17.28
                                                                       Prob > F      =  0.0001
                                                                       R-squared     =  0.2196
                                                                       Root MSE      =  2623.7
                
                ------------------------------------------------------------------------------
                             |               Robust
                       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                         mpg |  -238.8943   57.47701    -4.16   0.000    -353.4727    -124.316
                       _cons |   11253.06   1376.393     8.18   0.000     8509.272    13996.85
                ------------------------------------------------------------------------------
                
                Linear regression                                      Number of obs =      74
                                                                       F(  1,    72) =    1.76
                                                                       Prob > F      =  0.1888
                                                                       R-squared     =  0.0131
                                                                       Root MSE      =  2950.4
                
                ------------------------------------------------------------------------------
                             |               Robust
                       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                    headroom |   399.2149   300.9356     1.33   0.189    -200.6892     999.119
                       _cons |    4970.31   856.2261     5.80   0.000     3263.454    6677.165
                ------------------------------------------------------------------------------
                
                Linear regression                                      Number of obs =      74
                                                                       F(  1,    72) =   14.00
                                                                       Prob > F      =  0.0004
                                                                       R-squared     =  0.0988
                                                                       Root MSE      =  2819.4
                
                ------------------------------------------------------------------------------
                             |               Robust
                       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                       trunk |   216.7482   57.93111     3.74   0.000     101.2647    332.2318
                       _cons |   3183.504   728.6539     4.37   0.000     1730.959    4636.049
                ------------------------------------------------------------------------------
                
                Linear regression                                      Number of obs =      74
                                                                       F(  1,    72) =   27.51
                                                                       Prob > F      =  0.0000
                                                                       R-squared     =  0.2901
                                                                       Root MSE      =  2502.3
                
                ------------------------------------------------------------------------------
                             |               Robust
                       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                      weight |   2.044063   .3897465     5.24   0.000     1.267117    2.821008
                       _cons |  -6.707353   1032.394    -0.01   0.995    -2064.747    2051.332
                ------------------------------------------------------------------------------
                r; t=0.04 13:16:25
                
                . display `"`estim_names'"'
                     `"robust mpg"'  `"robust headroom"'  `"robust trunk"'  `"robust weight"' 
                r; t=0.00 13:16:25
                
                . display `"`estim_names2'"'
                `"robust mpg"' `"robust headroom"' `"robust trunk"' `"robust weight"'
                r; t=0.00 13:16:25
                
                . display `"`estim_names3'"'
                "robust mpg" "robust headroom" "robust trunk" "robust weight"
                r; t=0.00 13:16:25
                Stata/MP 14.1 (64-bit x86-64)
                Revision 19 May 2016
                Win 8.1

                Comment

                Working...
                X