Macro within a loop and include - stata

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

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.

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.

Related

Is there a way to solve Stata's r(601) while looping the append for imported excel files?

I am trying to append multiple Excel files into a large database by executing the following code:
cls
set more off
clear all
global route = "C:\Users\NICOLE\Desktop\CAR"
cd "$route"
tempfile buildDB
save `buildDB', emptyok
local filenames : dir "$route" files "*.xlsx"
display `filenames'
foreach f of local filenames {
import excel using `"`f'"' ,firstrow allstring clear
gen source = `"`f'"'
append using `buildDB'
save `"`buildDB'"', replace
}
save "C:\Users\NICOLE\Desktop\CAR\DB_EG-RAC.dta" ,replace
Stata manages to append all of the files, but it also displays the following message of error:
file C:\Users\NICOLE.xlsx not found r(601);
And I do not know how to solve it, because it does not let my code run as it should. Thanks!
We have deadlock here. On the face of it the filename in question is not one you write in your code, but could only be part of the result of
local filenames : dir "$route" files "*.xlsx"
But the file named isn't even in the same directory as that named. Moreover, you are adamant that the file doesn't exist and Stata according to your error report can't find it.
The question still remains: how does Stata get asked to open a file that supposedly doesn't exist?
My only guesses are feeble:
Code you are not showing is responsible.
You are running slightly different versions of this script in different places and getting confused. Can you replicate this error that you did get once all over again? Have you searched everywhere remotely possible on the C: drive for this file nicole.xlsx?
It is crucial to realise that we can test nothing here. The problem has not been presented reproducibly.

GAMS: Filename cannot be used as valid UEL

I trying to merge a large data set in gams. The file should consist of multiple gdx files with several names. The programme merges the files as I would like them to however: it replaces the names of the file to be merged with File_1, File_2, File_3 and so on. I would like to see the name of the gdx file in the merged file (and so far the script I wrote worked fine).
I'm receiving the following error for each line:
***Filename cannot be used as a valid UEL
Existing name: ImpactYesPGTNoLDViolation-D1-PG10-LDI-LDE0.001-LB0.0045-PDC0-D10
Replaced with File_1
Why does this happen? Could it be that the existing name is too long? I tried finding out more about this error but so far have not found any information on it. And is there anyway to fix it? I need the information of the existing name in order to further process the output.
You are right. This name is too long to be used as UEL (aka label). You can only use up to 63 characters. You can read more about this and other limitations for UELs here

How to use the original filename in a multi file template in resharper?

I have a multi file template in resharper and I can use $NAME$ macro to get the name of the original file to use to name the other files in the template. But I also want to use the $NAME$ of the original file in the content of the other file template.
Is this possible? I can't see a macro which seems suitable for the internal variables as onlt the Current File Name seems available.
Anyone know if this is possible or how I might workaround this?
As a workaround, you may create a parameter $FILENAME$ (macro "Current file name without extension") in the first file e.g. in the comments, like:
class Foo
{
//$FILENAME$
}
Then you may call this parameter in other files of the multifile template - this parameter will contain the name of the first file since the first file will be generated before other ones.
Unfortunately, there isn't a macro that will give you this. I've added a feature request that you can vote on and track (and more specific detail as to what your requirements are would be useful) - http://youtrack.jetbrains.com/issue/RSRP-415055
It is possible to write your own macros as part of a plugin, but there isn't a sure-fire way of getting the name of the first document in the created file set. The IHotspotSessionContext instance that is passed to the macro via IHotspotSession.Context property includes an enumerable of IDocument, from which you can get IDocument.Moniker, which will be the full path for file based documents. However, there's no guarantee of the order of the enumerable - it's backed by a hashset. You might be able to rely on implementation details (small set, no removes) to be able to use the first document as the original, but there is really no guarantee of this.

QPixmap::save() returns successful, but yields no file

The docs say this should work:
bool did=pixmap.save( "hoppy.png" );
qDebug("did is: %d",did);
My logging returns 1 suggesting, as per docs, that the save was successful. However, no file appears on my drive. According to the docs, this save() should indeed save a file, so what is missing?
According to the docs, this save() should indeed save a file, so what is missing?
Nothing missing; it is simply saved to the folder of where you run the qt executable from. If it is a direct invocation, then it is beside the executable, otherwise it is beside the script or other program that calls the qt executable (sure, the other program could modify the current working directory, but let us forget about that for a bit).
If that is not what you would like to do, you better use an absolute path to the location where you wish to save it. However, if it is some common location, consider using QStandardPaths.
Another answer notes:
Nothing missing; it is simply saved to the folder of where you run the qt executable from
However my question indicated that this is actually not the case here.
Using the full path rather than a relative path or instead of using the ~ character, resolved it.

How to override the default text in MATLAB

In MATLAB, when you click File -> New -> Function M-File, you get a file with the following contents:
function [ output_args ] = Untitled( input_args )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
end
Is it possible to override this behaviour, and specify your own text?
(The motivation is that I'm trying to persuade my colleagues to document their m-files more thoroughly, and having default text for them to fill in might encourage them.)
I didn't even know File->New->Function did that.
The way I solved the issue was to write a function that you call via
>>newFunction myNewFunctionName
It then
pops up an inputdlg window, which asks the user for the synopsis and the H1 line and allows to already write help to explain input and output arguments. There, the user also selects whether myNewFunctionName is a function or a class in order to choose the right header and 'function call'
checks whether a function of the same name exists already
asks for a folder to save the function, and
opens the function in the editor
The header is set up so that it's easy to fill in info about input and output. It also automatically lists the username of the person who created the file as well as the date and the Matlab version.
EDIT
For new classes, the template function automatically makes sure that they subclass my general superclass that implements methods such as 'help' (which calls doc(class(obj)) )
Now if the template functionwould also write the algorithm part of the function, it would be really convenient. :)
EDIT2
Here's a link to the function on the file exchange.
I would suggest making your own default m-file template, called default.m for example, and placing it in a folder on the MATLAB path where your colleagues can access it. You should then set the file to be read-only. Your colleagues can then execute any one of the following commands in the MATLAB Command Window when they want to create a new function m-file:
open default.m
open('default.m')
edit default.m
edit('default.m')
The functions OPEN and EDIT will open a file in the MATLAB Editor. Since the file default.m is read-only, if anyone tries to save over it they will get a dialog box warning them as such and asking them to save to a new file (or overwrite it). That should keep them from accidentally modifying the template.
I searched through all text files starting from matlabroot folder, but could not find that template. Seems it's hard-coded, which is weird.
I like Jonas approach. As my two cents, you can download a function (not mine) doing similar things with some customization from here.
After more pondering, I've come up with a solution that I'm happy with, combining Jonas' and gnovice's answers. It's a function that creates a new m-file (with template documentation), and opens it in the editor. It is available from the Matlab Central File Exchange.