Announcement

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

  • Run program multiple times with small changes

    I am running the same kind of regressions with small changes, and I feel like I am doing it in an awkward way, like this:
    Code:
    capture program drop example
    program example
    {
    local y = varlist
    reg x `y'
    }
    end
    
    capture drop varlist
    g varlist = "a"
    example
    
    capture drop varlist
    g varlist = "a b"
    example
    It works, but I am defining a variable varlist (with the same value for perhaps millions of observations), and I specify it as a local in the program. I have tried using local and global outside of the program (instead of, or in addition to specifying the local in the program) and it didn't work. I have read sections of the Stata manual and searched this forum, and although I'm sure there is an example out there that does what I want in a better way, I couldn't find it. I encounter the same problem when instead of defining a program, I call on another .do file; I suppose the solution will be similar. For numerical values I have found that I can use
    Code:
     scalar number = 1
    before calling on the program and just use "number" in the program - is there an equivalent for strings?

    Thank you!

  • #2
    Can you describe why you want to what you want to do? At the moment, I am confused why you want to run a regression of dependent variable on a constant. Besides that, your code does not do what you think it does.
    So please, provide the background of your question and if possible a sample of your data. Otherwise, it will be hard to give you a meaningful answer.

    Comment


    • #3
      Thank you Sven-Kristjan for your reply! a and b are variables in my example, not constants.

      Example data might look like this:
      x a b
      1 2 3
      2 1 4
      2 2 5
      2 3 5
      I hope that clarifies it. The code does exactly what I want.

      Although I'm not sure if it will be useful to provide more background information, I want to use different versions of variables in regressions as a robustness check. In this particular example, I have 4 "main" regressions explaining weekly stock returns using, among others, stock liquidity as a control variable. 3 out of the 4 have the same liquidity control variable (the other one doesn't have it). But, there are a lot of alternative stock liquidity measures, so I want to see if my results are robust to using different liquidity measures. Since they are highly correlated, I do not want to add them all in the same regression. The example code would become more like this

      Code:
      capture program drop stockreturn
      program stockreturn
      {
      local y = varlist
      reg return `y'
      reg return retailvolume `y'
      }
      end
      
      capture drop varlist
      g varlist = "marketreturn liquidity"
      stockreturn
      
      capture drop varlist
      g varlist = "marketreturn alternativeliquidity"
      stockreturn
      
      capture drop varlist
      g varlist = "marketreturn alternativeliquidity2"
      stockreturn
       ...
      Last edited by Paulan van Nes; 29 Nov 2019, 13:57.

      Comment


      • #4
        You can do something like "g varlist = ........" , but it's almost certainly not what you want. That will assign the same list of variable names to every observation of a variable called varlist. And, you will not be able to program with it.

        What you are trying to do, I believe, can be accomplished in various ways. Here's an illustration parallel to what I think you want to do:
        Code:
        sysuse auto
        foreach v of varlist mpg headroom displacement {
           regress price weight `v'
        }
        If this looks incomprehensible to you, you'll want to look at the documentation for -foreach-, and possibly for -macro-.

        Before posting another question, I'd encourage you to take a look at the StataList FAQ, which explains how to present code and example data, among many other things.

        Comment


        • #5
          Thank you Mike Lacy. You are right in that I do not want to assign the same list of variable names to every observation, as I mentioned in the opening post, but other than that it works. Thank you for pointing out the foreach function! In this example it will work fine indeed.

          However, it seems harder to use when I simultaneously want to change more things in the program, like the name of the output file written using e.g. outreg2. Now, in this case, I could settle for the file names to include the exact variable name, but that would not be ideal and that still wouldn't solve the problem when I want to add other changes (like changing the value of a scalar).
          Last edited by Paulan van Nes; 01 Dec 2019, 21:01.

          Comment


          • #6
            I have found the solution to my problem: simply define a scalar instead of a variable. I did not realize that a scalar could be a string. The solution to my original example problem thus becomes
            Code:
            capture program drop example
            program example
            {
            local y = varlist
            reg x `y'
            }
            end
            
            capture drop varlist
            scalar varlist = "a"
            example
            
            capture drop varlist
            scalar varlist = "a b"
            example
            Last edited by Paulan van Nes; 04 Dec 2019, 17:09.

            Comment

            Working...
            X