Announcement

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

  • How to convert a local string to a string variable and vice versa.

    Dear everyone,

    I would like to know if someone knows a STATA code that I can use to convert string local (for instance, 'local 8 = pop weight land' or 'local 7 pop weight land') to string variable.

    The code that I have been working on is the following:

    Code:
    local  1 Index_2018m9(`=tm(2017m1)') Index_2018m9(`=tm(2017m2)')  Index_2018m9(`=tm(2017m3)')  Index_2018m9(`=tm(2017m4)')  ///
             Index_2018m9(`=tm(2017m5)') Index_2018m9(`=tm(2017m6)')  Index_2018m9(`=tm(2017m7)')  Index_2018m9(`=tm(2017m8)')  ///
             Index_2018m9(`=tm(2017m9)') Index_2018m9(`=tm(2017m10)') Index_2018m9(`=tm(2017m11)') Index_2018m9(`=tm(2017m12)') ///
             Index_2018m9(`=tm(2018m1)') Index_2018m9(`=tm(2018m2)')  Index_2018m9(`=tm(2018m3)')  Index_2018m9(`=tm(2018m4)')  ///
             Index_2018m9(`=tm(2018m5)') Index_2018m9(`=tm(2018m6)')  Index_2018m9(`=tm(2018m7)')  Index_2018m9(`=tm(2018m8)')  ///
                                         Index_2018m9(`=tm(2018m10)') Index_2018m9(`=tm(2018m11)') Index_2018m9(`=tm(2018m12)')
    
    local  2 Index_2018m9(`=tm(2017m5)') Index_2018m9(`=tm(2017m6)')  Index_2018m9(`=tm(2017m7)')  Index_2018m9(`=tm(2017m8)')  ///
             Index_2018m9(`=tm(2017m9)') Index_2018m9(`=tm(2017m10)') Index_2018m9(`=tm(2017m11)') Index_2018m9(`=tm(2017m12)') ///
             Index_2018m9(`=tm(2018m1)') Index_2018m9(`=tm(2018m2)')  Index_2018m9(`=tm(2018m3)')  Index_2018m9(`=tm(2018m4)')  ///
             Index_2018m9(`=tm(2018m5)') Index_2018m9(`=tm(2018m6)')  Index_2018m9(`=tm(2018m7)')  Index_2018m9(`=tm(2018m8)')  ///
                                         Index_2018m9(`=tm(2018m10)') Index_2018m9(`=tm(2018m11)') Index_2018m9(`=tm(2018m12)')
    
    local  3 Index_2018m9(`=tm(2015m1)') Index_2018m9(`=tm(2015m2)')   ///
             Index_2018m9(`=tm(2015m5)') Index_2018m9(`=tm(2015m6)')   ///
             Index_2018m9(`=tm(2015m9)') Index_2018m9(`=tm(2015m10)')  ///
             Index_2018m9(`=tm(2016m1)') Index_2018m9(`=tm(2016m2)')   ///
             Index_2018m9(`=tm(2016m5)') Index_2018m9(`=tm(2016m6)')   ///
             Index_2018m9(`=tm(2016m9)') Index_2018m9(`=tm(2016m10)')  ///
             Index_2018m9(`=tm(2017m1)') Index_2018m9(`=tm(2017m2)')   ///
             Index_2018m9(`=tm(2017m5)') Index_2018m9(`=tm(2017m6)')   ///
             Index_2018m9(`=tm(2017m9)') Index_2018m9(`=tm(2017m10)')  ///
             Index_2018m9(`=tm(2018m1)') Index_2018m9(`=tm(2018m2)')   ///
             Index_2018m9(`=tm(2018m5)') Index_2018m9(`=tm(2018m6)')   ///
                                         Index_2018m9(`=tm(2018m10)')
    
    local  4 Index_2018m9(`=tm(2015m3)')  Index_2018m9(`=tm(2015m4)')  ///
             Index_2018m9(`=tm(2015m7)')  Index_2018m9(`=tm(2015m8)')  ///
             Index_2018m9(`=tm(2015m11)') Index_2018m9(`=tm(2015m12)') ///
             Index_2018m9(`=tm(2016m3)')  Index_2018m9(`=tm(2016m4)')  ///
             Index_2018m9(`=tm(2016m7)')  Index_2018m9(`=tm(2016m8)')  ///
             Index_2018m9(`=tm(2016m11)') Index_2018m9(`=tm(2016m12)') ///
             Index_2018m9(`=tm(2017m3)')  Index_2018m9(`=tm(2017m4)')  ///
             Index_2018m9(`=tm(2017m7)')  Index_2018m9(`=tm(2017m8)')  ///
             Index_2018m9(`=tm(2017m11)') Index_2018m9(`=tm(2017m12)') ///
             Index_2018m9(`=tm(2018m3)')  Index_2018m9(`=tm(2018m4)')  ///
             Index_2018m9(`=tm(2018m7)')  Index_2018m9(`=tm(2018m8)')  ///
             Index_2018m9(`=tm(2018m11)') Index_2018m9(`=tm(2018m12)')
    
    local  5 Index_2018m9(`=tm(2015m1)') Index_2018m9(`=tm(2015m3)')   ///
             Index_2018m9(`=tm(2015m5)') Index_2018m9(`=tm(2015m7)')   ///
             Index_2018m9(`=tm(2015m9)') Index_2018m9(`=tm(2015m11)')  ///
             Index_2018m9(`=tm(2016m1)') Index_2018m9(`=tm(2016m3)')   ///
             Index_2018m9(`=tm(2016m5)') Index_2018m9(`=tm(2016m7)')   ///
             Index_2018m9(`=tm(2016m9)') Index_2018m9(`=tm(2016m11)')  ///
             Index_2018m9(`=tm(2017m1)') Index_2018m9(`=tm(2017m3)')   ///
             Index_2018m9(`=tm(2017m5)') Index_2018m9(`=tm(2017m7)')   ///
             Index_2018m9(`=tm(2017m9)') Index_2018m9(`=tm(2017m11)')  ///
             Index_2018m9(`=tm(2018m1)') Index_2018m9(`=tm(2018m3)')   ///
             Index_2018m9(`=tm(2018m5)') Index_2018m9(`=tm(2018m7)')   ///
                                         Index_2018m9(`=tm(2018m11)')
    
    local  6 Index_2018m9(`=tm(2016m5)') Index_2018m9(`=tm(2016m7)')   ///
             Index_2018m9(`=tm(2016m9)') Index_2018m9(`=tm(2016m11)')  ///
             Index_2018m9(`=tm(2017m1)') Index_2018m9(`=tm(2017m3)')   ///
             Index_2018m9(`=tm(2017m5)') Index_2018m9(`=tm(2017m7)')   ///
             Index_2018m9(`=tm(2017m9)') Index_2018m9(`=tm(2017m11)')  ///
             Index_2018m9(`=tm(2018m1)') Index_2018m9(`=tm(2018m3)')   ///
             Index_2018m9(`=tm(2018m5)') Index_2018m9(`=tm(2018m7)')   ///
                                         Index_2018m9(`=tm(2018m11)')
    
    local  7 Index_2018m9(`=tm(2015m1)') ///
             Index_2018m9(`=tm(2015m5)') ///
             Index_2018m9(`=tm(2015m9)') ///
             Index_2018m9(`=tm(2016m1)') ///
             Index_2018m9(`=tm(2016m5)') ///
             Index_2018m9(`=tm(2016m9)') ///
             Index_2018m9(`=tm(2017m1)') ///
             Index_2018m9(`=tm(2017m5)') ///
             Index_2018m9(`=tm(2017m9)') ///
             Index_2018m9(`=tm(2018m1)') ///
             Index_2018m9(`=tm(2018m5)')
    
    local  8 Index_2018m9(`=tm(2015m2)')  Index_2018m9(`=tm(2015m4)')  ///
             Index_2018m9(`=tm(2015m6)')  Index_2018m9(`=tm(2015m8)')  ///
             Index_2018m9(`=tm(2015m10)') Index_2018m9(`=tm(2015m12)') ///
             Index_2018m9(`=tm(2016m2)')  Index_2018m9(`=tm(2016m4)')  ///
             Index_2018m9(`=tm(2016m6)')  Index_2018m9(`=tm(2016m8)')  ///
             Index_2018m9(`=tm(2016m10)') Index_2018m9(`=tm(2016m12)') ///
             Index_2018m9(`=tm(2017m2)')  Index_2018m9(`=tm(2017m4)')  ///
             Index_2018m9(`=tm(2017m6)')  Index_2018m9(`=tm(2017m8)')  ///
             Index_2018m9(`=tm(2017m10)') Index_2018m9(`=tm(2017m12)') ///
             Index_2018m9(`=tm(2018m2)')  Index_2018m9(`=tm(2018m4)')  ///
             Index_2018m9(`=tm(2018m6)')  Index_2018m9(`=tm(2018m8)')  ///
             Index_2018m9(`=tm(2018m10)') Index_2018m9(`=tm(2018m12)')
    
    local  9 Index_2018m9(`=tm(2016m2)')  Index_2018m9(`=tm(2016m4)')  ///
             Index_2018m9(`=tm(2016m6)')  Index_2018m9(`=tm(2016m8)')  ///
             Index_2018m9(`=tm(2016m10)') Index_2018m9(`=tm(2016m12)') ///
             Index_2018m9(`=tm(2017m2)')  Index_2018m9(`=tm(2017m4)')  ///
             Index_2018m9(`=tm(2017m6)')  Index_2018m9(`=tm(2017m8)')  ///
             Index_2018m9(`=tm(2017m10)') Index_2018m9(`=tm(2017m12)') ///
             Index_2018m9(`=tm(2018m2)')  Index_2018m9(`=tm(2018m4)')  ///
             Index_2018m9(`=tm(2018m6)')  Index_2018m9(`=tm(2018m8)')  ///
             Index_2018m9(`=tm(2018m10)') Index_2018m9(`=tm(2018m12)')
    
    local 10 Index_2018m9(`=tm(2015m2)')   ///
             Index_2018m9(`=tm(2015m6)')   ///
             Index_2018m9(`=tm(2015m10)')  ///
             Index_2018m9(`=tm(2016m2)')   ///
             Index_2018m9(`=tm(2016m6)')   ///
             Index_2018m9(`=tm(2016m10)')  ///
             Index_2018m9(`=tm(2017m2)')   ///
             Index_2018m9(`=tm(2017m6)')   ///
             Index_2018m9(`=tm(2017m10)')  ///
             Index_2018m9(`=tm(2018m2)')   ///
             Index_2018m9(`=tm(2018m6)')   ///
             Index_2018m9(`=tm(2018m10)') 
    
    local predictores 1 2 3 4 5 6 7 8 9 10
    Those "locals", I use them as independent variables in a regression. I made many regressions. From those regressions, I chose the best regression and therefore I know what "local" number (for instance, "local 8") was used for that regression (for example, DID regression).
    Now, I want to run another regression (for example, data panel) with the independent variables that were used for the best DID regression.
    I do not know how to use the same "local" (for instance, "local 8") I used in a loop, but now for another regression.

    It occurs to me to convert the "local" to a variable string and when I want to use the "local 8" (now in a variable string), I will convert the variable string to "local" one. But I do not know how.

    I hope I didn't confuse you.

    Thanks a lot for your help

    Alexis Rodas


  • #2
    I will start by saying that my preference is to use macro names that start with a letter. So in the very simple example code below I have done that, and I hope it points you in a useful direction. My understanding of what you hope to accomplish may not be correct.

    Code:
    . // use my sample data
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . 
    . // define two lists of independent variables as local macros named v1 and v2
    . local v1 weight length
    
    . local v2 weight mpg 
    
    . 
    . // use a loop to fit regressions trying each set of independent variables
    . forvalues i=1/2 {
      2.     display " regression: `i' " _newline " list: v`i'  variables: `v`i'' "
      3.     regress price `v`i''
      4. }
     regression: 1 
     list: v1  variables: weight length 
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(2, 71)        =     18.91
           Model |   220725280         2   110362640   Prob > F        =    0.0000
        Residual |   414340116        71  5835776.28   R-squared       =    0.3476
    -------------+----------------------------------   Adj R-squared   =    0.3292
           Total |   635065396        73  8699525.97   Root MSE        =    2415.7
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          weight |   4.699065   1.122339     4.19   0.000     2.461184    6.936946
          length |  -97.96031    39.1746    -2.50   0.015    -176.0722   -19.84838
           _cons |   10386.54   4308.159     2.41   0.019     1796.316    18976.76
    ------------------------------------------------------------------------------
     regression: 2 
     list: v2  variables: weight mpg 
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(2, 71)        =     14.74
           Model |   186321280         2  93160639.9   Prob > F        =    0.0000
        Residual |   448744116        71  6320339.67   R-squared       =    0.2934
    -------------+----------------------------------   Adj R-squared   =    0.2735
           Total |   635065396        73  8699525.97   Root MSE        =      2514
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          weight |   1.746559   .6413538     2.72   0.008      .467736    3.025382
             mpg |  -49.51222   86.15604    -0.57   0.567    -221.3025     122.278
           _cons |   1946.069    3597.05     0.54   0.590    -5226.245    9118.382
    ------------------------------------------------------------------------------
    
    . 
    . // run the best model again on only foreign cars
    . 
    . local vars
    
    . display " new regression " _newline " list: v1  variables: `v1' "
     new regression 
     list: v1  variables: weight length 
    
    . regress price `v1' if foreign==1
    
          Source |       SS           df       MS      Number of obs   =        22
    -------------+----------------------------------   F(2, 19)        =     34.72
           Model |   113350528         2    56675264   Prob > F        =    0.0000
        Residual |  31012684.7        19  1632246.56   R-squared       =    0.7852
    -------------+----------------------------------   Adj R-squared   =    0.7626
           Total |   144363213        21   6874438.7   Root MSE        =    1277.6
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          weight |   4.937235   1.557596     3.17   0.005     1.677149    8.197321
          length |   14.76399   49.29231     0.30   0.768      -88.406     117.934
           _cons |  -7537.909   5247.076    -1.44   0.167    -18520.17    3444.348
    ------------------------------------------------------------------------------

    Comment

    Working...
    X