Announcement

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

  • Identify variables with a particular value

    Hello, I have a cross-sectional dataset with many variables, and I am attempting to compile a list (disaggregated by my groupvar, county) of instances in which any variable takes on the value of 5.

    I have attempted to do this using the foreach command to loop through all variables, but I'm having some trouble.

    When I use the code below, I get the "type mismatch r(109)" error message.

    foreach var of varlist all {
    by county: list if `var' == 5
    }

    Assuming this occurred because I was not restricting my loop to numeric variables, I attempted to isolate all of the numeric variables with

    ds, has(type numeric)

    However, when I apply the code above, it returns a list of all variables.

    Does anyone have any tips on the best way to compile of list of cases in which a variable takes on a particular value?

    Many thanks!
    Dan

  • #2
    Your code and indeed your question are a little hard to follow.

    In the first segment, did you really type all or was it _all? Either way, that code will indeed fail if it meets a string variable.

    Your closing question is about identifying cases (Stata says observations) for which a variable takes on a particular value and the answer is like

    Code:
    list if myvar == 5


    However, most of your question seems more general. findname (SJ) is an effect a superset of ds with (in my view) improved syntax.

    This example may indicate what you are looking for.

    Code:
    sysuse auto, clear
    findname, type(numeric)
    egen any5 = anymatch(`r(varlist)'), value(5)
    findname, any(@ == 5)
    bysort foreign : list `r(varlist)' if any5
    
    ---------------------------------------------------------------------------
    -> foreign = Domestic
    
         +--------------------------+
         | rep78   headroom   trunk |
         |--------------------------|
     20. |     5        2.0       8 |
     43. |     5        2.5      11 |
     46. |     2        5.0      16 |
         +--------------------------+
    
    ---------------------------------------------------------------------------
    -> foreign = Foreign
    
         +--------------------------+
         | rep78   headroom   trunk |
         |--------------------------|
      1. |     5        3.0      15 |
      5. |     5        2.0       8 |
      9. |     5        3.0      10 |
     10. |     4        2.5       5 |
     14. |     5        2.5      11 |
         |--------------------------|
     15. |     5        2.5      14 |
     16. |     5        3.0       9 |
     17. |     5        2.0      11 |
     19. |     5        3.0      15 |
     22. |     5        2.5      14 |
         +--------------------------+



    In short there are two techniques:

    1. Finding the observations with any 5.

    2. Finding the variables with any 5

    which can naturally be combined.

    You need to install
    findname before you can use it. Start with

    Code:
    search findname
    and find the most recent download location.


    Note that with its any() option findname just ignores type mismatches.
    Last edited by Nick Cox; 10 Aug 2016, 13:33.

    Comment

    Working...
    X