Announcement

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

  • If condition based on list

    I have a list of strings and am looking for something like the pd.isin function in Python, so I can just compare the boolean on the list in one condition and without loops.

    My idea was to store them in a local and then use inlist like this
    Code:
    sysuse auto.dta
    local brands `" "AMC Concord" "Buick Regal" "Buick Skylark" "'
    keep if inlist(make, `brands')
    but this does not work as this post explains
    https://www.statalist.org/forums/for...ith-local-list

    Any other ideas?

  • #2
    I don't know Python (my loss, no prejudice) and your real problem is likely to be more complicated than this.

    But the problem stated is only made more complicated by using a local. Not only is there the "faffing around" (a favourite put down of my charismatic mathematics teacher in 1965-66) of putting elements into a local and then getting them out again immediately after, what is fed to inlist() must be separated by commas.

    Code:
    sysuse auto.dta
    
    keep if inlist(make, "AMC Concord", "Buick Regal", "Buick Skylark")
    Three names are trivial but long before you get to 30 or 300 the coding becomes tedious and error-prone. The favoured method is then to put the subset you want in a separate dataset and then merge and then keep the intersection.
    Last edited by Nick Cox; 01 Apr 2021, 09:36.

    Comment


    • #3
      Thanks for the answer Nick Cox. Indeed, I am asking because my real list of strings is much longer. I don't get your comment about the subset? To define that subset I would still need to write keep if == "" | "" ..?. Update: I get now what you mean, the list of countries in a separate dataset and merge.. Okay, thanks!

      Edit: Sure, for this time, I might as well write out all elements in the inlist function, would have been faster by now anyway. My question was more general interest if there is a neater way, this python function I was referring to does exactly that: You can have a list of strings, compare a variable with .isin and it will a boolean for all observations where the row matches at least one element in the string.

      Last edited by Felix Stips; 01 Apr 2021, 09:49.

      Comment


      • #4
        This is an example of the technique Nick suggests in poost #2
        Code:
        input str40 make
        "AMC Concord"
        "Buick Regal"
        "Buick Skylark"
        "Model T"
        end
        tempfile makes
        save `makes'
        
        sysuse auto.dta, clear
        merge m:1 make using `makes'
        keep if _merge==3
        
        list make price
        And this is the output of the interesting part of the code.
        Code:
        . sysuse auto.dta, clear
        (1978 Automobile Data)
        
        . merge m:1 make using `makes'
        (note: variable make was str18, now str40 to accommodate using data's values)
        
            Result                           # of obs.
            -----------------------------------------
            not matched                            72
                from master                        71  (_merge==1)
                from using                          1  (_merge==2)
        
            matched                                 3  (_merge==3)
            -----------------------------------------
        
        . keep if _merge==3
        (72 observations deleted)
        
        .
        . list make price
        
             +-----------------------+
             | make            price |
             |-----------------------|
          1. | AMC Concord     4,099 |
          2. | Buick Regal     5,189 |
          3. | Buick Skylark   4,082 |
             +-----------------------+
        
        .
        Incidentally, if you read the output of help inlist() you will see that when the list is string values, you are limited to a maximum of 10, so if your real list of strings is not much longer than 3 - say, 11 - you will need to use multiple references to inlist string together with or (|) operators.

        Comment

        Working...
        X