diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index 0369f97622e170629040cb3fb666bd92884e8e6b..f5b67915ab8e6c8cc82b244693a9078a2646748b 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -2681,30 +2681,35 @@ DynamicModel::substituteLeadLagInternal(aux_var_t type)
   ExprNode::subst_table_t subst_table;
   vector<BinaryOpNode *> neweqs;
 
-  // Substitute in model local variables
-  for (map<int, NodeID>::iterator it = local_variables_table.begin();
-       it != local_variables_table.end(); it++)
+  // Substitute in used model local variables
+  set<int> used_local_vars;
+  for (size_t i = 0; i < equations.size(); i++)
+    equations[i]->collectModelLocalVariables(used_local_vars);
+
+  for (set<int>::const_iterator it = used_local_vars.begin();
+       it != used_local_vars.end(); ++it)
     {
+      const NodeID value = local_variables_table.find(*it)->second;
       NodeID subst;
       switch (type)
         {
         case avEndoLead:
-          subst = it->second->substituteEndoLeadGreaterThanTwo(subst_table, neweqs);
+          subst = value->substituteEndoLeadGreaterThanTwo(subst_table, neweqs);
           break;
         case avEndoLag:
-          subst = it->second->substituteEndoLagGreaterThanTwo(subst_table, neweqs);
+          subst = value->substituteEndoLagGreaterThanTwo(subst_table, neweqs);
           break;
         case avExoLead:
-          subst = it->second->substituteExoLead(subst_table, neweqs);
+          subst = value->substituteExoLead(subst_table, neweqs);
           break;
         case avExoLag:
-          subst = it->second->substituteExoLag(subst_table, neweqs);
+          subst = value->substituteExoLag(subst_table, neweqs);
           break;
         default:
           cerr << "DynamicModel::substituteLeadLagInternal: impossible case" << endl;
           exit(EXIT_FAILURE);
         }
-      it->second = subst;
+      local_variables_table[*it] = subst;
     }
 
   // Substitute in equations