diff --git a/matlab/utilities/dseries/from.m b/matlab/utilities/dseries/from.m
index 2346108532c6c64558183954490633832f668c23..4fa830917909ade11f31fc2982f7549f54ab7c55 100644
--- a/matlab/utilities/dseries/from.m
+++ b/matlab/utilities/dseries/from.m
@@ -62,35 +62,91 @@ end
 EXPRESSION = char([varargin{5:end}]);
 
 % Get all the variables involved in the recursive expression.
-variables = regexpi(EXPRESSION, '\w*\(t\)|\w*\(t\-\d\)|\w*\(t\+\d\)','match');
+variables = unique(regexpi(EXPRESSION, '\w*\(t\)|\w*\(t\-\d\)|\w*\(t\+\d\)','match'));
 
-% Remove the time indices.
+% Build an incidence table (max lag/lead for each variable)
+%
+% Column 1: Name of the variable.
+% Column 2: Maximum lag order.
+% Column 3: Equal to 1 if the variable appears at the current period, 0 otherwise.
+% Column 4: Maximum lead order.
+leadlagtable = cell(0,4);
 for i=1:length(variables)
+    current = ~isempty(regexpi(variables{i},'\(t\)'));
+    lag = ~isempty(regexpi(variables{i},'\(t\-\d\)'));
+    lead = ~isempty(regexpi(variables{i},'\(t\+\d\)'));
     start = regexpi(variables{i},'\(t\)|\(t\-\d\)|\(t\+\d\)');
+    index = variables{i}(start:end);
     variables(i) = {variables{i}(1:start-1)};
+    if isempty(leadlagtable)
+        leadlagtable(1,1) = {variables{i}};
+        if current
+            leadlagtable(1,3) = {1};
+        else
+            leadlagtable(1,3) = {0};
+        end
+        if lag
+            tmp = regexpi(index,'\d','match');
+            leadlagtable(1,2) = {str2num(tmp{1})};
+        else
+            leadlagtable(1,2) = {0};
+        end
+        if lead
+            tmp = regexpi(index,'\d','match');
+            leadlagtable(1,4) = {str2num(tmp{1})};
+        else
+            leadlagtable(1,4) = {0};
+        end
+    else
+        linea = strmatch(variables{i},leadlagtable(:,1));
+        if isempty(linea)
+            linea = size(leadlagtable,1)+1;
+            leadlagtable(linea,1) = {variables{i}};
+            leadlagtable(linea,2) = {0};
+            leadlagtable(linea,3) = {0};
+            leadlagtable(linea,4) = {0};
+        end
+        if current
+            leadlagtable(linea,3) = {1};
+        end
+        if lag
+            tmp = regexpi(index,'\d','match');
+            leadlagtable(linea,2) = {max(str2num(tmp{1}),leadlagtable{linea,2})};
+        end
+        if lead
+            tmp = regexpi(index,'\d','match');
+            leadlagtable(linea,4) = {max(str2num(tmp{1}),leadlagtable{linea,4})};
+        end
+    end
 end
 
-% Remove duplicates.
-variables = unique(variables);
+% Set the number of variables
+number_of_variables = size(leadlagtable,1);
 
-% Test that all the involved variables are available dseries objects. Also check
-% that these time series are defined over the time range given by d1 and d2.
-for i=1:length(variables)
+% Test that all the involved variables are available dseries objects. Also check that
+% these time series are defined over the time range given by d1 and d2 (taking care of
+% the lags and leads).
+for i=1:number_of_variables
+    current_variable = leadlagtable{i,1};
     try
-        var = evalin('caller',variables{i});
+        var = evalin('caller',current_variable);
     catch
-        error(['dseries::from: Variable ' variables{i} ' is unknown!'])
+        error(['dseries::from: Variable ' current_variable ' is unknown!'])
     end
     if ~isdseries(var)
-        error(['dseries::from: Variable ' variables{i} ' is not a dseries object!'])
+        error(['dseries::from: Variable ' current_variable ' is not a dseries object!'])
     else
-        if d1<var.dates(1)
-            error(sprintf('dseries::from: First date in variable %s is posterior to %s!\n               Check the initial date of the loop.', variables{i}, char(d1)))
+        if d1<var.dates(1)+leadlagtable{i,2}
+            msg = sprintf('dseries::from: Initial date of the loop (%s) is inconsistent with %s''s range!\n',char(d1),current_variable);
+            msg = [msg, sprintf('               Initial date should be greater than or equal to %s.',char(var.dates(1)+leadlagtable{i,2}))];
+            error(msg)
         end
-        if d2>var.dates(end)
-            error(sprintf('dseries::from: Last date in variable %s is anterior to %s!\n               Check the terminal date of the loop.', variables{i}, char(d2)))
+        if d2>var.dates(end)-leadlagtable{i,4}
+            msg = sprintf('dseries::from: Last date of the loop (%s) is inconsistent with %s''s range!\n',char(d2),current_variable);
+            msg = [msg, sprintf('               Last date should be less than or equal to %s.',char(var.dates(end)-leadlagtable{i,4}))];
+            error(msg)
         end
-        eval(sprintf('%s = var;',variables{i}));
+        eval(sprintf('%s = var;',current_variable));
     end
 end