diff --git a/preprocessor/NumericalInitialization.cc b/preprocessor/NumericalInitialization.cc
index fdf1a052e34e76b12ed3b9104fff0696ed1602f3..dc6bc94136ffeb5df28d9f7394cbc5fe584ea8bf 100644
--- a/preprocessor/NumericalInitialization.cc
+++ b/preprocessor/NumericalInitialization.cc
@@ -182,16 +182,32 @@ HistValStatement::writeOutput(ostream &output, const string &basename) const
 {
   output << "%" << endl
          << "% HISTVAL instructions" << endl
-         << "%" << endl;
+         << "%" << endl
+         << "oo_.endo_simul = zeros(M_.endo_nbr,M_.maximum_lag);" << endl;
 
   for (hist_values_type::const_iterator it = hist_values.begin();
        it != hist_values.end(); it++)
     {
-      const int &symb_id = it->first.first;
-      const int &lag = it->first.second;
+      int symb_id = it->first.first;
+      int lag = it->first.second;
       const NodeID expression = it->second;
 
       SymbolType type = symbol_table.getType(symb_id);
+      if (type == eEndogenous && lag < 0)
+	{
+	  const int new_symb_id = symbol_table.searchAuxiliaryVars(symb_id,lag);
+	  if (new_symb_id != -1)
+	    {
+	      symb_id = new_symb_id;
+	      lag = 0;
+	    }
+	  else if (symbol_table.AuxVarsSize() > 0)
+	    {
+	      cerr << "Histval: this variable doesn't exist with such a lag in the model" << endl;
+	      exit(EXIT_FAILURE);
+	    }
+	    
+	}
       int tsid = symbol_table.getTypeSpecificID(symb_id) + 1;
 
       if (type == eEndogenous)
diff --git a/preprocessor/SymbolTable.cc b/preprocessor/SymbolTable.cc
index 91e1b6f4cf30c22bf31fb582f96274a61f31d076..2db56ae42d578b985dcce4c86193b35927cf147a 100644
--- a/preprocessor/SymbolTable.cc
+++ b/preprocessor/SymbolTable.cc
@@ -364,6 +364,15 @@ SymbolTable::addExpectationAuxiliaryVar(int information_set, int index, const st
   return symb_id;
 }
 
+int 
+SymbolTable::searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const
+{
+  for (int i=0; i < aux_vars.size();++i)
+    if ((aux_vars[i].get_orig_symb_id() == orig_symb_id) && (aux_vars[i].get_orig_lead_lag() == orig_lead_lag))
+      return aux_vars[i].get_symb_id();
+  return -1;
+}
+
 void
 SymbolTable::markPredetermined(int symb_id) throw (UnknownSymbolIDException, FrozenException)
 {
diff --git a/preprocessor/SymbolTable.hh b/preprocessor/SymbolTable.hh
index ecc886b21441e2e68587f9c379a3cbd330b8a442..43bdf87ff8ac2fa06b83d5348f8bd89f3f60fe40 100644
--- a/preprocessor/SymbolTable.hh
+++ b/preprocessor/SymbolTable.hh
@@ -204,6 +204,13 @@ public:
     \return the symbol ID of the new symbol
   */
   int addExpectationAuxiliaryVar(int information_set, int index, const string &information_set_name) throw (FrozenException);
+  //! Searches auxiliary variables by symbol_id and lead_lag
+  /*!
+    \return the symbol ID of the auxiliary variable and -1 if not found
+  */
+  int searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const;
+  //! Returns the number of auxiliary variables
+  int AuxVarsSize() const {return aux_vars.size();};
   //! Tests if symbol already exists
   inline bool exists(const string &name) const;
   //! Get symbol name (by ID)