0

In Stata, I'm trying to use the command include to run several regressions in one do file. The overall goal is to help in forecasting Natural Gas production.

I have a do file for each product and basin that I'm interested in. Within each do file I am running several versions of the regression based on data available at specific times (e.g. the first set of regressions is for product type 4, in basin 2, with information available in June 2020).

The regression command within the do file looks something like this:

include "gas\temp\NG var.doh"

foreach time in  dec14 dec15 dec16 dec17  previous current {
    arima price_`perm4type' `perm4pvars' if tin(,`yq_`time'') , ar(1) ma()
}

I have the perm4pvars defined in the file NG var.doh like this:

local perm4pvars  txfreeze PNGHH_`time' d.CPIE_`time' d.IFNRESPUOR_`time' POILWTI_`time' 

When I run my do file the time from my doh file doesn't show up. So I get an error message: "PNGHH_ is an ambiguous abbreviation"

How can I get the time to show up in my regress command?

I did try doing this in my .doh file

foreach time in  dec14 dec15 dec16 dec17  previous current {
    local perm4pvars  txfreeze PNGHH_`time' d.CPIE_`time' d.IFNRESPUOR_`time' POILWTI_`time'
}

I got it to run, only for time=current

2
  • I wanted to use the include command so I can change the variables across multiple do files all at once. Commented Nov 21, 2022 at 15:17
  • I see what the problem is now. My advice is to keep do-files simple and self-contained. Commented Nov 21, 2022 at 15:34

2 Answers 2

1

The relationship between the included .do file and the main .do file is not symmetric.

The point of include is that the included do file is read into the main do file. But the included do file knows nothing about any files it is included in. So definitions in the included do file may not usefully refer to definitions in the main .do file, or to any others, unless they in turn are included, or so I presume.

That explains why your reference to local macro time in the included file doesn't do what you want. It's not illegal in any do file or Stata program to refer to a local macro that doesn't exist (meaning, is not visible from the file) but as here the consequences may not be what you want.

Sign up to request clarification or add additional context in comments.

Comments

0

Imo your issue is more basic than what the above answer suggests, in that you want to use locals from a loop outside of it. Or, in the case where you got it to run for time=current, misunderstanding what the loop does. Might be good to get a good understanding of the functioning of loops first, also since you seem to be using multiple other loops that are not detailed in your question and where I can only assume those are specified correctly.

Assuming the gas\temp\NG var.doh file only holds the line defining the local, i.e. local perm4pvars txfreeze PNGHH_`time' d.CPIE_`time' d.IFNRESPUOR_`time' POILWTI_`time', a way to get it working the way you want (or at least how I think you want it, since you do not detail the specifications you want to achieve with your loop) is to move the include inside the loop, changing your code to:

foreach time in dec14 dec15 dec16 dec17 previous current {
    include "gas\temp\NG var.doh"
    arima price_`perm4type' `perm4pvars' if tin(,`yq_`time'') , ar(1) ma()
}

This way, the line in the included .do file can use the local time from the loop. In the case there is more in the .doh file you would have to change your code a bit more to get it to work, however I can't help you with that unless you give me more information about the structure of the code and what you want to achieve with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.