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 (November 2009, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Tue, 24 Nov 2009 11:30:18 -0600
Reply-To:     Joe Matise <snoopy369@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Joe Matise <snoopy369@GMAIL.COM>
Subject:      Re: Urgent - Please help with the code
Comments: To: "Data _null_," <iebupdte@gmail.com>
In-Reply-To:  <ce1fb7450911240857m1a4d671dkb88ffc1eab82980d@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Ahh, that's right. I think I must always forget PEEK/POKE because I disliked pointer math in C++ and thus avoid thinking about it whenever I can...

-Joe

On Tue, Nov 24, 2009 at 10:57 AM, Data _null_; <iebupdte@gmail.com> wrote:

> We can use PEEK and POKE to create a "5 element rolling queue". As > you said the next to last value in the OP's data is incorrect. > > data have; > if _n_ eq 1 then do; > array x[5]; > retain x; > ax1 + addr(x1); > ax2 + addr(x2); > pl + 8*(dim(x)-1); > drop x: ax: p:; > end; > infile cards; > input z check; > > sum = sum(of x[*]); > if not missing(z) then do; > *Move first 4 elements down one position; > call poke(peekC(ax1,pl),ax2,pl); > x1 = z; > end; > cards; > 10 . > . 10 > 20 10 > 30 30 > . 60 > 40 60 > 50 100 > 30 150 > . 170 > . 170 > 10 170 > 20 160 > 30 150 > 40 130 > . 130 > ;;;; > run; > > proc print; > run; > > On 11/24/09, Joe Matise <snoopy369@gmail.com> wrote: > > A more fun way then I initially suggested: > > data have; > > input val1 result; > > datalines; > > 10 . > > . 10 > > 20 10 > > 30 30 > > . 60 > > 40 60 > > 50 100 > > 30 150 > > . 170 > > . 170 > > 10 170 > > 20 160 > > 30 150 > > 40 130 > > . 130 > > ;;;; > > run; > > > > data want; > > set have; > > n=0; > > o=1; > > res=0; > > if _n_ > 1 then do; > > do o=_n_-1 to 1 by -1; > > set have(rename=val1=val1_old drop=result) point=o; > > if not missing(val1_old) then do; > > res=res+val1_old; > > n=n+1; > > end; > > if n ge 5 then leave; > > end; > > end; > > run; > > > > Your initial datastep, by the way, had one difference with mine; I think > my > > result is correct [on the next to last row]. > > > > -Joe > > > > On Tue, Nov 24, 2009 at 10:07 AM, Joe Matise <snoopy369@gmail.com> > wrote: > > > > > Use RETAIN, and keep track of the 'first' variable in your list, and > the > > > count of records you've accumulated. Once that count hits five, beyond > that > > > when you accumulate something into the new variable, also subtract out > the > > > previous 'top'. > > > > > > That's the simple data step solution; I suspect there might be a proc > > > expand solution or something similar, but not that I'm familiar with. > > > > > > -Joe > > > > > > > > > On Tue, Nov 24, 2009 at 4:53 AM, Zish <zishanpurple@gmail.com> wrote: > > > > > >> The data set looks like > > >> > > >> Val1 Sum of last 5 (Non Blank) > > >> 10 . > > >> . 10 > > >> 20 10 > > >> 30 30 > > >> . 60 > > >> 40 60 > > >> 50 100 > > >> 30 150 > > >> . 170 > > >> . 170 > > >> 10 170 > > >> 20 160 > > >> 30 150 > > >> 40 130 > > >> . 130 > > >> > > >> I need to write a code which calculates the second column, which is > > >> the sum of last five values present in column1. > > >> > > > > > > > > >


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