Announcement

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

  • How to sift through data and only keep the rows with the value I need

    Hi everyone.

    Please see the attached sample data. I would like to keep all the rows for any cell value in column cat1, or cat2, or cat3 that start with "F".

    Appreciate it if you could give me some suggestions and guidance.

    Thank you.
    Attached Files
    Last edited by Anqi Zhang; 12 May 2022, 12:57.

  • #2
    Perhaps
    Code:
    keep if substr(cat1,1,1)=="F" | substr(cat2,1,1)=="F" | substr(cat3,1,1)=="F"

    Comment


    • #3
      Originally posted by William Lisowski View Post
      Perhaps
      Code:
      keep if substr(cat1,1,1)=="F" | substr(cat2,1,1)=="F" | substr(cat3,1,1)=="F"
      Thank you Dr. Lisowski.

      If I have many columns, dozens of them, instead of three as shown here. Is there a way to group them first instead of using many "|" syntax?

      Thank you again.

      Comment


      • #4
        Code:
        generate tokeep = 0
        foreach v in varlist cat1 cat2 cat3 {
            replace tokeep = 1 if substr(`v',1,1)=="F"
        }
        keep if tokeep
        See the output of help varlist for examples of ways to concisely list dozens of variable names.

        Comment

        Working...
        X