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 (October 1999, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 15 Oct 1999 11:13:02 -0400
Reply-To:     "Brucken, Nancy" <Nancy.Brucken@WL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "Brucken, Nancy" <Nancy.Brucken@WL.COM>
Subject:      Re: What's wrong with the lag function?
Content-Type: text/plain; charset="iso-8859-1"

Hi Ya, This description of the LAG function comes from the SAS 6.12 online help:

The LAG functions, LAG1, LAG2, . . . , LAG100 return values from a queue. LAG1 can also be written as LAG. A LAGn function stores a value in a queue and returns a value stored previously in that queue. Each occurrence of a LAGn function in a program generates its own queue of values. The queue for a LAGn function is initialized with n missing values, where n is the length of the queue (for example, a LAG2 queue is initialized with two missing values). When the LAGn function is executed, the value at the top of the queue is removed and returned, the remaining values are shifted upwards, and the new value of the argument is placed at the bottom of the queue. Hence, missing values are returned for the first n executions of a LAGn function, after which the lagged values of the argument begin to appear. Storing values at the bottom of the queue and returning values from the top of the queue occurs only when the function is executed. A LAGn function that is executed conditionally will store and return only values from the observations for which the condition is satisfied. If the argument of a LAGn function is an array name, a separate queue is maintained for each variable in the array.

Copyright (c) 1995, SAS Institute Inc., Cary, NC 27513-2414 USA. All rights reserved.

So, what I think it's saying is that the first time the LAG function is invoked, it returns a missing value. In your first example, it's only invoked when C=0. That condition is met for the first time on the third record in your dataset, so the LAG function comes back with a missing value. In your second example, it's called for every record. So, by the time your third record is read, it has already been called twice before, and thus returns the value that you expect. Does this make any sense?

Hope it helps, Nancy

Nancy Brucken Parke-Davis, QIP (734) 622-5767 E-mail address: Nancy.Brucken@wl.com

-----Original Message----- From: Huang, Ya [mailto:Y.Huang@ORGANONINC.COM] Sent: Friday, October 15, 1999 10:27 AM To: SAS-L@LISTSERV.UGA.EDU Subject: What's wrong with the lag function?

Hello All,

This question seems to have been asked by somebody long time ago, I don't remember the answer, so forgive me to ask it again.

Can somebody explain to me why I have to use a intermediate variable to keep the lag value and then I can use it to assign to other variable?

DATA XX; INPUT A B C; CARDS; 1 2 1 3 4 1 5 6 0 7 8 1 ; data yy; set xx; if c=0 then a=lag(b); /** not working ***/

PROC PRINT;

data zz; set xx; d=lag(b); if c=0 then a=d; /** this way works **/

proc print; run;

Syntaxly no error in both of the data steps, logically I really don't see the difference between these two data steps, so what is really going on?

Thanks

Ya Huang

Nancy Brucken Parke-Davis, QIP (734) 622-5767 E-mail address: Nancy.Brucken@wl.com


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