BookmarkSubscribeRSS Feed
brophymj
Quartz | Level 8

I'm using code the uses the lag function but for some reason the lag1 function is calling values from two periods before. I've attached screenshot where you can see the issue. I'm not sure what's causing this but my whole code relies a lot on lags so hopefull this is something that can be resolved. 

 

data check1;
retain vol_x vol_x1 vol_x2;
set check;

vol_x1=lag1(vol_x);
vol_x2=lag2(vol_x);

if Price_USD gt 0 then vol_24hr_units = Vol_24hr/Price_USD;
vol_1hr_units = vol_24hr_units/24 + (vol_24hr_units-lag1(vol_24hr_units))/2;
vol_x = (vol_1hr_units + lag1(vol_1hr_units) + lag2(vol_1hr_units) + lag3(vol_1hr_units) + lag4(vol_1hr_units))/5;
price_x = (Price_USD + lag1(Price_USD) + lag2(Price_USD) + lag3(Price_USD) + lag4(Price_USD))/5;

run;


Example.GIF
6 REPLIES 6
art297
Opal | Level 21

Using the lag function gets (methinks) complicated with computed values. I think you actually want to keep the last five values in a FIFO array and simply retain them. I or someone else can provide a better suggestion if you provide an example dataset in the form of a datastep.

 

Art, CEO, AnalystFinder.com

 

brophymj
Quartz | Level 8

Hi Art 

 

Thanks for your reply. I've been through the code many times and it just doesn't make send why it's not working. I'll post an example. There's a useful macro on the forum where you can create the sas code for your dataset. Do you have the code to create it?

 

Thanks

 

Astounding
PROC Star

This is happening because you apply the LAG function to VOL_X before actually computing VOL_X.  One easy fix would be to replace the creation of VOL_X1 and VOL_X2 with these statements:

 

VOL_X2 = VOL_X1;

VOL_X1 = VOL_X;

 

Since all 3 variables are being retained, that's all that would be needed.  Just place these statements at the same location where VOL_X1 and VOL_X2 are now being computed.

Tom
Super User Tom
Super User

You are storing the result of LAG1() and LAG2() of VOL_X at the top of your data step and then later on you are changing the value of VOL_X.  SInce the LAG() function just returns the values that it stores it EXECUTES this will have an impact on what values it returns.

 

So your code does these step on each pass through the data step :

  vol_x = some value (either missing or retained from previous step);

  vol_x1=lag(vol_x);

  vol_x = some new value

 

So values resulting value of VOL_X1 will be in order

_n_=1 - Missing since there is no lagged value to return on the frist obsevation.

_n_=2 - Missing (since that is the first value of VOL_X that you saved)

_n_=3 - some new value that you calculated on the first pass through the data step and retained over to the second pass.

ballardw
Super User

Can you show what you expect the value to be?

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 927 views
  • 0 likes
  • 5 in conversation