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 (July 2005, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 18 Jul 2005 16:54:51 -0400
Reply-To:   Jim Groeneveld <jim1stat@YAHOO.CO.UK>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Jim Groeneveld <jim1stat@YAHOO.CO.UK>
Subject:   Re: RETAIN statement and how SAS processes data
Comments:   To: Robert Slotpole <rslotpole@COMCAST.NET>

Hi Bob,

Me again. From two other postings, one from TMK and one from you ("it destroys startshare and endshare for all the other ticker values"), I understand you in your dataset already have the variables StartShare and EndShare. That makes things quite different. SET overwrites their values for each record with the existing values in the dataset, so RETAINing them does not make sense.

In this case you indeed have to use new variables, e.g. those new_ ones, and assign their values to the existing ones after each calculation:

data cmp_7 (DROP=new_:); set cmp_6; by ticker; * you don't use Date with FIRST, so you don't need to specify it with BY; If ticker = '*$$$' then do; * Using EndShare _before_ it is being recalculated: add to StartShare; new_StartShare = new_EndShare; * following is an implicit [RETAIN StartShare 0;] and SUM func; new_endshare+tot_sales+tot_purchases+tot_div; StartShare = new_StartShare; EndShare= new_EndShare; end; run;

Tell us whether this works.

Regards - Jim.

On Mon, 18 Jul 2005 12:12:14 -0700, rss <rslotpole@COMCAST.NET> wrote:

>Thank you all especially Chris at SAS and Marco who did a great job >identifying what was going on in the code. We now have two working >versions: > >data cmp_7 (drop= new_endshare new_startshare); >retain endshare; > set cmp_6; > by ticker date ; > retain new_endshare; > > If ticker = '*$$$' then do ; > > > > if first.ticker then new_startshare =0; > ** startshare depends on retained value of endshare; > else new_startshare = new_endshare; > ** endshare depends on new value of startshare; > new_endshare = new_startshare + tot_div + tot_sales + >tot_purchases; > > endshare=new_endshare; > startshare=new_startshare; > end; > else; >run; > > >This is the old way and because of previously defined variables we were >getting different results. When we creat new_startshares and >new_endshare the problem goes away. In a more compact form we also >have: > >data cmp_7 (drop=new_endshare new_startshare); > set cmp_6; > by ticker date ; > > If ticker = '*$$$' then do ; >new_endshare=tot_sales+tot_purchases+tot_div; >new_startshare+lag(new_endshare); >new_endshare=new_startshare+new_endshare; > >endshare=new_endshare; >startshare=new_startshare; > > end; >run; > > >Here's my question: in the code below which statement makes it >nescesary to use new variables or why is sas calculating things >differently because values have been previously defined > >data cmp_7(drop=new_endshare new_startshare); > set cmp_6; > by ticker date ; > > If ticker = '*$$$' then do ; > > >STATEMENT 1 >new_endshare=tot_sales+tot_purchases+tot_div; > versus: endshare =tot_sales+tot_purchases + tot_div; >seems statement 1 is pretty strightforward and shouldn't be causing the >problem > >STATEMENT 2 >new_startshare+lag(new_endshare); > versus: startshare =lag(endshare); >Statement 2 I think is ok but am a little less certain > >STATEMENT 3 >new_endshare=new_startshare+new_endshare; >versus: endshare = startshare + endshare **** should >this be just endshare + startshare??? I know that cnt= cnt+1 and >cnt+1 give two very different answers > >I think this blows me out of the water (statement 3) Is there any >other way of solving this or do you have to create new variables. Can >you explain in english what's the difference between the two lines? > > >omit last two lines >endshare=new_endshare; >startshare=new_startshare; > end; >run; > > >I'm trying to understand how the code works. I think the problem is >the third line of code. Is that the reason we need to use new >variables? > >Bob


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