LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (August 2004, week 5)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
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
Comments: To: Nathan Fong <nmfong@HOTMAIL.COM>
In-Reply-To:  <BAY2-DAV12NJihLEB8p000546ff@hotmail.com>
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 can code

data lead; merge mydata mydata(firstobs=2 keep=var rename=(var=lead_var)); run;

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; set mydata; by BYVAR; if first.BYVAR then __n=0; __n+1; run;

data view2 / view=view2; set mydata; by BYVAR; if first.BYVAR then __n=0; if ^first.BYVAR; __n+1; run;

data lead; merge view1 view2(keep=BYVAR var rename=(var=lead_var)); by BYVAR __n; drop __n; run;

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.

HTH,

Dale

--- 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 > <ramu@pequotcap.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 > multiple > > observations for each company and several hundred companies for > each year. > > > > thanks > > > > ramu > > > > ----------------------------- > > This email (and attachments, if any) is confidential and access by > anyone > > other than the addressee(s) is unauthorized. The security of email > > communication cannot be guaranteed and neither Pequot Capital > Management, > > Inc. nor any of its affiliates accepts liability for possible > claims > > arising as a result of the use of this medium to transmit messages > from > > or to Pequot Capital Management, Inc. or any of its affiliates. If > you > > are not the intended recipient, any disclosure, copying, > forwarding or > > distribution of this email is prohibited and immediate deletion > should > > be effected. We would appreciate your notifying the sender > immediately > > should you become aware of any instances of such occurrence. > > >

===== --------------------------------------- Dale McLerran Fred Hutchinson Cancer Research Center mailto: dmclerra@fhcrc.org Ph: (206) 667-2926 Fax: (206) 667-5977 ---------------------------------------

__________________________________ Do you Yahoo!? New and Improved Yahoo! Mail - 100MB free storage! http://promotions.yahoo.com/new_mail


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