Announcement

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

  • Keep multiple observations across multiple variables

    Disclosure: I am new to strata. Despite reading the manual and viewing multiple youtube channels I cannot find the accurate code to keep multiple observations across multiple variables.

    For example, there are sixteen drugs (observations) that could have been prescribed under 30 different variables (var=MED1, MED2, etc).

    I want to keep only patients who were prescribed one of these drugs under any of the MED variables (MED1-MED30). I was unsuccessful with multiple egen OK or inlist codes.

    Observations: 12029, 22287, 10150, 10157, 98065, 12116, 09772, 05261, 05051, 10251, 05266, 08179, 06104, 05074, 13111, 12318
    Variables: MED1, MED2... MED30

    Any suggestions?

  • #2
    Something like this:
    Code:
    gen byte keeper = 0
    foreach v of varlist MED* {
        replace keeper = 1 if inlist(`v', 12029, 22287, 10150, 10157, 98065, 12116, 09772, 05261, 05051, 10251, 05266, 08179, 06104, 05074, 13111, 12318)
    }
    keep if keeper
    Now, you didn't supply any example data. You gave a brief, but incomplete, description of your data. (Descriptions of data sets in words are almost inevitably incomplete.) I used my imagination as to what the data might actually look like, although there are other possibilities consistent with your description. Since I am working from imaginary data, the code proposed above is, in a sense, imaginary. And it may need modifications in order to run in the real data set. Moreover, it could not be properly tested without example data, so it may contain errors, even though I am quite confident that the gist of it is correct.

    To avoid these difficulties in the future, don't waste your breath describing data sets. Show example data, and the only guaranteed helpful way to do that is with the -dataex- command. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X