turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Nested Do Loop (Only want to accumulate within 1 l...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-29-2014 12:21 AM

Hi all,

I currently have this program which is supposed to simulate a gambling strategy (educational purposes; this is not a winning strategy by any means). My program is not quite working because when the 2nd iteration of k begins, the InHand variable continues to accumulate from the final value of when k = 1. Essentially, I need the InHand to go back to starting at 0 when a new iteration of k begins. Basically, I just want to repeat everything in the 'i' loop 10,000 times

Does anyone have an idea of a modification I can make to fix this?

data gamble;

do k = 1 to 10000;

do i = 1 to 100000 until (Strategy = "STOP");

Win = RAND('BINOMIAL',0.476,1);

if Win = 1 then Payoff = 5;

else if Win = 0 then Payoff = -5;

InHand + Payoff;

if InHand >= 45 then Strategy = "STOP";

else if InHand <= -50 then Strategy = "STOP";

else Strategy = "CONTINUE";

if InHand = 45 then Result = "WIN ";

else Result = "LOSS";

end;

output;

output;

end;

run;

Thank you in advance!

Accepted Solutions

Solution

06-29-2014
11:43 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-29-2014 11:43 AM

This is what you need?

data gamble;

do k = 1 to 10000;

do i = 1 to 100000 until (Strategy = "STOP");

Win = RAND('BINOMIAL',0.476,1);

if Win = 1 then Payoff = 5;

else if Win = 0 then Payoff = -5;

InHand + Payoff;

if InHand >=45 or inHand<=-50 then Strategy = "STOP";

else Strategy = "CONTINUE";

if InHand = 45 then Result = "WIN ";

else Result = "LOSS";

end;

output;

call missing(Inhand);

end;

run;

All Replies

Solution

06-29-2014
11:43 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-29-2014 11:43 AM

This is what you need?

data gamble;

do k = 1 to 10000;

do i = 1 to 100000 until (Strategy = "STOP");

Win = RAND('BINOMIAL',0.476,1);

if Win = 1 then Payoff = 5;

else if Win = 0 then Payoff = -5;

InHand + Payoff;

if InHand >=45 or inHand<=-50 then Strategy = "STOP";

else Strategy = "CONTINUE";

if InHand = 45 then Result = "WIN ";

else Result = "LOSS";

end;

output;

call missing(Inhand);

end;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-30-2014 08:24 AM

Thanks a lot to both of you for your help. Both solutions worked perfectly.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-29-2014 11:53 AM

All you need is the statement **InHand = 0;** after **do k = 1 to 10000;**

I also suggest calling STREAMINIT as the first statement of the datastep to get reproducible results.

Why two output statements?

You may also consider replacing the BINOMIAL distribution with the BERNOULLI.

PG

PG