Loop over variables instead of parameters in one model

Hi all,
I want to loop over several variables in one model, but didn’t find any similar examples in the forum.What I see are mostly about looping over parameters or values of variables, in these cases the matlab command set_param_value is used.So I am wondering whether the set_param_value could be used in my situation,i.e.,looping over variables instead of parameters.The following codes are simplified examples as what I do, I don’t know whether the set_param_value command could be used or not. If not ,is there any other command to play the same role?

【simplified code】
// The following is in a m file

n_arr = [“variable_a”, “variable_b”,“variable_c”];
for ii=1:length(n_arr)
n=n_arr(ii);
save variablefile n;
dynare welfare1 noclearall;
end

// The following is in a mod file which is named as “welfare1”

load variablefile;
set_param_value(‘n’,n);

In the simplified codes above, there is an endogeneous variable n in the model ,and it is used in several equations of the model.In fact the variable n is a kind of an indicator(policy instrument), I want to compare which indicator is better, so I need to run the same model several times for each time with one indicator representing variable n.Could anyone give me a help? Many thanks in advance.

This is a case of using the macro language. Did you have a look at the bkk.mod-example?

Thank you very much for reminding me of thinking in another way,jpfeifer. By looking into the bkk.mod example once again, I suddenly think of a way to loop over variables, though I am not sure. One restriction is that I need to make welfare analysis, so that matlab file in m format is added. Anyway, I need your help and judgement whether the following codes work or not.

【modified code】
// The following is in a m file
…
n_arr = [“a”, “b”,“c”,“d”,“e”,“f”,...];
for ii=1:length(n_arr)
nn=n_arr(ii);
save variablefile nn;
dynare welfare1 noclearall;
end
…

// The following is in a mod file which is named as “welfare1”
…
load variablefile;
model;
...
n=nn;
…
end;
...
stoch_simul(order=2, irf=50);

With the modified codes above, the purpose is to run the codes "length(n_arr) " times, for each time the model is the same except for the n=nn equation. The nn variable is chosen from the n_arr array. I am not sure whether I write the codes in a feasible way. I need help once again, many many thanks.

I would do the loop at the Matlab-level. For example, take

var y, c, k, a, h, b;
varexo e, u;

parameters beta, rho, alpha_@{var_string}, delta, theta, psi, tau;

alpha_@{var_string} = 0.36;
rho   = 0.95;
tau   = 0.025;
beta  = 0.99;
delta = 0.025;
psi   = 0;
theta = 2.95;

phi   = 0.1;

model;
c*theta*h^(1+psi)=(1-alpha_@{var_string})*y;
k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1)))
    *(exp(b(+1))*alpha_@{var_string}*y(+1)+(1-delta)*k));
y = exp(a)*(k(-1)^alpha_@{var_string})*(h^(1-alpha_@{var_string}));
k = exp(b)*(y-c)+(1-delta)*k(-1);
a = rho*a(-1)+tau*b(-1) + e;
b = tau*a(-1)+rho*b(-1) + u;
end;

initval;
y = 1.08068253095672;
c = 0.80359242014163;
h = 0.29175631001732;
k = 11.08360443260358;
a = 0;
b = 0;
e = 0;
u = 0;
end;

shocks;
var e; stderr 0.009;
var u; stderr 0.009;
var e, u = phi*0.009*0.009;
end;

stoch_simul;

You can then call the mod-file with a preprocessor-switch like

dynare example1 -Dvar_string="temp"

Sorry, jpfeifer,I can’t understand the option following the dynare code. :sob:

The -D switch allows you to pass a value to the macro variable (here var_string) at the command line. This allows you to loop in Matlab, e.g.

n_arr = {'a','b','c'};
for ii=1:length(n_arr)
    eval(['dynare variablefile -Dvar_string="',n_arr{ii},'"']);    
end

Thank you very much,jpfeifer. I will try to apply the useful code.