Date: Thu, 24 Apr 2003 08:46:52 -0400
Reply-To: Howard_Schreier@ITA.DOC.GOV
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Howard_Schreier@ITA.DOC.GOV
Subject: Re: opposite lag function
To explain *why* there is no opposite function, I prefer to use an analogy:
I can go to the public library near my home and look at newspapers from
yesterday, from last week, etc. But they never seem to have the papers from
tomorrow or from next week.
What Ping wants is to implement the opposite of a common *usage* of the LAG
function: look-back.
Several techniques have been suggested for performing look-ahead. Some of
them are rather limited in that they don't work with BY groups. Actually,
the LAGn functions are pretty clumsy to use with BY groups, especially when
n>1.
SQL is a good alternative. It's symmetric in that look-back and look-ahead
are done the same way.
Because SQL does not recognize any order in the rows (observations) being
processed, it is necessary to have an explicit ordinal variable. Often such
a variable is present in the dataset. If not, it can be generated via the
MONOTONIC function.
Example:
data have;
do group = 'a','b';
do value = 1,3,3,2,5,1,8; output; end;
end;
run;
proc sql;
create view numbered as
select *, monotonic() as obsnum
from have;
create view want as
select numbered.group, numbered.value, cross.value as lookahead
from numbered left join numbered as cross
on numbered.group=cross.group and numbered.obsnum+1=cross.obsnum
order by numbered.obsnum;
quit;
Result:
Obs group value lookahead
1 a 1 3
2 a 3 3
3 a 3 2
4 a 2 5
5 a 5 1
6 a 1 8
7 a 8 .
8 b 1 3
9 b 3 3
10 b 3 2
11 b 2 5
12 b 5 1
13 b 1 8
14 b 8 .
On Wed, 23 Apr 2003 10:58:47 -0400, yu_ping <yu_ping@RESEARCH.CIRC.GWU.EDU>
wrote:
>Could any one tell me how to move all of the observations for a variable
one
>line up. SAS has lag function, but I want the opposite function of lag. Is
>there anyone know how move the observations up.
>
>
>Thanks a million.
>
>Ping