Announcement

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

  • Using foreach on for a subset of the data

    Hello, I am trying to replace missing values of ptfidelity_9_ - ptfidelity_11_ below with 0

    Code:
    foreach x of varlist ptfidelity_9_ - ptfidelity_11_ {
    replace `x' = 0 if missing(`x') if redcap_event_name_rc == 1
    }

    Error: invalid syntax

    I have also tried
    foreach x of varlist ptfidelity_9_ - ptfidelity_11_ if redcap_event_name_rc == 1 {
    replace `x' = 0 if missing(`x')
    }

    but I get the same error.

    It seems that using the if statement at the end of the command should work as it is at the end of the command. Cant figure why it is not.... Some assistance is sure appreciated!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float redcap_event_name_rc byte(ptfidelity_9_ ptfidelity_10_ ptfidelity_11_)
    0 . . .
    1 . 1 .
    0 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 0 1 1
    2 . . .
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    0 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 0 1 0
    2 . . .
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 0 1 1
    2 . . .
    3 . . .
    0 . . .
    1 0 1 0
    2 . . .
    3 . . .
    0 . . .
    1 1 0 0
    2 . . .
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 0 1 1
    2 . . .
    3 . . .
    0 . . .
    1 1 0 1
    2 . . .
    3 . . .
    0 . . .
    1 . 1 1
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 0 0 0
    2 . . .
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    1 0 1 1
    2 . . .
    3 . . .
    0 . . .
    1 1 1 0
    2 . . .
    3 . . .
    0 . . .
    1 1 1 1
    2 . . .
    3 . . .
    0 . . .
    0 . . .
    1 1 1 1
    end
    label values ptfidelity_9_ pt_fidelity_9_
    label def pt_fidelity_9_ 0 "No", modify
    label def pt_fidelity_9_ 1 "Yes", modify
    label values ptfidelity_10_ pt_fidelity_10_
    label def pt_fidelity_10_ 0 "No", modify
    label def pt_fidelity_10_ 1 "Yes", modify
    label values ptfidelity_11_ pt_fidelity_11_
    label def pt_fidelity_11_ 0 "No", modify
    label def pt_fidelity_11_ 1 "Yes", modify

  • #2
    Use & to specify two if conditions

    Code:
    replace `x' = 0 if missing(`x') & redcap_event_name_rc == 1

    Comment


    • #3
      Of course! Thanks so much.

      Comment

      Working...
      X