Announcement

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

  • Issue with Nested Loop: inner loop does not run.

    EDIT: just realised copied pasted code doesnt show indentation- have also uploaded a image of my code..

    Background: I am in the process of creating a by infection by visit database (HPV infections- has different stain types). As part of it, i have to create certain clearance variables to calculate time to clearance (like clearance date and clearance visit). in my code, i have a loop in a loop. When i run the code, everything runs perfectly until the second loop, which stata refuses to recognise/ run. Im not sure if its a syntax or indentation or some other issue...
    I have also tried alternative ways like writing date and visit as "date" "visit". It always stops right before the second loop- foreach y in date visit {

    I have bolded the 2 loops.

    note: the 1,2,3 are referring to 3 separate clearance definitions that we are looking at.

    CODE:


    foreach x in 1 2 3 {
    gen clear`x'=.
    if `x'==1 {
    bys ID strain_type: replace clear`x'=1 if Result==0 & Result[_n-1]==1
    continue
    }
    else if `x'==2 {
    bys ID strain: replace clear`x'=1 if Result==0 & Result[_n-1]==1 & Result[_n+1]==0
    continue
    }
    else if `x'==3 {
    bys ID strain_type: replace clear`x' =1 if endrow[_n+1]==1 & Result==0 & Result[_n+1]==0 & inlist(infection, "Prevalent", "Incident")
    continue
    }
    gen firstclear`x'=.
    bys ID strain_type: gen sumclear`x'=sum(clear`x')
    bys ID strain_type: replace firstclear`x'=1 if clear`x'==1 & sumclear`x'==1
    drop clear`x' sumclear`x'
    rename firstclear`x' clear`x' //IT RUNS UNTIL HERE
    foreach y in date visit {
    if y==visit {
    bys ID strain_type: gen clear`x'`y'= Visit if firstclear`x'==1
    continue
    }
    else if y==date {
    bys ID strain_type: gen clear`x'`y'= Oral_Date if firstclear`x'==1
    continue
    }
    bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
    bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
    bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
    bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n-1] if clear`x'`y'==.
    }
    bys ID strain_type: gen timetoclear`x'=clear`x'date-firstinfdate
    }

    Attached Files
    Last edited by Sakshi Rajatbhai Tewari; 15 Apr 2022, 08:10.

  • #2
    Please read the FAQ (ESPECIALLY sections 10 and 12) to reformat this question. Images are rarely helpful, using
    Code:
    Code
    delimiters are always preferable.

    Please give your example data using the dataex command. For us to provide meaningful feedback, you must provide either: your example data using the dataex command, the real data from an easily importable source (i.e., Github), or the equivalent of a toy example. Otherwise, anything we say is simply a waste of time. Note, that I'm not trying to be mean in saying this, I'm saying this because if we can't see your dataset exactly as you do with a minimal worked example, anything we suggest is just guesswork. Questions like this one (judging by the code) appear to be syntactically (over?)complicated, and without a minimal worked example of a dataset and code that you've tried to accomplish your task, solving this is simply impossible. Indentation isn't the issue (thank Christ Stata doesn't care about indentation like Python does). Out of curiosity, did you learn to write code in Python?

    Anyways: please, provide us with your example data that encapsulates the problem (as well as your code in the code delimiters) and we're more than okay with helping you solve this.

    EDIT: Welcome to Statalist, Sakshi.
    Last edited by Jared Greathouse; 15 Apr 2022, 08:29.

    Comment


    • #3
      For this
      Code:
      if y==visit {
      ...
      else if y==date {
      try this
      Code:
      if "`y'"=="visit" {
      ...
      else if "`y'"=="date" {
      As it stands now, Stata interprets y and visit and date as variable names; you need to surround them with quotation marks to make them strings, and further, you need to evaluate the local macro y with `y' to substitute the value of the local macro instead of its name.

      Comment


      • #4
        Cant seem to edit original Q. So posting info as reply.
        STATA VERSION= 16.1
        More background: This is an epidemiologic study where healthy people are screened and followed over time to look at HPV infection mechanisms. For this dataset, Oral rinse is collected at every visit and tested for different strains of HPV. if HPV is present, they have a positive result, if its absent, they have a negative result.
        1. Data is confidential so im changing certain things in it and only looking at clear1 definition in this data example. This is a TOY example.
        2. Here I have given example of 2 people. ID 1 and 2. We are looking at 3 HPV strains for both- 16,18,31. ID 1 has 2 visits in the study, while ID 2 has 3 visits.
        3. strain = strain_type in the code, i shortened it in example
        4. Oral date is the date of oral rinse being sampled.
        5. Visit informs of visit number
        6. Result variable tells us if person was positive or negative (1 or 0) for a particular strain at a particular visit.
        7. Clear 1 = the first negative Result after a positive Result for a particular strain over study follow up.
        8. Clear1visit= visit at which a person cleared a particular strain.
        the code i mention below data example runs until second loop. The main job of second loop is to create the clear1visit and clear1date variables in this example. (in my actual dataset, it will create it for each clear definition: eg: clear1visit, clear2visit and clear3visit

        HTML Code:
        ID  strain  Oral_Date             Visit   Result     Clear1     clear1visit    clear1date
        1     16       1Apr19               1          1           .           .                 .
        1     16       3May20               2          0          1          2           3May20 
        1     18       1Apr19               1          1           .           .                 .
        1     18       3May20               2          0           .           .                 .
        1     31       1Apr19               1          0           .           .                 .
        1     31       3May20               2          1           .           .                 .
        
        2     16       1Jan18               1          1           .           .                 .
        2     16       9Feb19               2          0          1          2           9Feb19 
        2     16       5Mar20               3          1           .           .                 .
        2     18       1Jan18               1          0           .           .                 .
        2     18       9Feb19               2          1           .           .                 .
        2     18       5Mar20               3          0          1          3            5Mar20
        2     31       1Jan18               1          0           .           .                 .
        2     31       9Feb19               2          1           .           .                 .
        2     31       5Mar20               3          1           .           .                 .





        Code:
        foreach x in 1 2 3 {
            gen clear`x'=.
            if `x'==1 {
                bys ID strain_type: replace clear`x'=1 if Result==0 & Result[_n-1]==1 
                continue
            }
            else if `x'==2 {
                bys ID strain: replace clear`x'=1 if Result==0 & Result[_n-1]==1 & Result[_n+1]==0
                continue
            }
            else if `x'==3 {
                bys ID strain_type: replace clear`x' =1 if endrow[_n+1]==1 & Result==0 & Result[_n+1]==0 & inlist(infection, "Prevalent", "Incident")
                continue
            }
            gen firstclear`x'=.
            bys ID strain_type: gen sumclear`x'=sum(clear`x')
            bys ID strain_type: replace firstclear`x'=1 if clear`x'==1 & sumclear`x'==1
            drop clear`x' sumclear`x'
            rename firstclear`x' clear`x'
            foreach y in date visit {  //SECOND LOOP
                if y==visit {
                            bys ID strain_type: gen clear`x'`y'= Visit if clear`x'==1 
                            continue
                }
                else if y==date {
                            bys ID strain_type: gen clear`x'`y'= Oral_Date if clear`x'==1
                            continue
                }
                bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==. 
                bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
                bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
                bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n-1] if clear`x'`y'==.
            }
            bys ID strain_type: gen timetoclear`x'=clear`x'date-firstinfdate 
        }
        I hope this helps. I created an account today so I'm trying my best to make exchanges as efficient as possible. Thanks for your input.
        To answer your question- i dont have a strong coding background (undergrad in biotechnology) - we learnt stata as part of my epidemiology masters, but i am learning R and python on the side as i believe they're more employer friendly.

        Comment


        • #5
          Originally posted by William Lisowski View Post
          For this
          Code:
          if y==visit {
          ...
          else if y==date {
          try this
          Code:
          if "`y'"=="visit" {
          ...
          else if "`y'"=="date" {
          As it stands now, Stata interprets y and visit and date as variable names; you need to surround them with quotation marks to make them strings, and further, you need to evaluate the local macro y with `y' to substitute the value of the local macro instead of its name.
          Oh this was so silly on my part- i haven't put y as a macro in if else..Thanks!
          however when i correct that (i tried every way possible, what you said, removing the quotes around `y' etc) it still doesnt work..
          I get this sense that stata is not even reading the second loop for some reason. Because, if it were, my original code should have gotten an error like "cant find variable y" (since i hadnt written it as a macro)..

          Comment


          • #6
            Okay thanks for the code, but we still need the toy dataset generated by dataex. Yout put your dataset in HTML code. But what we need here is the data in the code delimiters. That is, we need to see something like this.
            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float(year cigsale3) double cf float(relative diff_ te_ub te_lb cf_ub cf_lb)
            1970   123 119.82134651069296 -19   3.1786535  13.181376  -6.824069         .         .
            1971   121 121.28473667832696 -18   -.2847367   9.717986  -10.28746         .         .
            1972 123.5 124.80728902850656 -17   -1.307289   8.695435 -11.310012         .         .
            1973 124.4 123.71281491583973 -16    .6871866   10.68991 -9.3155365         .         .
            1974 126.7 125.58585418431278 -15   1.1141428  11.116866   -8.88858         .         .
            1975 127.1 125.50124154491989 -14    1.598757   11.60148  -8.403966         .         .
            1976   128 127.23161992677203 -13      .76838  10.771103  -9.234344         .         .
            1977 126.4 125.22142446733864 -12   1.1785771    11.1813  -8.824146         .         .
            1978 126.1 124.30211296755871 -11   1.7978855   11.80061  -8.204838         .         .
            1979 121.9  121.4519594845576 -10     .448042  10.450766  -9.554681         .         .
            1980 120.2 120.31033638806436  -9  -.11033944   9.892384 -10.113063         .         .
            1981 118.6 118.45282869275808  -8   .14716978  10.149893  -9.855554         .         .
            1982 115.4 116.16303441553967  -7   -.7630329    9.23969 -10.765756         .         .
            1983 110.8 112.16553654588606  -6  -1.3655335    8.63719 -11.368257         .         .
            1984 104.8 104.79919815323449  -5 .0008048985  10.003528 -10.001918         .         .
            1985 102.8 103.78663228449747  -4   -.9866292   9.016094 -10.989352         .         .
            1986  99.7 100.55414064169827  -3   -.8541437    9.14858 -10.856867         .         .
            1987  97.5  98.53773049469044  -2  -1.0377305   8.964993 -11.040454         .         .
            1988  90.1  94.31016267480518  -1   -4.210164   5.792559 -14.212887 108.52305   88.5176
            1989  82.4  91.43004762108781   0   -9.030046   .9726767  -19.03277 110.46281  90.45737
            1990  77.8  86.73737755464903   1   -8.937374   1.065349 -18.940098 105.67747  85.67203
            1991  68.7  81.49684970441062   2  -12.796853   -2.79413 -22.799576 104.29642  78.70272
            1992  67.5  80.17442849432022   3   -12.67443  -2.671706  -22.67715 102.85158  77.50272
            1993  63.4  79.30744600654128   4  -15.907445  -5.904722  -25.91017 105.21761 73.402725
            1994  58.6   77.5410142580436   5  -18.941015 -8.9382925  -28.94374 106.48476  68.60272
            1995  56.4  77.24890915711582   6   -20.84891 -10.846185  -30.85163 108.10054 66.402725
            1996  54.5  75.21275766342676   7  -20.712757 -10.710034  -30.71548 105.92824  64.50272
            1997  53.8  75.18500615087264   8   -21.38501 -11.382285  -31.38773 106.57274  63.80272
            1998  52.3  76.59469871504923   9    -24.2947 -14.291976  -34.29742 110.89212  62.30272
            1999  47.2  73.84499577210156  10  -26.644995 -16.642271 -36.647717 110.49271  57.20272
            2000  41.6  68.03738237069064  11  -26.437384  -16.43466 -36.440105 104.47749  51.60272
            end
            And why? What difference does it make, might you ask. Well, when I copy the data you put into HTML code into my do file editor, Stata tells me
            Code:
            '5Mar20' cannot be read as a number
            So, if you could give me the equivalent of what you put into the code delimiters, formatted using dataex, we'll be much closer to solving this.



            This aside, what error code does Stata issue when the second loop should run? Or otherwise, beyond just saying it doesn't work, what does Stata do that it shouldn't do, or what does it not do that it should do?

            Comment


            • #7
              Originally posted by Jared Greathouse View Post
              Okay thanks for the code, but we still need the toy dataset generated by dataex. Yout put your dataset in HTML code. But what we need here is the data in the code delimiters. That is, we need to see something like this.
              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float(year cigsale3) double cf float(relative diff_ te_ub te_lb cf_ub cf_lb)
              1970 123 119.82134651069296 -19 3.1786535 13.181376 -6.824069 . .
              1971 121 121.28473667832696 -18 -.2847367 9.717986 -10.28746 . .
              1972 123.5 124.80728902850656 -17 -1.307289 8.695435 -11.310012 . .
              1973 124.4 123.71281491583973 -16 .6871866 10.68991 -9.3155365 . .
              1974 126.7 125.58585418431278 -15 1.1141428 11.116866 -8.88858 . .
              1975 127.1 125.50124154491989 -14 1.598757 11.60148 -8.403966 . .
              1976 128 127.23161992677203 -13 .76838 10.771103 -9.234344 . .
              1977 126.4 125.22142446733864 -12 1.1785771 11.1813 -8.824146 . .
              1978 126.1 124.30211296755871 -11 1.7978855 11.80061 -8.204838 . .
              1979 121.9 121.4519594845576 -10 .448042 10.450766 -9.554681 . .
              1980 120.2 120.31033638806436 -9 -.11033944 9.892384 -10.113063 . .
              1981 118.6 118.45282869275808 -8 .14716978 10.149893 -9.855554 . .
              1982 115.4 116.16303441553967 -7 -.7630329 9.23969 -10.765756 . .
              1983 110.8 112.16553654588606 -6 -1.3655335 8.63719 -11.368257 . .
              1984 104.8 104.79919815323449 -5 .0008048985 10.003528 -10.001918 . .
              1985 102.8 103.78663228449747 -4 -.9866292 9.016094 -10.989352 . .
              1986 99.7 100.55414064169827 -3 -.8541437 9.14858 -10.856867 . .
              1987 97.5 98.53773049469044 -2 -1.0377305 8.964993 -11.040454 . .
              1988 90.1 94.31016267480518 -1 -4.210164 5.792559 -14.212887 108.52305 88.5176
              1989 82.4 91.43004762108781 0 -9.030046 .9726767 -19.03277 110.46281 90.45737
              1990 77.8 86.73737755464903 1 -8.937374 1.065349 -18.940098 105.67747 85.67203
              1991 68.7 81.49684970441062 2 -12.796853 -2.79413 -22.799576 104.29642 78.70272
              1992 67.5 80.17442849432022 3 -12.67443 -2.671706 -22.67715 102.85158 77.50272
              1993 63.4 79.30744600654128 4 -15.907445 -5.904722 -25.91017 105.21761 73.402725
              1994 58.6 77.5410142580436 5 -18.941015 -8.9382925 -28.94374 106.48476 68.60272
              1995 56.4 77.24890915711582 6 -20.84891 -10.846185 -30.85163 108.10054 66.402725
              1996 54.5 75.21275766342676 7 -20.712757 -10.710034 -30.71548 105.92824 64.50272
              1997 53.8 75.18500615087264 8 -21.38501 -11.382285 -31.38773 106.57274 63.80272
              1998 52.3 76.59469871504923 9 -24.2947 -14.291976 -34.29742 110.89212 62.30272
              1999 47.2 73.84499577210156 10 -26.644995 -16.642271 -36.647717 110.49271 57.20272
              2000 41.6 68.03738237069064 11 -26.437384 -16.43466 -36.440105 104.47749 51.60272
              end
              And why? What difference does it make, might you ask. Well, when I copy the data you put into HTML code into my do file editor, Stata tells me
              Code:
              '5Mar20' cannot be read as a number
              So, if you could give me the equivalent of what you put into the code delimiters, formatted using dataex, we'll be much closer to solving this.



              This aside, what error code does Stata issue when the second loop should run? Or otherwise, beyond just saying it doesn't work, what does Stata do that it shouldn't do, or what does it not do that it should do?
              Hi,
              pasted below:

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input long ID byte strain_type float Oral_Date double Visit float(Result endrow) str12 infection
              1083  6 21125 1 0 . "No infection"
              1083  6 21439 2 0 . "No infection"
              1083  6 21831 3 0 . "No infection"
              1083  6 22229 4 0 1 "No infection"
              1083 11 21125 1 0 . "No infection"
              1083 11 21439 2 0 . "No infection"
              1083 11 21831 3 0 . "No infection"
              1083 11 22229 4 0 1 "No infection"
              1083 16 21125 1 0 . "No infection"
              1083 16 21439 2 0 . "No infection"
              1083 16 21831 3 0 . "No infection"
              1083 16 22229 4 0 1 "No infection"
              1083 18 21125 1 0 . "No infection"
              1083 18 21439 2 0 . "No infection"
              1083 18 21831 3 0 . "No infection"
              1083 18 22229 4 0 1 "No infection"
              1083 31 21125 1 0 . "No infection"
              1083 31 21439 2 0 . "No infection"
              1083 31 21831 3 0 . "No infection"
              1083 31 22229 4 0 1 "No infection"
              1083 33 21125 1 0 . "No infection"
              1083 33 21439 2 0 . "No infection"
              1083 33 21831 3 0 . "No infection"
              1083 33 22229 4 0 1 "No infection"
              1083 34 21125 1 0 . "No infection"
              1083 34 21439 2 0 . "No infection"
              1083 34 21831 3 0 . "No infection"
              1083 34 22229 4 0 1 "No infection"
              1083 35 21125 1 0 . "No infection"
              1083 35 21439 2 0 . "No infection"
              1083 35 21831 3 0 . "No infection"
              1083 35 22229 4 0 1 "No infection"
              1083 39 21125 1 0 . "No infection"
              1083 39 21439 2 0 . "No infection"
              1083 39 21831 3 0 . "No infection"
              1083 39 22229 4 0 1 "No infection"
              1083 40 21125 1 0 . "No infection"
              1083 40 21439 2 0 . "No infection"
              1083 40 21831 3 0 . "No infection"
              1083 40 22229 4 0 1 "No infection"
              1083 42 21125 1 0 . "No infection"
              1083 42 21439 2 0 . "No infection"
              1083 42 21831 3 0 . "No infection"
              1083 42 22229 4 0 1 "No infection"
              1083 43 21125 1 0 . "No infection"
              1083 43 21439 2 0 . "No infection"
              1083 43 21831 3 0 . "No infection"
              1083 43 22229 4 0 1 "No infection"
              1083 44 21125 1 0 . "No infection"
              1083 44 21439 2 0 . "No infection"
              1083 44 21831 3 0 . "No infection"
              1083 44 22229 4 0 1 "No infection"
              1083 45 21125 1 0 . "No infection"
              1083 45 21439 2 0 . "No infection"
              1083 45 21831 3 0 . "No infection"
              1083 45 22229 4 0 1 "No infection"
              1083 51 21125 1 0 . "No infection"
              1083 51 21439 2 0 . "No infection"
              1083 51 21831 3 0 . "No infection"
              1083 51 22229 4 0 1 "No infection"
              1083 52 21125 1 0 . "No infection"
              1083 52 21439 2 0 . "No infection"
              1083 52 21831 3 0 . "No infection"
              1083 52 22229 4 0 1 "No infection"
              1083 53 21125 1 0 . "No infection"
              1083 53 21439 2 0 . "No infection"
              1083 53 21831 3 0 . "No infection"
              1083 53 22229 4 0 1 "No infection"
              1083 54 21125 1 0 . "No infection"
              1083 54 21439 2 0 . "No infection"
              1083 54 21831 3 0 . "No infection"
              1083 54 22229 4 0 1 "No infection"
              1083 56 21125 1 0 . "No infection"
              1083 56 21439 2 0 . "No infection"
              1083 56 21831 3 0 . "No infection"
              1083 56 22229 4 0 1 "No infection"
              1083 58 21125 1 0 . "No infection"
              1083 58 21439 2 0 . "No infection"
              1083 58 21831 3 0 . "No infection"
              1083 58 22229 4 0 1 "No infection"
              1083 59 21125 1 0 . "No infection"
              1083 59 21439 2 0 . "No infection"
              1083 59 21831 3 0 . "No infection"
              1083 59 22229 4 0 1 "No infection"
              1083 66 21125 1 0 . "No infection"
              1083 66 21439 2 0 . "No infection"
              1083 66 21831 3 0 . "No infection"
              1083 66 22229 4 0 1 "No infection"
              1083 68 21125 1 1 . "Prevalent"   
              1083 68 21439 2 0 . "Prevalent"   
              1083 68 21831 3 0 . "Prevalent"   
              1083 68 22229 4 0 1 "Prevalent"   
              1083 70 21125 1 0 . "No infection"
              1083 70 21439 2 0 . "No infection"
              1083 70 21831 3 0 . "No infection"
              1083 70 22229 4 0 1 "No infection"
              1083 74 21125 1 0 . "No infection"
              1083 74 21439 2 0 . "No infection"
              1083 74 21831 3 0 . "No infection"
              1083 74 22229 4 0 1 "No infection"
              2015  6 20989 1 0 . "No infection"
              2015  6 21411 2 0 . "No infection"
              2015  6 21839 3 0 . "No infection"
              2015  6 22217 4 0 1 "No infection"
              2015 11 20989 1 0 . "No infection"
              2015 11 21411 2 0 . "No infection"
              2015 11 21839 3 0 . "No infection"
              2015 11 22217 4 0 1 "No infection"
              2015 16 20989 1 0 . "No infection"
              2015 16 21411 2 0 . "No infection"
              2015 16 21839 3 0 . "No infection"
              2015 16 22217 4 0 1 "No infection"
              2015 18 20989 1 0 . "No infection"
              2015 18 21411 2 0 . "No infection"
              2015 18 21839 3 0 . "No infection"
              2015 18 22217 4 0 1 "No infection"
              2015 31 20989 1 0 . "No infection"
              2015 31 21411 2 0 . "No infection"
              2015 31 21839 3 0 . "No infection"
              2015 31 22217 4 0 1 "No infection"
              2015 33 20989 1 0 . "No infection"
              2015 33 21411 2 0 . "No infection"
              2015 33 21839 3 0 . "No infection"
              2015 33 22217 4 0 1 "No infection"
              2015 34 20989 1 0 . "No infection"
              2015 34 21411 2 0 . "No infection"
              2015 34 21839 3 0 . "No infection"
              2015 34 22217 4 0 1 "No infection"
              2015 35 20989 1 0 . "No infection"
              2015 35 21411 2 0 . "No infection"
              2015 35 21839 3 0 . "No infection"
              2015 35 22217 4 0 1 "No infection"
              2015 39 20989 1 0 . "Incident"    
              2015 39 21411 2 1 . "Incident"    
              2015 39 21839 3 0 . "Incident"    
              2015 39 22217 4 0 1 "Incident"    
              2015 40 20989 1 0 . "No infection"
              2015 40 21411 2 0 . "No infection"
              2015 40 21839 3 0 . "No infection"
              2015 40 22217 4 0 1 "No infection"
              2015 42 20989 1 1 . "Prevalent"   
              2015 42 21411 2 0 . "Prevalent"   
              2015 42 21839 3 0 . "Prevalent"   
              2015 42 22217 4 0 1 "Prevalent"   
              2015 43 20989 1 0 . "No infection"
              2015 43 21411 2 0 . "No infection"
              2015 43 21839 3 0 . "No infection"
              2015 43 22217 4 0 1 "No infection"
              2015 44 20989 1 1 . "Prevalent"   
              2015 44 21411 2 1 . "Prevalent"   
              2015 44 21839 3 1 . "Prevalent"   
              2015 44 22217 4 0 1 "Prevalent"   
              2015 45 20989 1 0 . "No infection"
              2015 45 21411 2 0 . "No infection"
              2015 45 21839 3 0 . "No infection"
              2015 45 22217 4 0 1 "No infection"
              2015 51 20989 1 0 . "No infection"
              2015 51 21411 2 0 . "No infection"
              2015 51 21839 3 0 . "No infection"
              2015 51 22217 4 0 1 "No infection"
              2015 52 20989 1 0 . "No infection"
              2015 52 21411 2 0 . "No infection"
              2015 52 21839 3 0 . "No infection"
              2015 52 22217 4 0 1 "No infection"
              2015 53 20989 1 0 . "No infection"
              2015 53 21411 2 0 . "No infection"
              2015 53 21839 3 0 . "No infection"
              2015 53 22217 4 0 1 "No infection"
              2015 54 20989 1 1 . "Prevalent"   
              2015 54 21411 2 0 . "Prevalent"   
              2015 54 21839 3 0 . "Prevalent"   
              2015 54 22217 4 0 1 "Prevalent"   
              2015 56 20989 1 0 . "No infection"
              2015 56 21411 2 0 . "No infection"
              2015 56 21839 3 0 . "No infection"
              2015 56 22217 4 0 1 "No infection"
              2015 58 20989 1 0 . "No infection"
              2015 58 21411 2 0 . "No infection"
              2015 58 21839 3 0 . "No infection"
              2015 58 22217 4 0 1 "No infection"
              2015 59 20989 1 0 . "Incident"    
              2015 59 21411 2 1 . "Incident"    
              2015 59 21839 3 0 . "Incident"    
              2015 59 22217 4 0 1 "Incident"    
              2015 66 20989 1 0 . "No infection"
              2015 66 21411 2 0 . "No infection"
              2015 66 21839 3 0 . "No infection"
              2015 66 22217 4 0 1 "No infection"
              2015 68 20989 1 0 . "No infection"
              2015 68 21411 2 0 . "No infection"
              2015 68 21839 3 0 . "No infection"
              2015 68 22217 4 0 1 "No infection"
              2015 70 20989 1 0 . "No infection"
              2015 70 21411 2 0 . "No infection"
              2015 70 21839 3 0 . "No infection"
              2015 70 22217 4 0 1 "No infection"
              2015 74 20989 1 0 . "No infection"
              2015 74 21411 2 0 . "No infection"
              2015 74 21839 3 0 . "No infection"
              2015 74 22217 4 0 1 "No infection"
              end
              format %tddmy Oral_Date
              label values Result test_results_
              label def test_results_ 0 "Neg", modify
              label def test_results_ 1 "Pos", modify
              if you run my code on this it generates clear1, clear2, clear3, but nothing else. it should generate visit and date variables (which is what loop 2 does)
              The issue is, that it doesnt run the code starting from loop 2.. so the clear visit and date variables dont get created..i dont get any error. in my previous code i should have based on the fact that i didnt put y as a macro in the if else condition, stata should have said "variable y not found" but it did not..so it makes me believe that for some reason stata is not even running the second loop.

              Comment


              • #8
                You need to read the output of
                Code:
                help continue
                and understand that the continue commands in your code cause Stata to skip the rest of the commands in the loop and resume with the next iteration. So yes, loop 2 never did get run, as well as the five commands in loop1 before the start of loop2.
                Code:
                . foreach i in 1 2 {
                  2.     if `i'==1 {
                  3.         display "one"
                  4.         continue
                  5.     }
                  6.     else if `i'==2 {
                  7.         display "two"
                  8.     }
                  9.     display "i is `i'"
                 10. }
                one
                two
                i is 2
                
                .

                Comment


                • #9
                  Originally posted by William Lisowski View Post
                  You need to read the output of
                  Code:
                  help continue
                  and understand that the continue commands in your code cause Stata to skip the rest of the commands in the loop and resume with the next iteration. So yes, loop 2 never did get run, as well as the five commands in loop1 before the start of loop2.
                  Code:
                  . foreach i in 1 2 {
                  2. if `i'==1 {
                  3. display "one"
                  4. continue
                  5. }
                  6. else if `i'==2 {
                  7. display "two"
                  8. }
                  9. display "i is `i'"
                  10. }
                  one
                  two
                  i is 2
                  
                  .
                  IT WORKED!!! Thank you so much sir. I will read up on continue..

                  Comment

                  Working...
                  X