Announcement

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

  • how to remove certain variables from foreach command using if or while ?

    Hello everyone,
    I hope you're all fine.

    I have a question which took me so long, and I couldn't find a way to execute

    Let's suppose that I have the following command:
    foreach var of varlist recurrence_status anemia_cat decreased_Plt_cat
    increased_creat_cat hypernatremia_cat hyponatremia_cat
    hyperkalemia_cat hypokalemia_cat increased_bilirubin_cat
    increased_ALT_cat increased_AST_cat increased_ap_cat
    decreased_WBC_cat {
    if `var'== hyperkalemia_cat | `var'== recurrence_status {
    local 1 hyperkalemia_cat
    local 2 recurrence_status
    logistic `1' rs1695 age
    logistic `1' rs11615 age
    logistic `1' rs2228001 age
    logistic `2' rs1695 age
    logistic `2' rs11615 age
    logistic `2' rs2228001 age
    }
    else if `var' ==anemia_cat {
    local 3 anemia_cat
    logistic `3' rs1695 BMI
    logistic `3' rs11615 BMI
    logistic `3' rs2228001 BMI
    }
    else if `var' =! hyperkalemia_cat | `var' !=recurrence_status | `var' !=anemia_cat

    logistic `var' rs1695
    logistic `var' rs11615
    logistic `var' rs2228001
    }

    I want the foreach loop to run the the logistic regression command with age adjustment if the variables are recurrence_status or hyperkalemia_cat
    and to run the the logistic regression command with BMI adjustment if the variable is anemia_cat

    otherwise, run the foreach command for all the remaining variables without adjustment

    How can I do this using if or while or any other loop that might help ?

  • #2
    Your code by accident is legal but very likely does not do what you want. You want to test the names of variables, not their values. This correction will stand for all:

    Code:
     `var'== hyperkalemia_cat | `var'== recurrence_status
    should be

    Code:
     "`var'"== "hyperkalemia_cat" | "`var'" == "recurrence_status"
    As you wrote the code, Stata will look in the first observation only.

    Comment


    • #3
      There are several other problems with the code shown in #1. The variable list in the -foreach var of varlist...- command breaks over several lines, but does not contain the needed /// to tell Stata that the command continues on the next line. The final -else if...- lacks an opening {. And the -foreach- itself lacks a closing }.

      Moreover, the code can be considerably shortened and made much more readable (as well as efficient because it will avoid rerunning the same regression multiple times) as follows:
      Code:
      foreach var of varlist recurrence_status anemia_cat decreased_Plt_cat ///
          increased_creat_cat hypernatremia_cat hyponatremia_cat ///
          hyperkalemia_cat hypokalemia_cat increased_bilirubin_cat ///
          increased_ALT_cat increased_AST_cat increased_ap_cat ///
          decreased_WBC_cat {
              if inlist("`var'", "hyperkalemia_cat", "recurrence_status") {
                  local adjustment age
              }
              else if "`var'" == "anemia_cat" {
                  local adjustment BMI
              }
              else {
                  local adjustment
              }
              logistic `var' rs1695 `adjustment'
              logistic `var' rs11615 `adjustment'
              logistic `var' rs2228001 `adjustment'
      }
      And this revised code more clearly reflects the underlying logic: all variables mentioned in the -foreach- are to be regressed, separately, against rs1695, rs11615, and rs2228001. And different adjustment factors (including the possibility of none at all in most cases) are to be applied according to the particular outcome variable.

      Comment


      • #4
        Thank you, sir.
        The code worked exactly as I wanted!

        I really appreciate it

        Comment

        Working...
        X