1function n = get_difference_order(var)
2
3% Returns true iff endogenous variable `var` is a variable in difference.
4%
5% INPUTS
6% - var   [string, integer]  Variable name or index in M_.endo_names.
7%
8% OUTPUTS
9% - boo   [logical]          true/false.
10
11% Copyright (C) 2018 Dynare Team
12%
13% This file is part of Dynare.
14%
15% Dynare is free software: you can redistribute it and/or modify
16% it under the terms of the GNU General Public License as published by
17% the Free Software Foundation, either version 3 of the License, or
18% (at your option) any later version.
19%
20% Dynare is distributed in the hope that it will be useful,
21% but WITHOUT ANY WARRANTY; without even the implied warranty of
22% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23% GNU General Public License for more details.
24%
25% You should have received a copy of the GNU General Public License
26% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
27
28global M_
29
30% Get index of the auxiliary
31id = get_aux_variable_id(var);
32
33% Set default difference order
34n = 0;
35
36% If var is not an auxiliary variable it cannot be a first difference.
37if ~id, return, end
38
39while id
40    if M_.aux_vars(id).type==8
41        % diff auxiliary variable.
42        n = n+1;
43        v = M_.aux_vars(id).orig_index;
44        if v<=M_.orig_endo_nbr
45            id = 0;
46        else
47            id = get_aux_variable_id(v);
48        end
49    elseif M_.aux_vars(id).type==9
50        % lagged diff auxiliary variable
51        v = M_.aux_vars(id).orig_index;
52        id = get_aux_variable_id(v);
53    else
54        break
55    end
56end