Announcement

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

  • How to drop observations if they match elements of a local?

    Hi everyone,

    Is there a way to drop certain observations if they match one of the elements in a local?

    I have a few hospital names that I have defined in my local using
    Code:
    #delim 
    local sample `" "St Peters Hospital" 
    "Mission Hospital" "';
    #delim cr
    (I've just mentioned two hospitals here; in reality, I have 60 hospitals I want to include in the local. The hospital names are strings with spaces in between words.)

    In my dataset, I have a variable HospitalName, which has an exhaustive list of 700 hospitals in the country. The local 'sample' is a subset of that. I want Stata to keep only the observations where HospitalName matches one of the names from the local. Could some please advise how to do this? From searching, it seems as if I may need a combination of levelsof + inlist + keep if, but I haven't been able to figure out the code for this.

    Thanks very much in advance!
    Harshita

  • #2
    keep and drop change places here. I think you mean keep everywhere drop is mentioned.

    There are various answers to this and here's mine, which is don't do this.

    Local macros are key in Stata programming but that doesn't mean that they are needed for all kinds of Stata tasks.

    For 60 names a process that is in practice no more tricky to get right is to put the 60 names in a new dataset, merge on identifiers and then keep the intersection of datasets.

    See https://www.stata.com/support/faqs/d...s-for-subsets/

    See also the recent thread https://www.statalist.org/forums/for...sting-variable where the advantages of this are emphasised.

    levelsof is a wonderful command but not useful here.
    Last edited by Nick Cox; 22 Feb 2023, 02:26.

    Comment


    • #3
      Thanks for your response, Nick! Yes, I meant to write keep where I wrote drop, apologies!

      And many thanks for your advice on the use of locals. I will instead use the new dataset + merge option you suggested.
      Last edited by Harshita Joshi; 22 Feb 2023, 03:31.

      Comment

      Working...
      X