BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi.
I was wondering if anyone could help me assign a variable value to the LAG function. In other words, my code generates values for n and P and then I need to to get the LAG n (P).

As an example, I have :
n P
. 5
. 7
. 6
4 7

I want to use the value of n=4 to ask SAS to get the P that is 4 observations behind.
Thanks,
Art
4 REPLIES 4
LinusH
Tourmaline | Level 20
You mean to build a dynamically lag? As far as I know that couldn't be done using the lag function. Another possibility is to you the absolute with itself using absolute and relative row numbers in the join expression:

data obsNum;
set YourData;
obsNum = _n_;
run;

proc sql;
create table output as
select n, P, Pn
from YourData as a
left join YourData as b
on a.obsNum - n = b.obsNum
;
quit;

/Linus
Data never sleeps
Cynthia_sas
SAS Super FREQ
Hi:
The documentation on the LAG function explains that
"The LAG functions, LAG1, LAG2, ..., LAG100 return values from a queue. LAG1 can also be written as LAG. A LAGn function stores a value in a queue and returns a value stored previously in that queue. Each occurrence of a LAGn function in a program generates its own queue of values.

The queue for LAGn is initialized with n missing values, where n is the length of the queue (for example, a LAG2 queue is initialized with two missing values). When LAGn is executed, the value at the top of the queue is removed and returned, the remaining values are shifted upwards, and the new value of the argument is placed at the bottom of the queue. Hence, missing values are returned for the first n executions of LAGn, after which the lagged values of the argument begin to appear."


There can be issues of the wrong value being returned if you use conditional logic to invoke the LAG function, as illustrated in the example on returning every other value (that is shown in the doc).

In this instance experimenting with your actual data may be necessary. For example, in the data you show in your posting, when N=4, it is on the 4th row of data, so there are only 3 rows 'before' this row, therefore, there would be nothing (missing) for the lag4 function to return. In your example, on the N=4 row, the returned values for these invocations of the LAGn functions would be:
[pre]
lag1 function would return 6
lag2 function would return 7
lag3 function would return 5
lag4 function would return .
[/pre]

cynthia

[pre]
data n_p_data2;
infile datalines;
input n p;
return;
datalines;
. 5
. 7
. 6
4 7
;
run;

data uselag2;
set n_p_data2;
lval1 = lag(p);
lval2 = lag2(p);
lval3 = lag3(p);
lval4 = lag4(p);
if n = 1 then lval = lval1;
else if n = 2 then lval = lval2;
else if n = 3 then lval = lval3;
else if n = 4 then lval = lval4;
else if n = . then lval = .;
run;

proc print data=uselag2;
var n p lval lval1-lval4;
run;
[/pre]
deleted_user
Not applicable
Linus and Cynthia, thanks very much for your suggestions. I will try these out.

Best regards,
Art
deleted_user
Not applicable
i think you can use the code as follows
data new;
set yourdata point=j;
i=j-n;
set yourdata point=i;
np=p;output;
j=j+1;
run;
this is my idea to move the point by yourself, I haven't check the code, it may require some revise.

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!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 4 replies
  • 1677 views
  • 0 likes
  • 3 in conversation