diff --git a/matlab/+pac/+estimate/iterative_ols.m b/matlab/+pac/+estimate/iterative_ols.m
index 0269a91e528e71cb10c9e14d56721fede4ebe11a..9836f4aa7aa37e236a1b88a7a8866ea58d64a53f 100644
--- a/matlab/+pac/+estimate/iterative_ols.m
+++ b/matlab/+pac/+estimate/iterative_ols.m
@@ -83,9 +83,18 @@ if ~isempty(M_.pac.(pacmodl).h1_param_indices)
     end
 end
 
+% Reorder ec.vars locally if necessary. Second variable must be the
+% endogenous variable, while the first must be the associated trend.
+if M_.pac.(pacmodl).ec.isendo(2)
+    ecvars = M_.pac.(pacmodl).ec.vars;
+else
+    ecvars = flip(M_.pac.(pacmodl).ec.vars);
+end
+
 % Build matrix for EC and AR terms.
 DataForOLS = dseries();
-DataForOLS{'ec-term'} = data{M_.endo_names{M_.pac.(pacmodl).ec.vars(2)}}.lag(1)-data{M_.endo_names{M_.pac.(pacmodl).ec.vars(1)}}.lag(1);
+% Error correction term is trend minus the level of the endogenous variable.
+DataForOLS{'ec-term'} = data{M_.endo_names{ecvars(1)}}.lag(1)-data{M_.endo_names{ecvars(2)}}.lag(1);
 listofvariables3 = {'ec-term'};
 xparm = { M_.param_names(M_.pac.(pacmodl).ec.params(1))};
 for i = 1:length(M_.pac.(pacmodl).ar.params)
diff --git a/matlab/+pac/+estimate/nls.m b/matlab/+pac/+estimate/nls.m
index bf5dc8bd22a271a4454547b0004bde040be73d26..e11c83c3eeb25ec101e8000024f04aee487d08f7 100644
--- a/matlab/+pac/+estimate/nls.m
+++ b/matlab/+pac/+estimate/nls.m
@@ -44,6 +44,13 @@ global M_ oo_
 [pacmodl, lhs, rhs, pnames, enames, xnames, pid, eid, xid, ~, ipnames_, params, data, islaggedvariables] = ...
     pac.estimate.init(M_, oo_, eqname, params, data, range);
 
+% Check that the error correction term is correct.
+if M_.pac.(pacmodl).ec.isendo(1)
+    error(['\nThe error correction term in PAC equation (%s) is not correct.\nThe ' ...
+           'error correction term should be the difference between a trend\n' ...
+           'and the level of the endogenous variable.'], pacmodl);
+end
+
 % List of objects to be replaced
 objNames = [pnames; enames; xnames];
 objIndex = [pid; eid; xid];
diff --git a/matlab/+pac/+update/parameters.m b/matlab/+pac/+update/parameters.m
index d0b4faf16940ef02eed1db810399bbc2812085af..dfefb8c81fd68a8c7130cf3c2fb9f36fee923cde 100644
--- a/matlab/+pac/+update/parameters.m
+++ b/matlab/+pac/+update/parameters.m
@@ -65,7 +65,7 @@ end
 pacvalues = DynareModel.params([pacmodel.ec.params; pacmodel.ar.params(1:pacmodel.max_lag)']);
 
 % Get the indices for the stationary/nonstationary variables in the VAR system.
-id = find(strcmp(DynareModel.endo_names{pacmodel.ec.vars(2)}, varmodel.list_of_variables_in_companion_var));
+id = find(strcmp(DynareModel.endo_names{pacmodel.ec.vars(find(~pacmodel.ec.isendo))}, varmodel.list_of_variables_in_companion_var));
 
 if isempty(id)
     % Find the auxiliary variables if any
@@ -75,14 +75,11 @@ if isempty(id)
     else
         for i=1:length(ad)
             auxinfo = DynareModel.aux_vars(get_aux_variable_id(varmodel.list_of_variables_in_companion_var{ad(i)}));
-            if isequal(auxinfo.endo_index, pacmodel.ec.vars(2))
-                % /!\ First element of ec.vars is the level of the endogenous
-                % variable in the PAC equation, the second element
-                % is the target variable.
+            if isequal(auxinfo.endo_index, pacmodel.ec.vars(find(~pacmodel.ec.isendo)))
                 id = ad(i);
                 break
             end
-            if isequal(auxinfo.type, 8) && isequal(auxinfo.orig_index, pacmodel.ec.vars(2))
+            if isequal(auxinfo.type, 8) && isequal(auxinfo.orig_index, pacmodel.ec.vars(find(~pacmodel.ec.isendo)))
                 id = ad(i);
                 break
             end
diff --git a/preprocessor b/preprocessor
index 27c9a9a5e03e05396b3df1a69351e44b11e5993b..e19a14869eae4f2ed1571446c543ee39726421d8 160000
--- a/preprocessor
+++ b/preprocessor
@@ -1 +1 @@
-Subproject commit 27c9a9a5e03e05396b3df1a69351e44b11e5993b
+Subproject commit e19a14869eae4f2ed1571446c543ee39726421d8