## Computing minimum value of a variable in DATA STEP

Occasional Contributor
Posts: 6

# Computing minimum value of a variable in DATA STEP

[ Edited ]

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;``````

Occasional Contributor
Posts: 6

## Re: Computing minimum value of a variable in DATA STEP

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;``````
Super User
Posts: 6,754

## Re: Computing minimum value of a variable in DATA STEP

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.

Super User
Posts: 23,683

## Re: Computing minimum value of a variable in DATA STEP

Their calculating an average here, but the idea for a minimum is similar.

http://support.sas.com/kb/25/027.html

Discussion stats
• 3 replies
• 600 views
• 0 likes
• 3 in conversation