10-31-2016 09:06 PM - edited 10-31-2016 09:14 PM
This is simple. I want to get the minimum value of a variable (Qtr) in a (hopefully built-in) function within DATA STEP.
In essence, I want to use the lag function to compute values (var_out) within each Group. (Data is sorted ascending by Group then by Qtr). Something like the following doesn't work.
DATA data_out; MERGE data_in data_est; BY Group; IF Qtr > min(Qtr)+3 THEN DO; var_out = var_in -(_A_1*lag(res) + _A_2*lag2(res) + _A_3*lag3(res)); END; RUN;
10-31-2016 09:13 PM
What I actually did was something more like this:
DATA data_out; MERGE data_in data_est; BY Group; var_out = var_in -(_A_1*lag(res) + _A_2*lag2(res) + _A_3*lag3(res)); *To set to missing the first 3 values in each Group; IF Qtr < min(Qtr)+4 THEN DO; var_out=. END; RUN;
10-31-2016 10:27 PM
I'm guessing that the value of QTR isn't a key element here. What you really need is to have at least 4 quarters of data so that the LAG functions return RES values for the same GROUP.
If that's the case, you could try:
merge data_in data_est;
if first.group then n_records=1;
else n_records + 1;
back1 = lag(res);
back2 = lag2(res);
back3 = lag3(res);
if n_records > 3 then var_out = var_in - (_A_1*back1) + _A_2*back2 + _A_3*back3;
drop back1 back2 back3 n_records;
It's important for LAG to execute on every observation, or else it won't return the proper value.