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
- /
- SAS Procedures
- /
- Coin Toss Problem

Topic Options

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-17-2009 09:21 AM

I am doing a coin toss simulation, simple enough (below is my code)

but I want to add one more step to it and not sure how do I go about it:

If I get Heads, I stop, but if I get a Tail, I toss again, if I get a head I stop, but again if I get a tail I toss again......I keep tossing until I get a head, then I add up all the times I get a head and all the tails.

I guess I need a macro and another while loop, but I can't seem to get it, any help would be greatly appreciated. THANKS!

DATA random;

DO i=1 to 100;

x = UNIFORM(123456);

IF x>.5 THEN coin = 'heads';

ELSE coin = 'tails';

OUTPUT;

END;

RUN;

PROC FREQ DATA=random;

table coin;

RUN;

but I want to add one more step to it and not sure how do I go about it:

If I get Heads, I stop, but if I get a Tail, I toss again, if I get a head I stop, but again if I get a tail I toss again......I keep tossing until I get a head, then I add up all the times I get a head and all the tails.

I guess I need a macro and another while loop, but I can't seem to get it, any help would be greatly appreciated. THANKS!

DATA random;

DO i=1 to 100;

x = UNIFORM(123456);

IF x>.5 THEN coin = 'heads';

ELSE coin = 'tails';

OUTPUT;

END;

RUN;

PROC FREQ DATA=random;

table coin;

RUN;

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

Posted in reply to deleted_user

11-17-2009 10:01 AM

for the purpose of clarity, your challenge is perhaps in need of more sample input and output data.

Before working hard to simulate something that is delivered with a SAS function, have you looked at**"Using Random-Number Functions" **http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a001281561.htm ?

PeterC

Before working hard to simulate something that is delivered with a SAS function, have you looked at

PeterC

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

Posted in reply to Peter_C

11-17-2009 10:35 AM

I am using Random numbers, I made few changes but still not right, any help???

Thanks in advance!

data coin;

Headcount=0;

Tailcount=0;

do k=1 to 1000; /*do it 1000 times*/

c = 0; /* this is to reset the loop if I get tail*/

do while (c = 0);

do i = 1 to 100;

x = ranuni (-3); /*generate randon number*/

IF x>.5 THEN do;

HeadCount = HeadCount+1;

c = 1;

OUTPUT;

end;

else do;

TailCount=TailCount+1;

c=0;

end;

end;

end;

output;

file print ;

put 'Number of Heads=' HeadCount;

put 'Number of Tail=' TailCount;

end;

Thanks in advance!

data coin;

Headcount=0;

Tailcount=0;

do k=1 to 1000; /*do it 1000 times*/

c = 0; /* this is to reset the loop if I get tail*/

do while (c = 0);

do i = 1 to 100;

x = ranuni (-3); /*generate randon number*/

IF x>.5 THEN do;

HeadCount = HeadCount+1;

c = 1;

OUTPUT;

end;

else do;

TailCount=TailCount+1;

c=0;

end;

end;

end;

output;

file print ;

put 'Number of Heads=' HeadCount;

put 'Number of Tail=' TailCount;

end;

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

Posted in reply to deleted_user

11-17-2009 12:27 PM

have you tried do while ('tail condition') loop?

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

Posted in reply to abdullala

11-17-2009 03:07 PM

No, I don't even know what that is, I will look it up. Thanks

Any help would be appreciate it!

Any help would be appreciate it!

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

Posted in reply to deleted_user

11-17-2009 07:27 PM

Are you after something like the following?

proc format;

value HeadTail

low - 0.5 = 'Tail'

0.5 <- high = 'Head'

;

run;

DATA random;

format coin HeadTail.;

DO group=1 to 100;

toss=1;

coin = UNIFORM(123456);

do until (coin>.5);

output;

coin = UNIFORM(123456);

toss+1;

end;

END;

RUN;

PROC FREQ DATA=random;

table group*coin;

RUN;

proc format;

value HeadTail

low - 0.5 = 'Tail'

0.5 <- high = 'Head'

;

run;

DATA random;

format coin HeadTail.;

DO group=1 to 100;

toss=1;

coin = UNIFORM(123456);

do until (coin>.5);

output;

coin = UNIFORM(123456);

toss+1;

end;

END;

RUN;

PROC FREQ DATA=random;

table group*coin;

RUN;

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

Posted in reply to Patrick

11-17-2009 09:23 PM

I think this is what I am looking for. Thank you so much!

I am still not good at interpreting things is SAS and I can't make out the results.

What I am trying to prove is that at the end you have the same (almost) number of tails and heads. Lets say I start with 50 coins

I will get 25 heads and 25 tails (in a perfect scenario)

out of the 25 tails I toss again

I will get 12.5 heads and 12.5 tail (lets say 12 heads and 13 Tail)

out of the 13 tail I toss again

I will get 6 heads and 7 tails ( rounded)

out of the 7 tails I toss again

I will get 3 heads and 4 tails

out of the 4 tails I toss again

I will get 2 heads and 2 tails

out of 2 tails I toss again

I get 1 head and one tails

if I count all the heads I will get

25+12+6+3+2+1=49

now lets count the tails

25+13+7+4+2+1=52

so at the end I still have almost a 1:1 ratio

is that what I should expect when I run your code?

Thanks again for all you help

I am still not good at interpreting things is SAS and I can't make out the results.

What I am trying to prove is that at the end you have the same (almost) number of tails and heads. Lets say I start with 50 coins

I will get 25 heads and 25 tails (in a perfect scenario)

out of the 25 tails I toss again

I will get 12.5 heads and 12.5 tail (lets say 12 heads and 13 Tail)

out of the 13 tail I toss again

I will get 6 heads and 7 tails ( rounded)

out of the 7 tails I toss again

I will get 3 heads and 4 tails

out of the 4 tails I toss again

I will get 2 heads and 2 tails

out of 2 tails I toss again

I get 1 head and one tails

if I count all the heads I will get

25+12+6+3+2+1=49

now lets count the tails

25+13+7+4+2+1=52

so at the end I still have almost a 1:1 ratio

is that what I should expect when I run your code?

Thanks again for all you help

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

Posted in reply to deleted_user

11-17-2009 11:00 PM

Yes, that's what you can expect. You just would have to analyse the generated data appropriately (a simple Proc Freq won't do).

But.... What do you want to prove? That a SAS uniform distribution works as documented?

In the end SAS also uses only a mathematical algorithm to generate the random numbers - and I assume you'll find this algorithm documented... somewhere.

So: If you want to prove something then it would be a mathematical approach proofing that the algorithm used is correct and an empirical proof showing that SAS really uses the algorithm as documented.

And because it's about random numbers with no dependencies: Just generate a lot of observations and look at the distribution – no point to analyse sub-groups. It’s always the same probability so what do you expect?

But.... What do you want to prove? That a SAS uniform distribution works as documented?

In the end SAS also uses only a mathematical algorithm to generate the random numbers - and I assume you'll find this algorithm documented... somewhere.

So: If you want to prove something then it would be a mathematical approach proofing that the algorithm used is correct and an empirical proof showing that SAS really uses the algorithm as documented.

And because it's about random numbers with no dependencies: Just generate a lot of observations and look at the distribution – no point to analyse sub-groups. It’s always the same probability so what do you expect?

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

Posted in reply to Patrick

11-18-2009 10:13 AM

Thank you Patrick!

Believe or not my degree is in Mathematics, but I find Statistics confusing!

I am not going to prove the algorithm, I just wanted a simple simulation of it.

I have been trying to learn SAS for the past 2 months and I am in a love/hate relationship; while some days I can code easily, other days I find myself stuck and not really knowing where to start. So thank you so much for your help, I really do appreciate it

Believe or not my degree is in Mathematics, but I find Statistics confusing!

I am not going to prove the algorithm, I just wanted a simple simulation of it.

I have been trying to learn SAS for the past 2 months and I am in a love/hate relationship; while some days I can code easily, other days I find myself stuck and not really knowing where to start. So thank you so much for your help, I really do appreciate it

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

Posted in reply to deleted_user

11-26-2009 04:49 PM

Interestingly, we don't get 50% at all, we get 75%-25%.

That's because the last draw (that stops the loop) is not saved. So each time we have to redraw (1 out of 2 times), we don't save the final head.

This might be better:

DATA random;

format coin HeadTail.;

do group=1 to 1000;

toss=1;

do until (coin>.5);

coin = ranuni(0);

output;

toss+1;

end;

end;

run;

PROC FREQ DATA=random;

table coin;

RUN;

That's because the last draw (that stops the loop) is not saved. So each time we have to redraw (1 out of 2 times), we don't save the final head.

This might be better:

DATA random;

format coin HeadTail.;

do group=1 to 1000;

toss=1;

do until (coin>.5);

coin = ranuni(0);

output;

toss+1;

end;

end;

run;

PROC FREQ DATA=random;

table coin;

RUN;