BookmarkSubscribeRSS Feed
Usagi
Fluorite | Level 6

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;

 

3 REPLIES 3
Usagi
Fluorite | Level 6

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;
Astounding
PROC Star

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.

Reeza
Super User

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

 

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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