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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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