Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using the loop counter to do calculations in the loop

    Sorry, I am relatively new to Stata and I am struggling with a problem that shouldn't be too difficult but that I cannot solve.

    I have a datafile with results of 5000 bootstrap replications.
    I am using two variables from this file, DiffMeanQALY and DiffMeanCost. I am creating two new variables, CealingRatio and ProbCE, to create a graph.

    For a large number of threshold values (called CeilingRatio) for the ratio of QALY to Cost (DiffMeanCost divided by DiffMeanQALY) I want to calculate how many of the 5000 replications are below this threshold (ProbCE).
    I have some original code which is burdensome because it requires me to edit the increasing threshold values in many lines of code.
    In the end, I am able to make a linegraph of the points with coordinates CeilingRatio (x-axis), ProbCE (y-axis, only a limited number of lines in the datafile contribute points to the graph, that is fine).

    The original code is.
    egen CeilingRatio = fill(0 5000) ** this establishes 5000 possible values for the x-axis **
    count if DiffMeanCost / DiffMeanQALY <= 0 (which I already converted from ‘count if 0’*DiffMeanQALY >= DiffMeanCost')
    gen ProbCE = r(N)/5000 if CeilingRatio==0 ** ProbCE is calculated by dividing the count over 5000 **.
    count if DiffMeanCost / DiffMeanQALY <= 5000
    replace ProbCE = r(N)/5000 if CeilingRatio==5000
    count if DiffMeanCost / DiffMeanQALY <= 10000
    replace ProbCE = r(N)/5000 if CeilingRatio==10000
    etc.

    I want to replace this code with a loop that increases the threshold value every time the loop runs
    I think forvalues would be the right approach, and I have used the following two:

    forvalues i = 5000 (5000) 100000 {
    count if ‘i’*DiffMeanQALY >= DiffMeanCost ** with the original code**
    replace ProbCE = r(N)/5000 if CeilingRatio==’i’

    ** alternative **
    forvalues i = 5000 (5000) 100000 {
    count if DiffMeanCost/DiffMeanQALY <= ‘i’ ** with the converted code for count **.
    replace ProbCE = r(N)/5000 if CeilingRatio==’i’

    In both instances, the error message displayed is:

    . forvalues i = 0 (50000) 1000000 {
    2. count if DiffMeanCost/DiffMeanQALY <= 'i'
    3. replace ProbCE = r(N)/5000 if CeilingRatio=='i'
    4. }
    'i' invalid name
    r(198);

    I have seen examples in the Stata syntax guide that uses the 'i' for calculations in the loop, but this is not working.
    What am I doing wrong?

    Cheers,
    Henk

  • #2
    I have seen examples in the Stata syntax guide that uses the 'i' for calculations in the loop,..
    Look again, it does not use 'i'. In the PDF documentation it uses curly quotes, and the left quote is different from the right quote.

    Curly quotes are not available in Stata. However, if you look at examples of Stata code here on Statalist you will see that for accessing the contents of a local macro, the left quote is `, not '. The right quote is . On a US keyboard, you will find ` as the upper case character on the key immediately to the left of the 1! key.

    Comment


    • #3
      Thanks Clyde, as usual, the devil is in the detail. I will correct the ` and try again.

      Comment

      Working...
      X