Announcement

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

  • keep if either variable is not empty

    Hi all,
    I am very new to Stata. I have a few variables, and two of them are string: open_on, close_on. I want to keep only the sample where open_on is filled or close_on is filled or both are filled. I know my question sounds very easy...but I just do not how to convert it into a conditional code.
    I tried:
    Code:
    drop if opened_on == "" | closed_on == ""
    But only observations where both are filled are kept. I am not sure why.
    I would deeply appreciate your assistance!

  • #2
    Code:
    keep if (opened_on != "" | closed_on != "")

    Comment


    • #3
      If I follow, you want to drop rows where both variables == "", so you need &, not |.

      Code:
      clear
      input str1(opened_on closed_on)
      "a" "b"
      "" "c"
      "d" ""
      "" ""
      end
      
      * keep only the rows where open_on is filled or close_on is filled
      * or both are filled
      generate byte tokeep = opened_on != "" | closed_on != ""
      * drop rows where both variables == ""
      generate byte todrop = opened_on == "" & closed_on == ""
      list
      Output from -list-:

      Code:
      . list
      
           +---------------------------------------+
           | opened~n   closed~n   tokeep   todrop |
           |---------------------------------------|
        1. |        a          b        1        0 |
        2. |                   c        1        0 |
        3. |        d                   1        0 |
        4. |                            0        1 |
           +---------------------------------------+

      Note: Crossed with George Ford's response in #2.
      Last edited by Bruce Weaver; 12 Feb 2023, 10:17.
      --
      Bruce Weaver
      Email: [email protected]
      Version: Stata/MP 18.5 (Windows)

      Comment


      • #4
        thanks guys! solved

        Comment

        Working...
        X