Not applicable
Posts: 0

# Lagged variables problems

Hi,

I'm a beginner sas user and having trouble using lagged variables. I'm trying to create a decay curve that switches to a linear function at some point.

Code:

DATA GEVS.EPS_Decay_Curve;
power = 0.7;
tgr = 4.5;
max_growth = 12;
max_years = 50;

do X = 1 to max_years;
Y = 1/((X+1)**power);
A = (max_growth - tgr)*Y;
Increment = lag(A) - A;
SLAmt = A/(max_years-X);

if Increment
do;
if X=1 then
Switch =0;
else do;
Switch = 1;
end;
end;
else
Switch =0;

/* Calculate inc_f */
/* Store lags */
lagswitch = lag(switch);
laginc_f = lag(inc_f);
lagSLAmt = lag(SLAmt);

if switch = 0 then
inc_f = Increment;
else if switch = 1 && lagswitch = 0 then
inc_f = lagSLAmt;
else
inc_f = laginc_f;
output;
end;

drop power tgr max_growth max_years lagswitch laginc_f lagSLAmt;
run;

For some reason the inc_f is not calculating correctly. It should display 0.02967 for all values where switch=1.

Can someone help?

Jer
Not applicable
Posts: 0

## Re: Lagged variables problems

Calculate inc_f */
/* Store lags */
lagswitch = lag(switch);
laginc_f = lag(inc_f);
lagSLAmt = lag(SLAmt);

if switch = 0 then
inc_f = Increment;
else if switch = 1 && lagswitch = 0 then
inc_f = lagSLAmt;
else
inc_f = laginc_f;
output;
end;

drop power tgr max_growth max_years lagswitch laginc_f lagSLAmt;
run;

For some reason the inc_f is not calculating correctly. It should display 0.02967 for all values where switch=1.

Can someone help?

Jer
SAS Super FREQ
Posts: 9,368

## Re: Lagged variables problems

Hi:
These Tech Support notes and the documentation link illustrate the wrong and right ways to use the LAG function. LAG only puts a value in the queue when it is called. Therefore, it is better to do your lag OUTSIDE of any condition, get a value in the queue and then deal with the manipulations using the lagged value inside your conditional logic -- but don't use the LAG function itself inside the conditional logic. For more information, refer to the documentation....or if you get stuck, you might consider working with Tech Support on this issue.

cynthia

http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/etsug_tsdata_sect048.htm
http://support.sas.com/kb/24/665.html
http://support.sas.com/kb/25/938.html
http://support.sas.com/kb/24/694.html
Discussion stats
• 2 replies
• 126 views
• 0 likes
• 2 in conversation