Announcement

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

  • Conditioning certain values across a set of variables.

    Dear Statalist users,



    I am looking for a way to condition upon certain values across a set of variables.

    To be more specific, I want to create a dummy variable equal to 1 if an observation has ONLY certain values across a set of variables.




    For example, I want to create a dummy equal to 1 if a set of variables have one of the three values only: 0, 1 or 81. (nothing else).

    Here's a screenshot of a subset of my data. The first column is the ID variable, the second to the second-last column are the variables which should only have certain values, and the last column is the dummy variable I want to create.

    (In this image I manually created this dummy just for this post.)
    Click image for larger version

Name:	data_subset.png
Views:	1
Size:	7.6 KB
ID:	1655359




    In this image, ID=10001, 10002 and 10005 have dummy equal to 1, because the set of variables (code1996 ~ code2005) have value 0, 1 or 81 only.

    ID=10002 and 10006 have dummy equal to 0, because the set of variables have other values (they both have value 2 in the first two columns).



    The problem is that, this is just a subset of data with 6 variables to be conditioned upon. Full data has total 40 variables, and there are 50 different possible values each variable in the set can have.

    I want to create a dummy if all those 40 variables have 0, 1 or 81 only among 50 different possible values.

    I know "egen anymatch" function can check whether a set of variables have certain values, but I want those variables to have "ONLY" those variables, nothing else.




    I would appreciate if could share any tips/ways to do this.

    Thank you.
    Last edited by Seungmin Lee; 20 Mar 2022, 19:06.

  • #2
    The following untested code may do what you seek.
    Code:
    egen matches = anycount(code*), values(0 1 81)
    generate dummy = matches==6

    Comment


    • #3
      Thank you! This is very clever.

      Comment

      Working...
      X