|
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On
> Behalf Of dc353@hotmail.com
> Sent: Wednesday, March 26, 2008 9:50 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Re: RETAIN Statement --> strange behavior
>
> Here's the same example with the difference all in one line of code:
> If you run this with startshare=startshare+ cash it works as expected
> but if you comment out that line of code and replace with
> startshare=startshare+endshare you get different results. This just
> doesn't make any sense. In the second line the retained value of
> endshare isn't coming through. But when you set cash = endshare the
> retained value of cash is used. What's going on?
>
> data d9;
> set d8;
>
> retain endshare;
> retain cash;
> by ticker;
>
> if first.ticker then do;
> if ticker = '*$$$' then do;
> startshare=9617604;
> endshare=startshare+trade;
> cash=endshare;
> mv=endshare*price;
> cost = endshare*price;
> avecost=1;
> end;
> end;
> else
> if ticker = '*$$$' then do;
> startshare=startshare+cash;
> * startshare=startshare+endshare;
> endshare= startshare + trade;
> cash = endshare;
> mv = endshare*price;
> cost=endshare*price;
> avecost=1;
> end;
> run;
>
The problem is probably that you think RETAIN does something that it doesn't do. All the RETAIN does is prevent endshare from being reset to missing at the beginning of each loop through the data step. You still need to add the increments you want to endshare. Instead of
* startshare=startshare+endshare;
endshare= startshare + trade;
Maybe you want (remove the commented statement)
endshare + (startshare + trade);
Hope this is helpful,
Dan
Daniel J. Nordlund
Research and Data Analysis
Washington State Department of Social and Health Services
Olympia, WA 98504-5204
|