```Date: Fri, 21 Dec 2007 15:29:12 -0600 Reply-To: "data _null_," Sender: "SAS(r) Discussion" From: "data _null_," Subject: Re: Running deductions based on by variables? Comments: To: Floyd Moseby In-Reply-To: <44aebe13-fd32-4725-9d4f-c1c735c4662c@d4g2000prg.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 data work.test; input x y max @@; cards; 1234 02 3 1234 01 3 1234 00 3 1235 03 5 1235 03 5 1235 01 5 1236 -02 -3 1236 -02 -3 ;;;; run; data work.test; do until(last.x); set work.test; by x; if first.x then do; s = sign(max); r = abs(max); end; a = min(r,abs(y)); r = r - a; a = a * s; output; end; drop s r; run; proc print; run; Obs x y max a 1 1234 2 3 2 2 1234 1 3 1 3 1234 0 3 0 4 1235 3 5 3 5 1235 3 5 2 6 1235 1 5 0 7 1236 -2 -3 -2 8 1236 -2 -3 -1 On Dec 21, 2007 3:02 PM, Floyd Moseby wrote: > Folks, I am having a tough time with this one. Here's the basic > scenario: I have a table with observations sorted by two variables, > the first ascending (X) and the second descending (Y). Each by group > (designated by variable X) must be allocated a finite number of > widgets. The widgets will be allocated to the observations according > to variable Y; the number of widgets allocated to each observation are > determined by the value of variable Y, so that observations with the > highest Y value are allocated the most widgets, and the widgets are > allocated to those observations first. Most by groups will not be > able to allocate the widgets to more than the first couple of > observations; once the number of widgets have been allocated in full, > the remaining observations of that by group get 0. > > Here's some sample data: > > var X var Y max widgets > 1234 02 3 > 1234 01 3 > 1234 00 3 > 1235 03 5 > 1235 03 5 > 1235 01 5 > 1236 -02 -3 > 1236 -02 -3 > > What I need to end up with would resemble this: > > var X var Y max widgets allocated widgets > 1234 02 3 2 > 1234 01 3 1 > 1234 00 3 0 > 1235 03 5 3 > 1235 03 5 2 > 1235 01 5 0 > 1236 -02 -3 -2 > 1236 -02 -3 -1 > > Intuitively, I would use lag variables with conditional statements, > but it's clear that lag variables do not work within conditional > statements. Preceding the conditional logic with lag variable > creation comes close, but the data still doesn't yield the expected > results. > > I'm sure I will kick myself once I figure it out, but hopefully you > guys can help me kick myself sooner rather than later. Any help? > > floyd > ```

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