BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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;
9 REPLIES 9
Peter_C
Rhodochrosite | Level 12
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
deleted_user
Not applicable
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;
abdullala
Calcite | Level 5
have you tried do while ('tail condition') loop?
deleted_user
Not applicable
No, I don't even know what that is, I will look it up. Thanks
Any help would be appreciate it!
Patrick
Opal | Level 21
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;
deleted_user
Not applicable
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
Patrick
Opal | Level 21
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?
deleted_user
Not applicable
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
ChrisNZ
Tourmaline | Level 20
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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 2843 views
  • 0 likes
  • 5 in conversation