```Date: Sun, 20 May 2007 09:27:57 -0400 Reply-To: John James Sender: "SAS(r) Discussion" From: John James Subject: Re: Lag function 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 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/ ```

Back to: Top of message | Previous page | Main SAS-L page