| Date: | Tue, 12 Apr 2005 12:00:30 -0500 |
| Reply-To: | Gregg Snell <gsnell@DATASAVANTCONSULTING.COM> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Gregg Snell <gsnell@DATASAVANTCONSULTING.COM> |
| Subject: | Re: lag & lead |
| In-Reply-To: | <403593359CA56C4CAE1F8F4F00DCFE7D1A7E74@MAILBE2.westat.com> |
| Content-Type: | text/plain; charset=ISO-8859-1; format=flowed |
This seams to work. Notice that lag2/lag3 are pseudo lags by virtue of
calculation after the output statement. You will probably want to drop
unwanted variables.
Regards,
Gregg (wishing I were at SUGI) Snell
data test;
input ID YEAR X1 X2;
datalines;
1 1990 0.3 10
1 1991 0.45 15
1 1992 0.4 14.5
1 1993 0.25 15
1 1994 0.5 20
3 1999 1.1 50
3 2000 1.3 100
3 2001 2.45 150
3 2002 3.4 145
3 2003 2.5 75
3 2004 4.5 90
;
run;
data test2;
set test;
by id;
lag1x1=lag1(x1);
retain lag2x1 lag3x1;
if first.id then do;
lag1x1=.;
lag2x1=.;
lag3x1=.;
end;
lead1=_n_+1;
lead2=_n_+2;
lead3=_n_+3;
if lead1 le maxobs then set test(keep=id x1 rename=(id=lead1id
x1=lead1x1)) nobs=maxobs point=lead1;
else lead1x1=.;
if lead1id ne id then lead1x1=.;
if lead2 le maxobs then set test(keep=id x1 rename=(id=lead2id
x1=lead2x1)) nobs=maxobs point=lead2;
else lead2x1=.;
if lead2id ne id then lead2x1=.;
if lead3 le maxobs then set test(keep=id x1 rename=(id=lead3id
x1=lead3x1)) nobs=maxobs point=lead3;
else lead3x1=.;
if lead3id ne id then lead3x1=.;
output;
lag3x1=lag2x1;
lag2x1=lag1x1;
run;
Ed Heaton wrote:
>Chang,
>
>I guess I didn't read his email closely enough.
>
>No, it doesn't work for lags within BY groups. Sorry for not following
>the specs.
>
>Ed
>
>Edward Heaton, SAS Senior Systems Analyst,
>Westat (An Employee-Owned Research Corporation),
>1600 Research Boulevard, RW-3541, Rockville, MD 20850-3195
>Voice: (301) 610-4818 Fax: (301) 610-5128
>mailto:EdHeaton@Westat.com http://www.Westat.com
>
>
>
>-----Original Message-----
>From: Chang Chung [mailto:chang_y_chung@HOTMAIL.COM]
>Sent: Tuesday, April 12, 2005 8:46 AM
>To: Ed Heaton
>Subject: Re: lag & lead
>
>
>On Tue, 12 Apr 2005 08:12:46 -0400, Ed Heaton <EdHeaton@WESTAT.COM>
>wrote:
>
>Hi, Ed,
>
>Does this work for lagging or leading within a by-group? -- which Thomas
>needs?
>
>Cheers,
>Chang
>
>
>
>>Data new ;
>> Set test ;
>> If ( _n_ gt 1 ) then set test(
>> keep=X1
>> rename=( X1=LAG1X1 )
>> ) ;
>> If ( _n_ gt 2 ) then set test(
>> keep=X1
>> rename=( X1=LAG2X1 )
>> ) ;
>> If ( _n_ gt 3 ) then set test(
>> keep=X1
>> rename=( X1=LAG3X1 )
>> ) ; Merge
>> test(
>> firstObs=2
>> keep=X1
>> rename=( X1=LEAD1X1 )
>> )
>> test(
>> firstObs=3
>> keep=X1
>> rename=( X1=LEAD2X1 )
>> )
>> test(
>> firstObs=4
>> keep=X1
>> rename=( X1=LEAD3X1 )
>> )
>> ;
>>Run ;
>>
>>This can be made shorter with a couple of macros.
>>
>>%macro lookAhead( var= , data=_last_ , phase=1 ) ;
>>/* This macro must be called inside a MERGE statement. */
>> &data(
>> firstObs=%eval( &phase + 1 )
>> keep=&var
>> rename=( &var=LEAD&phase&var )
>> )
>>%mEnd lookAhead ;
>>
>>%macro lookBehind( var= , data=_last_ , phase=1 ) ;
>> If ( _n_ gt &phase ) then set &data(
>> keep=&var
>> rename=( &var=LAG&phase&var )
>> ) ;
>>%mEnd lookBehind ;
>>
>>Options mergeNoBy=noWarn ;
>>Data new ;
>> Set test ;
>> %lookBehind( var=X1 , data=test , phase=1 )
>> %lookBehind( var=X1 , data=test , phase=2 )
>> %lookBehind( var=X1 , data=test , phase=3 )
>> Merge
>> %lookAhead( var=X1 , data=test , phase=1 )
>> %lookAhead( var=X1 , data=test , phase=2 )
>> %lookAhead( var=X1 , data=test , phase=3 )
>> ;
>>Run ;
>>Options mergeNoBy=noWarn ;
>>
>>Ed
>>
>>Edward Heaton, SAS Senior Systems Analyst,
>>Westat (An Employee-Owned Research Corporation),
>>1600 Research Boulevard, RW-3541, Rockville, MD 20850-3195
>>Voice: (301) 610-4818 Fax: (301) 610-5128
>>mailto:EdHeaton@Westat.com http://www.Westat.com
>>
>>
>>
>>-----Original Message-----
>>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
>>Thomas
>>Sent: Tuesday, April 12, 2005 4:04 AM
>>To: SAS-L@LISTSERV.UGA.EDU
>>Subject: lag & lead
>>
>>
>>Hi all,
>>
>>I know the "lag" function is available to lag the variable for
>>N-period. But, I have a problem on how to take the "forward/leading" of
>>
>>
>
>
>
>>the variable using SAS. I hereby illustrate my problem with the
>>follwoing example. I would very much appreciate it if you could kindly
>>advise me how to achieve it (A generic case).
>>
>>Thank you very much!
>>Thomas
>>
>>*Input;
>>ID YEAR X1 X2
>>1 1990 0.3 10
>>1 1991 0.45 15
>>1 1992 0.4 14.5
>>1 1993 0.25 15
>>1 1994 0.5 20
>>3 1999 1.1 50
>>3 2000 1.3 100
>>3 2001 2.45 150
>>3 2002 3.4 145
>>3 2003 2.5 75
>>3 2004 4.5 90
>>
>>*Expected output (lag & forward of X1);
>>ID YEAR X1 X2 LAG1X1 LAG2X1 LAG3X1 LEAD1X1 LEAD2X1
>>LEAD3X1
>>1 1990 0.3 10 . . . 0.45 0.4 0.25
>>1 1991 0.45 15 0.3 . . 0.4 0.25 0.5
>>1 1992 0.4 14.5 0.45 0.3 . 0.25 0.5 .
>>1 1993 0.25 15 0.4 0.45 0.3 0.5 . .
>>1 1994 0.5 20 0.25 0.4 0.45 . . .
>>3 1999 1.1 50 . . . 1.3 2.45 3.4
>>3 2000 1.3 100 1.1 . . 2.45 3.4 2.5
>>3 2001 2.45 150 1.3 1.1 . 3.4 2.5 4.5
>>3 2002 3.4 145 2.45 1.3 1.1 2.5 4.5 .
>>3 2003 2.5 75 3.4 2.45 1.3 4.5 . .
>>3 2004 4.5 90 2.5 3.4 2.45 . . .
>>
>>/*The output only show for the "lag and lead" of one variable. may I
>>know how to generalize to N variable case and the N "lag and lead"?*/
>>
>>
>
>
>
>
|