Macro loop problem

Hey all: what I want to do is to (loop over) trigger the estimation command for a model where in each iteration I change a model equation (i.e. I have different cases of a model and I want to estimate them right after each other). I created seperate files, 1 for each case labelled “1.mod”, “2.mod” …, that only contain the one model specific equation.

Here is what I tried in my main file:

@#define cases =  "1", "2",...]

VARIABLES BLOCK
PARAMETERS BLOCK

model(linear);
EQUATIONS THAT ARE THE SAME FOR ALL CASES
**@#include "{case}.mod" ** // to include the case specific equations
end;

VAROBS

ESTIMATED_PARAMS BLOCK

ESTIMATED_PARAMS_INIT BLOCK

@#for case in cases 
ESTIMATION CMD
@#endfor

it tells me:

ERROR in macro-processor: mainfile.mod:36.1: Could not open {case}.mod

the same happens if I try to loop over the whole code and not only the estimation cmd.

is this simply a syntax mistake or is my thinking wrong here?

I am not sure what you are trying to achieve, but note that you are not looping over different models here, i.e. different runs of the preprocessor. Rather you loop within one model. This won’t work.
For me it seems more promising to loop over calls to Dynare from Matlab, where the mod-file includes on include directive and you use matlab to change that included file before each call to Dynare.

thank you so much for the fast answer!

I am not so sure I understood how this would look thought, could you post a small rough example in pseudo-code?

What I want to achieve is: to estimate the model many times after each other, where in each iteration I slightly change one equation in the model block (different case of the model), with just one command.
(I simply want to have one main file because I find it more appealing than copy+pasting the mod file 10 times and then running each mod-case on its own, does that make sense?)

I was thinking about including one file in the mod-file and then wrapping it with something like:

equations={'k=0.9*k(-1)+invest;'
'k=0.8*k(-1)+invest;'};

for ii=1:size(equations,1)
%write equation to file
fid=fopen('my_equation.mod','w');
fprintf(fid,'%s \n',equations{ii,1});
fclose(fid);
%run file
dynare my_modfile noclearall
%save results
save('results_',num2str(ii)])
end
2 Likes