Announcement

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

  • Referencing local variable in nested foreach var of varlist

    Hi,

    I would like to do something which I believe is fairly simple, but I can't get the syntax correct. I would like to nest two for loops together, one which loops through a local variable and one which uses the value of the local to pull all the variables that start with that local.

    My data contains, for example, the following columns:
    B1_var1 B1_var2 B1_var3
    B5_var1 B5_var2 B5_var3
    ...

    The B1 variables come from the B1 table, B5 from B5 table, and so on.

    I would like to loop through the variables by first referencing its table, and then running a regression for each variable which begins with the table prefix.

    Code:
    local tables "B1 B5 C3 C3A C5 C6"
    foreach table of local tables{
        foreach var of varlist `table'_*{
            reghdfe `var' shock, a(id year) cluster(id)
        }
    }
    What is the correct syntax?

    Thanks!

  • #2
    As far as I can tell with my toy example quoted below, the syntax you used will do what the title of this topic describes.
    Code:
    . describe, simple
    B1_a  B1_b  C6_y  C6_z
    
    . local tables "B1 C6"
    
    . foreach table of local tables{
      2.     foreach var of varlist `table'_*{
      3.         display `" will run "reghdfe `var' shock, a(id year) cluster(id)" "'
      4.         }
      5. }
     will run "reghdfe B1_a shock, a(id year) cluster(id)" 
     will run "reghdfe B1_b shock, a(id year) cluster(id)" 
     will run "reghdfe C6_y shock, a(id year) cluster(id)" 
     will run "reghdfe C6_z shock, a(id year) cluster(id)"
    If this doesn't answer your question, before posting further, please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], as described in section 12 of the FAQ.

    Section 12.1 is particularly pertinent

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    ...
    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.

    Comment


    • #3
      The syntax is exactly what you show in your post.

      Are you getting any error messages from it? If so, show the output you are getting along with an example of your data that demonstrates the problem (using the -dataex- command, of course).

      If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      Added: Crossed with #2.

      Comment


      • #4
        Let me follow up with a guess.

        Perhaps your problem is that you have written your code in the do-file editor window, and then rather than running everything at once, you are running it by selecting a few lines and running them, then selecting the next few lines and running them, and so on. I can't tell, because you have not shown the results of running your code, but this is a very high on my FQA - frequent question answers - list.

        Consider the following example. In the do-file editor window, I have a two-line program that I run in its entirety.
        Code:
        . do "/Users/lisowskiw/Downloads/example.do"
        
        . local message Hello, world.
        
        . display "The message is `message'"
        The message is Hello, world.
        
        . 
        end of do-file
        Now I run the same two lines by selecting the first line and running it, then selecting the second line and running it.
        Code:
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
        
        . local message Hello, world.
        
        . 
        end of do-file
        
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
        
        . display "The message is `message'"
        The message is 
        
        . 
        end of do-file
        The important thing to keep in mind is that local macros vanish when the do-file within which they were created ends. If you look carefully at the results above, you'll see that when I selected a single line to run, it was copied into a temporary do-file and run, so even though both lines are in the same window in the do-file editor, they are run as separate do-files, and local macro defined in the first line vanishes at the end of that do-file, and is undefined when the second line is run.

        Comment


        • #5
          Hi everyone, Thanks for the input -- turns out it was the local macros + copying and pasting issue. I will know to be aware of this issue in the future.

          Comment

          Working...
          X