Date: Thu, 24 Apr 2003 15:15:03 -0500
Reply-To: pudding_man@lycos.com
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Puddin' Man <pudding_man@LYCOS.COM>
Organization: Lycos Mail (http://www.mail.lycos.com:80)
Subject: Re: opposite lag function
Content-Type: text/plain; charset=us-ascii
Yes, I can see that an sql approach might have certain
advantages for this. Howard's code works well, and his use
of monotonic is straightforward.
If a "look-back" is a lag, a "look-ahead" can be considered
as a lead (pronounced 'leed').
It occured to me that a SET .. POINT= approach might also
have certain advantages. Such code might look something
like (for a 'lead' of 2 obs):
%let lead=2;
data xx(drop=ids);
do id=1 to 2;
do ids=1 to 4;
x=5*uniform(8);
output;
end;
end;
run;
data yy(drop=l&lead._id f);
retain pt &lead;
set xx nobs=nn end=e1; by id; pt+1;
f=((_n_+&lead)<=nn);
if f then
set xx(keep=id x rename=(x=l&lead._x id=l&lead._id)) point=pt;
if not(f) or not(id=l&lead._id) then l&lead._x=.;
run;
proc print; run;
This, of course, assumes the data is pre-sorted by ID.
So long as the desired 'lead' is small (i.e. 1, 2, 6, 12), the
buffer-switching for POINT= should be kept to a minimum.
If the dataset fits in memory, SASFILE could be employed.
It appears this approach has a material performance
advantage, probably owing largely to the ORDER BY
clause in the sql.
Skoal,
Puddin'
***********************************************************
*** Puddin' Man *** Pudding_Man@lycos.com ********
***********************************************************;
"Bessie Smith was murdered by a conspiracy between
Jim Crow and John Barleycorn."
- generally attributed to Tenessee Williams
On Thu, 24 Apr 2003 08:46:52
Howard_Schreier wrote:
>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
>
____________________________________________________________
Get advanced SPAM filtering on Webmail or POP Mail ... Get Lycos Mail!
http://login.mail.lycos.com/r/referral?aid=27005
|