Stata 'while' command

I'm struggling to take some SAS code and 'translate' it into stata and am hoping someone can help. I suspect its a lack of understanding what stata is doing but any thoughts appreciated. The part of the SAS code I am trying to implement in STATA is:

do while (p>0.5);

Which I thought in stata was:

while (p>0.5){
replace ka=(ka-1)
replace p=binomial(n,ka,prop)

However, I get completely different answers, so obviously the way stata deals with the loop is different to sas. Oh, and I know the SAS code is giving the correct answers.

Can post more code if it would be helpful but didnt want to bombard the post with lots of code.


The code you've written will modify p and ka for every observation (ie every row of your dataset), but the "while (p>0.5)" will only look at the first observation (first row) of your dataset. Is that what you want?
No I want it to look at p for each row (move with the bits that are changing if that makes sense!), since p will constantly change, how do I tell it to do that? I though it might be something like p[_n-1] but that didnt appear to solve it.

Thanks for any help!


So basically you want to run the loop a different number of times for each row. If the reason for doing this is to identify one particular threshold you may be better off using one of Stata's binomial distribution functions (eg invbinomial or binomialtail).

Otherwise you need to run the -while- statement for each row, eg:

local row=1
while `row'<=_N {
   while (p[`row']>0.5){
      replace ka=(ka-1) in `row'
      replace p=binomial(n,ka,prop) in `row'
   local row = `row' + 1
Last edited:
Thanks thats quite helpful though still doesnt quite solve what I'm doing (and explaining badly). You are right I am trying to get it to loop until the binomial reaches a certain threshold (in this example, 0.5).

I want the loop to begin again for each row. Each row will have a different n and ka on it to start from. However, initially (until the replace part) every row will have the same p (whatever value is specfied prior to the loop). I'm trying to find the values of ka and its corresponding p which are past the threshold (corresponding to the n on that row).

Does that make any sense at all? I hope so, but if not, what you have given so far has been very helpful. Its the replacing ka part of the code which seems to be the bit going wrong, but your comment about the different binomial functions made me wonder if it was that that was actually the issue (though the other commands dont seem to help if I am implementing them correctly).


Not sure why the code I gave you didn't work. I tried it on a "dummy" dataset here and seemed to work fine. The first -while- loop steps through each row of the dataset (from 1 to _N, which is a special system variable indicating the number of rows). The second -while- loop reduces the ka for the current row until p<=0.5 (in that row). Are you sure there's no typo, eg 'row' instead of `row'? Perhaps it would help if you pasted in your code and output.

I think you're right, the other binomial functions don't seem to allow the determination of a threshold ka.

. list

     |  n   ka   prop   p |
  1. | 20   20     .3   1 |
  2. | 20   30     .2   1 |
  3. | 20   10     .6   1 |
  4. | 20   15     .7   1 |

. local row=1

. while `row'<=_N {
  2.    while (p[`row']>0.5){
  3.       replace ka=(ka-1) in `row'
  4.       replace p=binomial(n,ka,prop) in `row'
  5.    }
  6.    local row = `row' + 1
  7. }
(1 real change made)
(0 real changes made)
(1 real change made)

. list

     |  n   ka   prop          p |
  1. | 20    5     .3   .4163708 |
  2. | 20    3     .2   .4114488 |
  3. | 20    9     .6   .1275212 |
  4. | 20   13     .7   .3919902 |
You know what, I just sat and typed out all my code for you then realised that I am talking absolute nonscence. It works perfectly fine, I was just comparing it to the wrong page in my binomial tables!!

So sorry for that, you have been incredibly helpful going through this with me, its much clearer if your able to explain it to someone!