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;
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;
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:
data want;
merge data_in data_est;
by group;
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;
run;
It's important for LAG to execute on every observation, or else it won't return the proper value.
Their calculating an average here, but the idea for a minimum is similar.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.