diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh
index 31ee8c017b2bb37685724aa4936242e72dc7b16e..575c72aefa53a9616b6834297e5193d721ff6491 100644
--- a/src/DynamicModel.hh
+++ b/src/DynamicModel.hh
@@ -486,6 +486,16 @@ public:
   //! Transforms the model by removing trends specified by the user
   void detrendEquations();
 
+  inline const nonstationary_symbols_map_t & getNonstationarySymbolsMap() const
+  {
+    return nonstationary_symbols_map;
+  }
+
+  inline const map<int, expr_t> & getTrendSymbolsMap() const
+  {
+    return trend_symbols_map;
+  }
+
   //! Substitutes adl operator
   void substituteAdl();
 
diff --git a/src/ModFile.cc b/src/ModFile.cc
index 837de22f1706210c191aa8561f32e47c53bb5b69..50a560a38f1a3dc12cc1c4f39bb873a52a7ff46f 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -514,6 +514,9 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, const
       dynamic_model.detrendEquations();
       trend_dynamic_model = dynamic_model;
       dynamic_model.removeTrendVariableFromEquations();
+      const auto & trend_symbols = dynamic_model.getTrendSymbolsMap();
+      const auto & nonstationary_symbols = dynamic_model.getNonstationarySymbolsMap();
+      epilogue.detrend(trend_symbols, nonstationary_symbols);
     }
 
   mod_file_struct.orig_eq_nbr = dynamic_model.equation_number();
diff --git a/src/ModelEquationBlock.cc b/src/ModelEquationBlock.cc
index cd2e70315098e4114cbabf667d7be9e07e5fa2d0..a4eaf92b691c80bfe640b70770b43f11e501e438 100644
--- a/src/ModelEquationBlock.cc
+++ b/src/ModelEquationBlock.cc
@@ -335,6 +335,31 @@ Epilogue::checkPass(WarningConsolidation &warnings) const
       so_far_defined.push_back(it.first);
 }
 
+void
+Epilogue::detrend(const map<int, expr_t> & trend_symbols_map,
+                  const nonstationary_symbols_map_t & nonstationary_symbols_map)
+{
+  for (auto it = nonstationary_symbols_map.crbegin();
+       it != nonstationary_symbols_map.crend(); it++)
+    for (auto & [symb_id, expr] : def_table)
+      {
+        expr = expr->detrend(it->first, it->second.first, it->second.second);
+        assert(expr != nullptr);
+      }
+
+  for (auto & [symb_id, expr] : def_table)
+    {
+      expr = expr->removeTrendLeadLag(trend_symbols_map);
+      assert(expr != nullptr);
+    }
+
+  for (auto & [symb_id, expr] : def_table)
+    {
+      expr = expr->replaceTrendVar();
+      assert(expr != nullptr);
+    }
+}
+
 void
 Epilogue::writeEpilogueFile(const string &basename) const
 {
diff --git a/src/ModelEquationBlock.hh b/src/ModelEquationBlock.hh
index f01605eb8bc8f801d722c91100d6b2cc3abcb13f..9feb71afc0e3378852fbb45c5e08e6e34beb2e67 100644
--- a/src/ModelEquationBlock.hh
+++ b/src/ModelEquationBlock.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2010-2018 Dynare Team
+ * Copyright © 2010-2019 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -89,6 +89,10 @@ public:
   //! Checks that no variable is declared twice
   void checkPass(WarningConsolidation &warnings) const;
 
+  //! Deal with trend variables in the epilogue block
+  void detrend(const map<int, expr_t> & trend_symbols_map,
+               const nonstationary_symbols_map_t & nonstationary_symbols_map);
+
   //! Write the steady state file
   void writeEpilogueFile(const string &basename) const;
 };