SAS Datastep code example for probablility calculation please?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 137
Accepted Solution

SAS Datastep code example for probablility calculation please?

Hi Folks,

Can anybody provide me a simple SAS datastep algorithm for the below question and a statistical explanation for my understanding please

If I have a decision to make how do I calculate the probability of a selection how does one calculate the probability?

For example: What is the probability of me selecting Tea – with sugar – with milk.

What is the formula?    0.5 x 0.5 x 0.5


Accepted Solutions
Solution
‎11-10-2014 01:44 PM
Super User
Posts: 19,135

Re: SAS Datastep code example for probablility calculation please?

This is a better question for CrossValidated than here.

P(Tea, sugar, milk)= p(tea)*p(sugar)*p(milk)=0.5*0.5*0.5=1/8

Each odd is 0.5 because there are two options

i.e. choices are tea or coffee so p(tea)=1/2=0.5

and the same logic applies for remaining ingredients.

Another way to calculate this is to list all the possible combinations and see how many times you get your output:

1 out of 8 possible combinations:

Tea|Sugar|Milk

Tea|Sugar|No Milk

Tea|No Sugar|Milk

Tea|No Sugar|No Milk

Coffee|Sugar|Milk

Coffee|Sugar|No Milk

Coffee|No Sugar|Milk

Coffee|No Sugar|No Milk

View solution in original post


All Replies
Super User
Posts: 9,867

Re: SAS Datastep code example for probablility calculation please?

Sorry. I couldn't see that picture. Maybe it is being block by China.

Frequent Contributor
Posts: 137

Re: SAS Datastep code example for probablility calculation please?

Hi,

First off, I apologise for that coz it aint opening. Well the picture depicts as follows:

  1. Tea  ii. with sugar    iii. With milk  or iii. Without milk
  2. Tea    ii. without sugar   iii. With milk or iii. Without milk
  3. Coffee ii.  With sugar    iii. With milk  or iii. Without milk
  4. Coffee ii.  Without sugar    iii. With milk  or iii. Without milk

Wish I could get the pic to work, but that's exactly the information.

Hope you are well.

Thanks so much,

Charlotte

Respected Advisor
Posts: 4,816

Re: SAS Datastep code example for probablility calculation please?

I can't see the picture either. I get the message

Cycle Prohibited


Description: Could not process your request for the document because it would cause an HTTP proxy cycle. Please check the URL and your browser's proxy settings.

PG
Trusted Advisor
Posts: 1,228

Re: SAS Datastep code example for probablility calculation please?

Hi,

Probability selecting Tea – with sugar – with milk is

0.5 x 0.5 x 0.5

Which is aleardy mentioned above in your question.

Frequent Contributor
Posts: 137

Re: SAS Datastep code example for probablility calculation please?

Well, i noticed that. I'm not understanding the logic though or may be I'm having a blonde moment. lol. I thought it would be 0.5*0.25*0.25*0.25*0.25. I still would like you to explain me the formula if you don't mind.

Trusted Advisor
Posts: 1,228

Re: SAS Datastep code example for probablility calculation please?

Sure, tea – with sugar – with milk are 3 independent events each having two possible outcomes. In the given scenario with sugar has prob=0.5, with sugar prob=0.5 and with milk prob=0.5. In the next step we just need to multiply them.

Solution
‎11-10-2014 01:44 PM
Super User
Posts: 19,135

Re: SAS Datastep code example for probablility calculation please?

This is a better question for CrossValidated than here.

P(Tea, sugar, milk)= p(tea)*p(sugar)*p(milk)=0.5*0.5*0.5=1/8

Each odd is 0.5 because there are two options

i.e. choices are tea or coffee so p(tea)=1/2=0.5

and the same logic applies for remaining ingredients.

Another way to calculate this is to list all the possible combinations and see how many times you get your output:

1 out of 8 possible combinations:

Tea|Sugar|Milk

Tea|Sugar|No Milk

Tea|No Sugar|Milk

Tea|No Sugar|No Milk

Coffee|Sugar|Milk

Coffee|Sugar|No Milk

Coffee|No Sugar|Milk

Coffee|No Sugar|No Milk

Super User
Posts: 11,128

Re: SAS Datastep code example for probablility calculation please?

Pedantic note: Assumes the probabilities of the choices are independent.

Super User
Posts: 19,135

Re: SAS Datastep code example for probablility calculation please?

True Smiley Happy

Respected Advisor
Posts: 3,156

Re: SAS Datastep code example for probablility calculation please?

SAS has a function for it:

data _null_;

     p=0.5; /*is a numeric probability of success parameter, the success rate of each test*/

     n=3; /*is an integer number of independent Bernoulli trials parameter: the total number of the tests*/

     m=3;

     /*is an integer number of successes random variable: the number of successful test, in your case,

          the m is the Maximum: n, as the probability of one success in one trial is the same as all success in all trials*/

     prob=PROBBNML(p,n,m)- PROBBNML(p,n,m-1);

     put prob=;

run;

Haikuo

SAS Super FREQ
Posts: 3,630

Re: SAS Datastep code example for probablility calculation please?

Or use the CDF function. The following statements are equivalent:

prob=PROBBNML(p,n,m) - PROBBNML(p,n,m-1);

prob2 = cdf("Binomial",m,p,n) - cdf("Binomial",m-1,p,n);

Respected Advisor
Posts: 3,156

Re: SAS Datastep code example for probablility calculation please?

Thanks for sharing, Rick! CDF function is obviously much more powerful.

Frequent Contributor
Posts: 137

Re: SAS Datastep code example for probablility calculation please?

Hi Haikuo and Rick,Thank you so much for the Genius help. I can't appreciate enough. Anyways, did you memorise SAS functions or you developed a knack to quickly search for the right now? I am asking this coz your inputs will only help me learn faster.

Cheers,

Charlotte

SAS Super FREQ
Posts: 3,630

Re: SAS Datastep code example for probablility calculation please?

SAS is big. I don't pretend to know it all, but I've taken the time to learn about the functions that I use over and over. CDF is one of those functions.  If you do a lot with univariate distributions in SAS, read this article: Four essential functions for statistical programmers - The DO Loop

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 458 views
  • 9 likes
  • 8 in conversation