Announcement

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

  • Determine if values of one variable are all the same based on another variable

    Hello, I want to determine if values of var1 are all the same based on a group of values (levels of an identifier variable) in var2 (both vars are strings).
    I tried using:
    bysort var2: egen byte differ = diff(var1)
    however it does that comparison between variables and not with values within a variable, and also doesn't work with by option.
    Thank you for any help.

  • #2
    that will not work as your command is not legitimate (diff requires at least 2 variables in the parenthetical part and compares the values of those two variables); I think that what you want is more like:
    Code:
    bysort var2 (var1): gen byte differ=_n!=_N

    Comment


    • #3
      I am not sure this is what Straso needs. Take the auto data and assume we want to compare the values of foreign for each value of rep78.
      Code:
      . sysuse auto, clear
      . tab rep78 foreign
      
          Repair |
          Record |       Car type
            1978 |  Domestic    Foreign |     Total
      -----------+----------------------+----------
               1 |         2          0 |         2 
               2 |         8          0 |         8 
               3 |        27          3 |        30 
               4 |         9          9 |        18 
               5 |         2          9 |        11 
      -----------+----------------------+----------
           Total |        48         21 |        69
      The way I understand the question, differ should take the value 1 if rep78 is 3, 4 or 5. With the code in post #2 we get:
      Code:
      . bysort rep78 (foreign): gen byte differ=_n!=_N
      . list rep78 foreign differ if rep78==1
      
           +---------------------------+
           | rep78    foreign   differ |
           |---------------------------|
        1. |     1   Domestic        1 |
        2. |     1   Domestic        0 |
           +---------------------------+
      
      . list rep78 foreign differ if rep78==5
      
           +---------------------------+
           | rep78    foreign   differ |
           |---------------------------|
       59. |     5   Domestic        1 |
       60. |     5   Domestic        1 |
       61. |     5    Foreign        1 |
       62. |     5    Foreign        1 |
       63. |     5    Foreign        1 |
           |---------------------------|
       64. |     5    Foreign        1 |
       65. |     5    Foreign        1 |
       66. |     5    Foreign        1 |
       67. |     5    Foreign        1 |
       68. |     5    Foreign        1 |
           |---------------------------|
       69. |     5    Foreign        0 |
           +---------------------------+
      Instead, this FAQ may provide some answers to the original question: How do I count the number of distinct strings across a set of variables?

      Comment


      • #4
        http://www.stata.com/support/faqs/da...ions-in-group/ may also help.

        Comment

        Working...
        X