BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PC
Calcite | Level 5 PC
Calcite | Level 5

Hello All,

I have this dataset:

Case

Amount

Need1

Need2

Need3

1

30

20

15

10

2

12

20

15

10

3

10

20

15

10

4

8

20

15

10

5

5

20

15

10

6

2

20

15

10

And need your help to obtain Cumulative_subtotal and Cumulative_subtotal_id fields:

Case

Amount

Need1

Need2

Need3

Cumulative_subtotal

Cumulative_subtotal_id

1

30

20

15

10

30

1

2

12

20

15

10

12

2

3

10

20

15

10

22

2

4

8

20

15

10

8

3

5

5

20

15

10

13

3

6

2

20

15

10

2

0

Create multiple cumulative subtotals of amount that cover/hedge Needi.

Cumulative_subtotal=30>=Need1=20 end of id=1;

Cumulative_subtotal=22>=Need2=15 end of id=2;

Cumulative_subtotal=13>=Need3=10 end of id=3;

Cumulative_subtotal_id=0 means not needed.

Thank you for your help,

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Your explanation helps.  Here is one possibility.

data want;

   cumulative_subtotal=0;

   retain cumulative_subtotal_id 1;

   array need {3} ;

   if cumulative_subtotal_id = 0 then total_need = 9999999999999999;

   else total_need = need{cumulative_subtotal_id};

   do until (cumulative_subtotal >= total_need);

      set have;

      cumulative_subtotal + amount;

      output;

   end;

   cumulative_subtotal_id + 1;

   if cumulative_subtotal_id > 3 then cumulative_subtotal_id=0;

run;

It's untested code, so you may need to tweak it.  But it should have enough working pieces.  Usually when a program is this clunky there is an easier way.  I'm just not seeing it right now.

Good luck.

View solution in original post

6 REPLIES 6
Mit
Calcite | Level 5 Mit
Calcite | Level 5

Could you please explain your problem a bit more.

what is cove/hedge etc..

Please put some usuable sample data

PC
Calcite | Level 5 PC
Calcite | Level 5

More details:

sas example.png

ballardw
Super User

What in the data tells that an id group is starting or ending?

PC
Calcite | Level 5 PC
Calcite | Level 5

An id identifies a subtotal: amounts that fill needi must have id=i. The id=i  ends when amount is no longer required for fill needi.

Thanks

Astounding
PROC Star

Your explanation helps.  Here is one possibility.

data want;

   cumulative_subtotal=0;

   retain cumulative_subtotal_id 1;

   array need {3} ;

   if cumulative_subtotal_id = 0 then total_need = 9999999999999999;

   else total_need = need{cumulative_subtotal_id};

   do until (cumulative_subtotal >= total_need);

      set have;

      cumulative_subtotal + amount;

      output;

   end;

   cumulative_subtotal_id + 1;

   if cumulative_subtotal_id > 3 then cumulative_subtotal_id=0;

run;

It's untested code, so you may need to tweak it.  But it should have enough working pieces.  Usually when a program is this clunky there is an easier way.  I'm just not seeing it right now.

Good luck.

PC
Calcite | Level 5 PC
Calcite | Level 5

Astounding,

Thank you for your help. Works perfectly in my example Smiley Happy. I just did a very small adjustment for cases when I have Needi=0 (ex. Need2=0, no amount is necessary to fill).

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1018 views
  • 1 like
  • 4 in conversation