turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- Health Care and Pharma
- /
- SAS in Health Care Related Fields
- /
- Using LAG with a variable lag

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-29-2008 10:14 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-29-2008 11:01 AM

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 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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-29-2008 11:16 AM

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]

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]

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-29-2008 12:43 PM

Linus and Cynthia, thanks very much for your suggestions. I will try these out.

Best regards,

Art

Best regards,

Art

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-30-2008 12:11 AM

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.

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.