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 (April 2006, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 5 Apr 2006 12:08:59 -0700
Reply-To:     "Terjeson, Mark (IM&R)" <Mterjeson@RUSSELL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "Terjeson, Mark (IM&R)" <Mterjeson@RUSSELL.COM>
Subject:      Re: Identifying the recent history for transaction,
              Problem Retain lag ????
Comments: To: M <mtamboli@GMAIL.COM>
Content-Type: text/plain; charset="US-ASCII"

Hi M,

The common misinterpretation of LAG() is that it is always pointing to the previous observation. While that is the desired objective many times, the way the LAG() function works actually allows much more flexibility and power.

Interpreting the LAG() function as always pointing to the previous observation is true if the LAG() is *not* inside *any* conditional statements. If you have a LAG() for the same desired value inside a conditional statement and you are thinking LAG() is always the last obs then you get out of sync very quickly.

The LAG() is actually the previous value encountered by the LAG() function. So if conditional statements are incurred you have a queue of only selected values to retreive.

The easiest way to handle this is to place a separate assignment outside any conditional code so that the retreivals from LAG() really are from the previous observation. e.g. after the SET statement (or whatever brings obs in) then do something like this: the_lagged_value = LAG(myvar); Then do your conditional stuff and use the the_lagged_value variable to your hearts content. This way you can scatter your use of the_lagged_value throughout your conditional code as often as you want and always be in sync.

Hope this is helpful.

Mark Terjeson Senior Programmer Analyst, IM&R Russell Investment Group

Russell Global Leaders in Multi-Manager Investing

-----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of M Sent: Wednesday, April 05, 2006 11:48 AM To: SAS-L@LISTSERV.UGA.EDU Subject: Identifying the recent history for transaction, Problem Retain lag ????

Hello,

I am not able to figure out what the problem is. Here is what i am trying to do Based on the history bit (hist_bit) of the current record do some action. if hist_bit eq 1 then i am trying to assign the previous product information to the current record.

Here is an example

Input (work.check1)

Name Prod_Name hist_bit Tom Certificate 0 Tom 1 Tom 1 Tom 1

Desired_Output

Name Prod_Name hist_bit Tom Certificate 0 Tom Certificate 1 Tom Certificate 1 Tom Certificate 1

However here is what i am getting based on the code below. Check is a flag set to determine what condition is being executed. I would appreciate if someone could help me in figuring out where the problem lies.

Thanks,

Name Prod_Name hist_bit check flag prod_hist Tom Certificate 0 5 0 Tom 1 3 1 Tom 1 3 1 Tom 1 3 1

/* Code begines here */

data temp; set check1; BY NAME; format prod_hist $CHAR50.; retain prod_hist flag;

if (FIRST.NAME)then do; prod_hist = ' '; flag = 0; check = 5; end;

if NOT(FIRST.NAME)then

do; if (hist_bit eq 0) then do; prod_hist = ' '; flag = 0; check = 6; end;

if (hist_bit eq 1) then do; if (PROD_NAME eq ' ') AND (flag eq 0) then do; prod_hist = lag (PROD_NAME); check = 1; PROD_NAME = prod_hist; flag = 1; end;

if (PROD_NAME ne ' ') AND (flag eq 0) then do; prod_hist = PROD_NAME; flag = 1; check = 2; end;

if (PROD_NAME eq ' ') AND (flag eq 1) then do; PROD_NAME = prod_hist; flag = 1; check = 3; end;

if (PROD_NAME ne ' ') AND (flag eq 1) then do; prod_hist = PROD_NAME; flag = 1; check = 4; end; end; end; run;


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