Announcement

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

  • Excluding inlist items

    I am trying to exclude items in a list for certain commands,
    e.g. looking for data where is loc_state is listed with value that's not a US state
    Code:
    browse if not inlist(loc_state,"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE") &
    not inlist(loc_state,"FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY") &
    not inlist(loc_state,"LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT") &
    not inlist(loc_state,"NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH") &
    not inlist(loc_state,"OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT") &
    not inlist(loc_state,"VT", "VA", "WA", "WV", "WI", "WY")
    Of course, to this Stata simply replies, to each of my conditions individually that "not" is unrecognized.

    How can I exclude the list items when running a command? What is the alternative to not? Or do I simply have the wrong syntax...should i be saying something like not(inlist(var,value,value))...
    Thank you for your help!

    Stata SE/17.0, Windows 10 Enterprise

  • #2
    Code:
    !inlist
    See

    Code:
    help operators

    Better advice is to create an indicator (=1 if a state is a US state, 0 otherwise).
    Last edited by Andrew Musau; 15 Nov 2021, 09:06.

    Comment


    • #3
      I believe your problem goes deeper than the incorrect use of "not" where the not operator "!" was needed.

      When I copy and paste your code I see that it spans six lines, with a line break following each "&".

      If your code is run in a do-file, it needs a line continuation sequence /// following each &.

      If your code is copied and pasted into the command window, it needs to be presented as a single line (no line breaks), thus
      Code:
      browse if ! inlist(loc_state,"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE") & ! inlist(loc_state,"FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY") & ! inlist(loc_state,"LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT") & ! inlist(loc_state,"NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH") & ! inlist(loc_state,"OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT") & ! inlist(loc_state,"VT", "VA", "WA", "WV", "WI", "WY")
      Line continuation is unfortunately not supported in Stata's command window - you have to use a single long line, which Stata will present as wrapped.

      Comment


      • #4
        Thank you!
        Thank you for your help!

        Stata SE/17.0, Windows 10 Enterprise

        Comment

        Working...
        X