diff --git a/doc/dynare.texi b/doc/dynare.texi
index 8d5a09c2a10d1ca852ee9feb0658a99413e3d384..39178a0efdaee64bec820aa439ece62408333182 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -4671,6 +4671,11 @@ a @uref{http://www.java.com/download,Java Runtime Environment}). Note that the
 base name (ie without extension) of the datafile has to be different from the
 base name of the model file.
 
+@item dirname = @var{FILENAME}
+Directory in which to store @code{estimation} output. To pass a
+subdirectory of a directory, you must quote the argument. Default:
+@code{<mod_file>}
+
 @item xls_sheet = @var{NAME}
 @anchor{xls_sheet}
 The name of the sheet with the data in an Excel file
diff --git a/matlab/dynare_estimation.m b/matlab/dynare_estimation.m
index 80e91c0417285d1919d1064d291f34d72bdde000..7bb49f38038222a859a2cecf17015140a96e4d82 100644
--- a/matlab/dynare_estimation.m
+++ b/matlab/dynare_estimation.m
@@ -56,7 +56,7 @@ nnobs = length(nobs);
 horizon = options_.forecast;
 
 if nargin<2 || ~exist(dname) || isempty(dname)
-    dname = M_.fname;
+    dname = options_.dirname;
 end
 
 M_.dname = dname;
diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m
index 4ec37b459cf3c9a6bbcb4ab0f66aecc9eab9c497..230661dc1c463f8b8db49e8e016b5788eb7330a3 100644
--- a/matlab/global_initialization.m
+++ b/matlab/global_initialization.m
@@ -33,6 +33,7 @@ global oo_ M_ options_ estim_params_ bayestopt_ estimation_info ex0_ ys0_
 estim_params_ = [];
 bayestopt_ = [];
 options_.datafile = '';
+options_.dirname = M_.fname;
 options_.dataset = [];
 options_.verbosity = 1;
 options_.terminal_condition = 0;
diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy
index 6c76d416172598ccafa81322654b5cbff7d86c3d..b7288da93a0dd3c7c8a1b787104e1f14b271b1fa 100644
--- a/preprocessor/DynareBison.yy
+++ b/preprocessor/DynareBison.yy
@@ -92,7 +92,7 @@ class ParsingDriver;
 %token CONSIDER_ALL_ENDOGENOUS CONSIDER_ONLY_OBSERVED
 %token DATAFILE FILE SERIES DOUBLING DR_CYCLE_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_MAXITER DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION DIFFERENTIATE_FORWARD_VARS
 %token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT EXTENDED_PATH ENDOGENOUS_PRIOR
-%token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS LAST_OBS SET_TIME
+%token FILENAME DIRNAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS LAST_OBS SET_TIME
 %token <string_val> FLOAT_NUMBER DATES
 %token DEFAULT FIXED_POINT OPT_ALGO
 %token FORECAST K_ORDER_SOLVER INSTRUMENTS SHIFT MEAN STDEV VARIANCE MODE INTERVAL SHAPE DOMAINN
@@ -1712,6 +1712,7 @@ estimation_options : o_datafile
 		   | o_filter_algorithm
 		   | o_proposal_approximation
 		   | o_distribution_approximation
+                   | o_dirname
                    ;
 
 list_optim_option : QUOTED_STRING COMMA QUOTED_STRING
@@ -2546,6 +2547,7 @@ o_qz_zero_threshold : QZ_ZERO_THRESHOLD EQUAL non_negative_number { driver.optio
 o_file : FILE EQUAL filename { driver.option_str("file", $3); };
 o_series : SERIES EQUAL symbol { driver.option_str("series", $3); };
 o_datafile : DATAFILE EQUAL filename { driver.option_str("datafile", $3); };
+o_dirname : DIRNAME EQUAL filename { driver.option_str("dirname", $3); };
 o_nobs : NOBS EQUAL vec_int
          { driver.option_vec_int("nobs", $3); }
        | NOBS EQUAL vec_int_number
diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll
index 0e7a31c1629445a7c0fe2ff644d0b3a5d3264321..4428dd37d25762c08236ffdcc007e29dec646c28 100644
--- a/preprocessor/DynareFlex.ll
+++ b/preprocessor/DynareFlex.ll
@@ -236,6 +236,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2
 <DYNARE_STATEMENT>dates  {dates_parens_nb=0; BEGIN DATES_STATEMENT; yylval->string_val = new string("dates");}
 <DYNARE_STATEMENT>file                  {return token::FILE;}
 <DYNARE_STATEMENT>datafile 		{return token::DATAFILE;}
+<DYNARE_STATEMENT>dirname       {return token::DIRNAME;}
 <DYNARE_STATEMENT>nobs 			{return token::NOBS;}
 <DYNARE_STATEMENT>last_obs 		{return token::LAST_OBS;}
 <DYNARE_STATEMENT>first_obs 		{return token::FIRST_OBS;}
diff --git a/tests/pi2004/ireland.mod b/tests/pi2004/ireland.mod
index ea17baad31120e471c685df0877ed85b8fd840f4..4c6552bdecedd88b916dd13cb9efca22043e1293 100644
--- a/tests/pi2004/ireland.mod
+++ b/tests/pi2004/ireland.mod
@@ -87,4 +87,4 @@ oy (log(eta));
 oc (log(eta));
 end;
 
-estimation(datafile=idata,mode_compute=1,nograph);
\ No newline at end of file
+estimation(datafile=idata,mode_compute=1,nograph,dirname='MYDIR/mysubdir');
\ No newline at end of file