Announcement

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

  • Troubles with For LOOP

    Dear stata users,

    I want to do a couple of edits to 35 variables, I have a dataset containing 35 columns with codes of injuries for 600 patients(with data in rows). These injury codes have meaning as they belong to different body regions, and that's where I am interested at. I want to give an overview in my paper of the number and percentage of patients that have an injury in a certain body region. I already generated that variable. The thing I can't get working is making stata replace the x by the number of the loop(starting with 1) and thereby reading the inj_x_ais_score as inj_1_ais_score and recognizing thereby the variable in the set and thereby get the thing running. The dumb solution to this would be to copy this code 35 times and replace the x manually from 1 to 35 but I hope that's not necessary.

    My code looks like this:

    forval x = 1/35{

    drop inj_x_ais_score

    drop inj_ais_string_x

    tostring inj_ais_x, generate(inj_ais_string_x)
    order inj_ais_string_x, after(inj_ais_x)
    generate inj_x_ais_score = substr(inj_ais_string_x,8,1)
    order inj_x_ais_score, after(inj_x_body_region)
    destring inj_x_ais_score, replace
    replace inj_x_body_region = 1 if inj_ais_x >= 100000 & inj_ais_x <= 200000
    replace inj_x_body_region = 2 if inj_ais_x >= 200000 & inj_ais_x <= 300000
    replace inj_x_body_region = 3 if inj_ais_x >= 300000 & inj_ais_x <= 400000
    replace inj_x_body_region = 4 if inj_ais_x >= 400000 & inj_ais_x <= 500000
    replace inj_x_body_region = 5 if inj_ais_x >= 500000 & inj_ais_x <= 600000
    replace inj_x_body_region = 6 if inj_ais_x >= 600000 & inj_ais_x <= 700000
    replace inj_x_body_region = 7 if inj_ais_x >= 700000 & inj_ais_x <= 900000
    replace inj_x_body_region = 9 if inj_ais_x >= 900000 & inj_ais_x <= 1000000
    }

    This happens:

    . forval x = 1/35{
    2.
    .
    . tostring inj_ais_x, generate(inj_ais_string_x)
    3. order inj_ais_string_x, after(inj_ais_x)
    4. generate inj_x_ais_score = substr(inj_ais_string_x,8,1)
    5. order inj_x_ais_score, after(inj_x_body_region)
    6. destring inj_x_ais_score, replace
    7. replace inj_x_body_region = 1 if inj_ais_x >= 100000 & inj_ais_x <= 200000
    8. replace inj_x_body_region = 2 if inj_ais_x >= 200000 & inj_ais_x <= 300000
    9. replace inj_x_body_region = 3 if inj_ais_x >= 300000 & inj_ais_x <= 400000
    10. replace inj_x_body_region = 4 if inj_ais_x >= 400000 & inj_ais_x <= 500000
    11. replace inj_x_body_region = 5 if inj_ais_x >= 500000 & inj_ais_x <= 600000
    12. replace inj_x_body_region = 6 if inj_ais_x >= 600000 & inj_ais_x <= 700000
    13. replace inj_x_body_region = 7 if inj_ais_x >= 700000 & inj_ais_x <= 900000
    14. replace inj_x_body_region = 9 if inj_ais_x >= 900000 & inj_ais_x <= 1000000
    15. }
    variable inj_ais_x not found
    r(111);

    end of do-file


    Can anyone help me out? that would be great, also to be a little bit more time efficiƫnt in the future

    Best regards,

    Anne Fokkema(surgery resident conducting research after thoracic trauma)

  • #2
    your forval loop is defined for `x' but you never use that within your loops - it is not clear what you want to do, but what you are doing is not using your forval loop at all; see
    Code:
    help forval
    and pay attention to all the characters in the examples

    Comment


    • #3
      Rich Goldstein has I guess the main point here. Note that the code in


      Code:
      replace inj_x_body_region = 1 if inj_ais_x >= 100000 & inj_ais_x <= 200000
      replace inj_x_body_region = 2 if inj_ais_x >= 200000 & inj_ais_x <= 300000
      replace inj_x_body_region = 3 if inj_ais_x >= 300000 & inj_ais_x <= 400000
      replace inj_x_body_region = 4 if inj_ais_x >= 400000 & inj_ais_x <= 500000
      replace inj_x_body_region = 5 if inj_ais_x >= 500000 & inj_ais_x <= 600000
      replace inj_x_body_region = 6 if inj_ais_x >= 600000 & inj_ais_x <= 700000
      replace inj_x_body_region = 7 if inj_ais_x >= 700000 & inj_ais_x <= 900000
      replace inj_x_body_region = 9 if inj_ais_x >= 900000 & inj_ais_x <= 1000000
      pushes first one way and then the other if values are exactly 200000(100000)900000. It looks as if you could just go


      Code:
      replace inj_x_body_region = floor(inj_ais_x/100000)
      where the x references should perhaps be `x'

      Comment


      • #4
        Rich Goldstein i don't understand competely. I want to make a loop which does the code that I wrote, as if the value 'i' would be 1, 2, 3, 4, 5, 6, until 35. Nick Cox I see your point, i made an adaptation, but still can't get the thing do what I want. The error I get now is ambigous abbreviation, any idea how to deal with that?

        My code looks like this now:
        forvalues i = 1(1)35{

        drop inj_'i'_ais_score

        drop inj_ais_string_'i'

        tostring inj_ais_'i', generate(inj_ais_string_'i')
        order inj_ais_string_'i', after(inj_ais_'i')
        generate inj_'i'_ais_score = substr(inj_ais_string_'i',8,1)
        order inj_'i'_ais_score, after(inj_'i'_body_region)
        destring inj_'i'_ais_score, replace
        replace inj_'i'_body_region = 1 if inj_ais_'i' >= 100000 & inj_ais_'i' < 200000
        replace inj_'i'_body_region = 2 if inj_ais_'i' >= 200000 & inj_ais_'i' < 300000
        replace inj_'i'_body_region = 3 if inj_ais_'i' >= 300000 & inj_ais_'i' < 400000
        replace inj_'i'_body_region = 4 if inj_ais_'i' >= 400000 & inj_ais_'i' < 500000
        replace inj_'i'_body_region = 5 if inj_ais_'i' >= 500000 & inj_ais_'i' < 600000
        replace inj_'i'_body_region = 6 if inj_ais_'i' >= 600000 & inj_ais_'i' < 700000
        replace inj_'i'_body_region = 7 if inj_ais_'i' >= 700000 & inj_ais_'i' < 900000
        replace inj_'i'_body_region = 9 if inj_ais_'i' >= 900000 & inj_ais_'i' < 1000000
        }

        What steps do I need to take to make it work?

        Best regards.

        Comment


        • #5
          You took one of my points but you missed the other. Your code can, I think, be slimmed down, as follows. Note that the left and right quotation marks for local macro references are different.

          I can't test anything and any problem of ambiguous abbreviation can only be resolved by knowing which command triggers it and what other variables you have

          Code:
          drop inj_*_ais_score 
          
          
          forvalues i = 1(1)35{
          
          generate inj_`i'_ais_score = real(substr(string(inj_ais_`i'),8,1)) 
          order inj_`i'_ais_score, after(inj_`i'_body_region)
          
          replace inj_`i'_body_region = floor(inj_ais_`i'/100000) 
          
          }
          However, you may have a specific reason for mapping 7000000 to 899999 to 7, as in

          Code:
           
           replace inj_`i'_body_region = 7 if inj_ais_`i' >= 700000 & inj_ais_`i' < 900000
          If you do you just need an extra line

          Code:
            
           replace inj_`i'_body_region = 7 if inj_`i'_body_region == 8

          Comment


          • #6
            It worked perfectly! thanks alot, this saved me alot of work!

            Comment

            Working...
            X