diff --git a/src/ModFile.cc b/src/ModFile.cc
index 5780b9e6348fc7fc53ef50a18b0a73c774dbd125..293100413662ade5c168499ca2bb4e1002c9e61d 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -610,7 +610,9 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, bool
     }
   // And finally perform the substitutions
   dynamic_model.substituteVarExpectation(var_expectation_subst_table);
-  dynamic_model.createVariableMapping(original_model.equation_number());
+  dynamic_model.createVariableMapping(mod_file_struct.orig_eq_nbr +
+                                      (mod_file_struct.ramsey_model_present ?
+                                       mod_file_struct.ramsey_eq_nbr : 0));
 
   if (mod_file_struct.stoch_simul_present
       || mod_file_struct.estimation_present