Announcement

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

  • Loops

    Hi StataForum, I am a relatively new user running loops. I am trying to figure out my error here when I try and run a loop within a loop to group variables by ICD9Code first and then a variable named Verify. I first identifying subjects with hypertension with certain ICD9Codes then assign values to a variable Verify if a subject has the ICD9Code for hypertension Any assistant greatly appreciated.


    . foreach n of numlist 1/69 {
    2. foreach v in "404" "404.1" "404.1.1" {
    3. if icd9code`n' == "`v'" {
    4. if verify`n' == 0 {
    5. replace eluncomphtnprofile = 1
    6. replace eluncomphtnchart = 0
    7. }
    8. else if verify`n' == 1 {
    9. replace eluncomphtnprofile = 1
    10. replace eluncomphtnchart = 1
    11. }
    12. else verify`n' == 2 {
    13. replace eluncomphtnprofile = 0
    14. replace eluncomphtnchart = 1
    15. }
    16. }
    17. }
    18. }

    } is not a valid command name
    r(199);

    Thank you,
    Joanna



  • #2
    Welcome on Statalist! Your question is about Stata syntax not Mata. You increase the chance of getting help by posting in the correct subforum.

    Nevertheless: There is an if missing in line 12.
    Code:
    foreach n of numlist 1/69 {
        foreach v in "404" "404.1" "404.1.1" {
            if icd9code`n' == "`v'" {
                if verify`n' == 0 {
                    replace eluncomphtnprofile = 1
                    replace eluncomphtnchart = 0
                }
                else if verify`n' == 1 {
                    replace eluncomphtnprofile = 1
                    replace eluncomphtnchart = 1
                }
                else if verify`n' == 2 {
                    replace eluncomphtnprofile = 0
                    replace eluncomphtnchart = 1
                }
            }
        }
    }
    https://twitter.com/Kripfganz

    Comment


    • #3
      Sebastian Kripfganz makes a good point. Here's another: commands of the form

      Code:
      if foo == "whatever"
      or

      Code:
      if foo == 42
      will only test what is true in the first observation of the dataset. So all your tests look in the first observation only and then make changes to all the values in a variable if the test is satisfied

      That may be what you want, but I would guess not.

      This is an FAQ, although to my mind the question in this document is really the answer: https://www.stata.com/support/faqs/p...-if-qualifier/

      I guess you need to turn your statements round and use qualifiers.

      People who do this are usually thinking in terms of other software where such constructs imply a loop over observations, but the if command in Stata is not like that.

      Comment


      • #4
        Thank you both for your feedback. Nick I will look into rewriting the loop command using the if with the qualifiers first. Joanna

        Comment


        • #5
          Try this:

          Code:
          forval n = 1/69 {
              local OK inlist(icd9code`n', "404", "404.1", "404.1.1") 
              replace eluncomphtnprofile = inlist(verify`n', 0, 1) if `OK' 
              replace eluncomphtnchart = inlist(verify`n', 1, 2) if `OK' 
          }

          Comment

          Working...
          X