## Repost: Loop over a particular combination

Regular Contributor
Posts: 154

# Repost: Loop over a particular combination

[ Edited ]

The table below lists each possibility for each variable 'A', 'B' and 'C', and the probability of that combination occurring. I already know all the probabilities.

Row # | A | B | C | Probability
============================================================
0 | ... | ... | ... | P0
------------------------------------------------------------
1 | ... | ... | ... | P1
------------------------------------------------------------
. | ... | . | . | .
------------------------------------------------------------
. | ... | . | . | .
------------------------------------------------------------
10 | ... | ... | ... | P10
------------------------------------------------------------

I would like to write an iteration within a combination of variables 'A', 'B' and 'C', start from Row = 0, keep increasing
for Row # = i. If P(Row >= i) * 300 - P(Row < i) * 600 > 0 do i = i+1; until P(Row >= i) * 300 - P(Row < i) * 600 <= 0 or i =10.

It will stop and output i for a combination of 'A', 'B' and 'C'. And i is what I need for a combination of (A / B / C). Which is regarded as 'Threshold' in my problem.

How could I write this kind of code since there are many combinations of variables 'A', 'B' and 'C'.

Example:

Row # | A | B | C | Probability
============================================================
0 | ... | ... | ... | 0.05
------------------------------------------------------------
1 | ... | ... | ... | 0.1
------------------------------------------------------------
2 | ... | ... | ... | 0.15
------------------------------------------------------------
3 | ... | ... | ... | 0.2
------------------------------------------------------------

4 | ... | ... | ... | 0.2
------------------------------------------------------------

...................................
10 | ... | ... | ... | 0.03
------------------------------------------------------------

When i = 0, P(Row >= 0) * 300 - P(Row < 0) * 600 = 1* 300 - 0* 600 = 300 > 0 Hence, i = 0+1 = 1

When i = 1, P(Row >= 1) * 300 - P(Row < 1) * 600 = 0.95* 300 - 0.05* 600 = 255 > 0 i = 1+1 = 2

When i = 2, P(Row >= 2) * 300 - P(Row < 2) * 600 = 0.85* 300 - 0.15* 600 = 165 > 0 i = 2+1 = 3

When i = 3, P(Row >= 3) * 300 - P(Row < 3) * 600 = 0.70* 300 - 0.30* 600 = 30 > 0 i = 3+1 = 4

When i = 4, P(Row >= 4) * 300 - P(Row < 4) * 600 = 0.50* 300 - 0.50* 600 = -150 < 0

we stop and output i =4 for this combination of 'A', 'B' and 'C'.

And, P(Row >= i) = 1- P(Row < i)

Thank you!

Super User
Posts: 13,358

## Re: Repost: Loop over a particular combination

It really would help to post the data values. Use a datastep generated from your data. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

When you say this for example:

When i = 3, P(Row >= 3) * 300 - P(Row < 3) * 600 = 0.70* 300 - 0.30* 600 = 30 > 0 i = 3+1 = 4

1) where does i come from at the begining? You don't mention a variable i in the description. Are you perhaps meaning two different things for i?

2) What does P(Row>=3) mean? Is that supposed to be a specific probability value or the sum/product/difference/other calculation involving all of the probabilites for row 3 through 10? If so provide the individual values  and how they are combined to get (I assume) a value of .70.

3) why does this: When i = 2, P(Count >= 2) * 300 - P(Row < 2) * 600 = 0.85* 300 - 0.15* 600 = 165 > 0 i = 2+1 = 3 use count?

I see a relation, I think, that  P(Row < 0) = (1- P(Row >= 0)). Does that relationship hold.

I am not sure what the actual values for the probability are an how they relate as there is not any example. example.

If all of these P(Row >= 0)  actually mean the value of the Probability variable that is being hidden behind P0 then your formula reduces to 900*Probability - 600

Regular Contributor
Posts: 154

## Re: Repost: Loop over a particular combination

Hi,

Sorry for making you confused. The content original post is gone with the example probability table.I just updated with my post so that probability can be seen.

i means Row #, we start from Row # = 0, which means i starts at 0, and keep increasing to some threshold.

By the way, as you mentioned, P(Row < 0) = (1- P(Row >= 0)) always holds.

And 'Count' is a typo, please regard it as row #, and I changed it as well.

Thank you!

Super User
Posts: 13,358

## Re: Repost: Loop over a particular combination

P(Row >= 0) apparently means P0 + P1 + P2 + .. + P10.

As such I think this really needs to go to Proc IML as you are doing matrix manipulation. To do this out side of Proc IML I think is going to require a transpose, several arrays and likely another transpose to get into a useable output format.

And you really should show enought data for a least one use case in the form of a datastep, the desired result in the form of a datastep as well.

I'm not going to make a fake data set to try to write code against. You have the starting data set and it is very easy to transform a existing data set to datastep code. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Discussion stats
• 3 replies
• 151 views
• 1 like
• 2 in conversation