Date: Sun, 20 May 2007 09:44:23 -0400
Reply-To: "data _null_;" <datanull@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "data _null_;" <datanull@GMAIL.COM>
Subject: Re: Lag function
In-Reply-To: <200705201327.l4KAkVGe024886@malibu.cc.uga.edu>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
What is the problem with the DATA step solution that Richard posted?
Looks like he covered everything.
On 5/20/07, John James <free562james@yahoo.com> wrote:
> Thanks for the reply. I would ideally like to use a data step to accomplish
> this. If anybody else has other suggestions, let me know.
>
> On Sun, 20 May 2007 08:09:29 -0400, Richard A. DeVenezia
> <rdevenezia@WILDBLUE.NET> wrote:
>
> >John James wrote:
> >> Dear SAS-L users,
> >>
> >> I use the lag function to create the second, third and fourth lag of
> >> variable X for each firm (y) in my sample across time (t). However, on
> >> certain occassions the lag variable created corresponds to a different
> >> firm. Is there a neat way to set these observations equal to missing?
> >>
> >> this is the code i am using
> >> proc sort data=a;
> >> by y t;
> >> run;
> >>
> >> data a;
> >> set a;
> >> xlag2=lag2 (x);
> >> xlag3=lag3 (x);
> >> xlag4=lag4 (x);
> >> run;
> >
> >There is probably a Stat procedure for doing such.
> >
> >Here is some data step code that uses macro to manage a lag array in a by
> >group setting.
> >
> >-----------------------------
> >data foo;
> > do group = 1 to 4;
> > do seq = 1 to 10;
> > x = group*100 + seq;
> > output;
> > end;
> > end;
> >run;
> >
> >%macro lagArray(by=,var=,first=,last=);
> > %local i;
> >
> > array &var.lag[&first:&last] &var.lag&first-&var.lag&last ;
> > %do i = &first %to &last;
> > &var.lag&i = lag&i(&var);
> > %end;
> >
> > if first.&by then &by.seq=1; else &by.seq+1;
> >
> > do _i_ = max(&by.seq,&first) to &last;
> > &var.lag[_i_] = .;
> > end;
> >
> > drop _i_ &by.seq;
> >
> >%mend;
> >
> >options mprint;
> >
> >proc format;
> > value missword .='-----' other=[z5.];
> >run;
> >
> >data scan;
> > set foo;
> > by group;
> >
> > %lagArray(by=group,var=x,first=2,last=5);
> >run;
> >
> >proc print;
> > by group;
> > id group;
> >run;
> >-----------------------------
> >
> >Richard A. DeVenezia
> >http://www.devenezia.com/
>
|