Date: Tue, 31 Aug 2004 17:48:05 -0700
Reply-To: Dale McLerran <stringplayer_2@YAHOO.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Dale McLerran <stringplayer_2@YAHOO.COM>
Subject: Re: lead function
Content-Type: text/plain; charset=us-ascii
Actually, there is a better way to implement a lead function.
Given that your data are already in time ascending order, you
mydata(firstobs=2 keep=var rename=(var=lead_var));
Note that we do not have any by statement here for our merge.
The operation of this is really straightforward, once you have
looked at it for just a moment. The second observation (with
just the variable that would be named in a lead function) is
merged with the first observation. The third observation is
merged with the second observation, etc.
Now, if you need to perform the operation with some BY group
processing, then SAS views can assist.
data view1 / view=view1;
if first.BYVAR then __n=0;
data view2 / view=view2;
if first.BYVAR then __n=0;
view2(keep=BYVAR var rename=(var=lead_var));
by BYVAR __n;
VIEW2 removes the first observation from each BY group. Both
views add a variable __n that is the ordered observation within
a BY group. On VIEW1, the counting starts with the first
observation within each BY group. On VIEW2, the counting starts
with the second observation within a BY group. When merged by
the BY variable and variable __n, then the first observation
in a BY group is paired with the second observation, the second
is paired with the third observation, etc. Again, rename and
keep options are employed to construct the lead function value
and to prevent overwriting of variables on VIEW1.
--- Nathan Fong <nmfong@HOTMAIL.COM> wrote:
> I have always taken care of this by either:
> 1) sorting by descending time period, then using lag function
> 2) creating a copy of the dataset with decremented time periods and
> renamed variables, then merging back in
> I'd be interested in hearing if there's a better way, but I suspect
> there isn't, due to the way the data step works.
> On Tue, 31 Aug 2004 19:21:36 -0400, Ramu Thiagarajan
> <email@example.com> wrote:
> > Sas-Lers:
> > Is there a Lead function (analagous to Lag function) in SAS? I
> have always
> > had to work with the time period to pull the leading observation.
> > I use SAS version 8.2. The data is set in a panel data format with
> > observations for each company and several hundred companies for
> each year.
> > thanks
> > ramu
> > -----------------------------
> > This email (and attachments, if any) is confidential and access by
> > other than the addressee(s) is unauthorized. The security of email
> > communication cannot be guaranteed and neither Pequot Capital
> > Inc. nor any of its affiliates accepts liability for possible
> > arising as a result of the use of this medium to transmit messages
> > or to Pequot Capital Management, Inc. or any of its affiliates. If
> > are not the intended recipient, any disclosure, copying,
> forwarding or
> > distribution of this email is prohibited and immediate deletion
> > be effected. We would appreciate your notifying the sender
> > should you become aware of any instances of such occurrence.
Fred Hutchinson Cancer Research Center
Ph: (206) 667-2926
Fax: (206) 667-5977
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!