Announcement

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

  • Help with parsing values in variables

    Dear Statalist,
    I'll like to create var3 as a dummy variable, with the value of 1 where an ID in var2 is also in var1 and 0 where otherwise. var1 and var2 contain IDs, with some commonalities.
    The results should be as below.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4 var1 str2 var2 float var3
    "a1" "a2" 1
    "a3" "a5" 0
    "a2" "a1" 1
    "a10" "a6" 0
    end

  • #2
    A user-written command called rowsort can do that:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4 var1 str2 var2
    "a1" "a2"
    "a3" "a5"
    "a2" "a1"
    "a10" "a6"
    end
    
    * Install it once, if you have not done so:
    net install pr0046, replace
    
    rowsort var1 var2, gen(n1 n2)
    duplicates tag n1 n2, gen(wanted)
    
    list
    Results:
    Code:
         +---------------------------------+
         | var1   var2    n1   n2   wanted |
         |---------------------------------|
      1. |   a1     a2    a1   a2        1 |
      2. |   a3     a5    a3   a5        0 |
      3. |   a2     a1    a1   a2        1 |
      4. |  a10     a6   a10   a6        0 |
         +---------------------------------+

    Comment


    • #3
      Thanks for the suggestion, Ken. I tried it. Rowsort works with only numeric variables. Even with numeric variables, it seem to compare the values row-wise, and not checking if the value in var2 is in a different. May be I'm missing something. Kindly see my results using rowsort


      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(var1 var2) float(var3 n1 n2) byte wanted
      20 15 1 15 20 0
      11 0 0 0 11 0
      15 6 1 6 15 0
      end
      [/CODE]

      Comment


      • #4
        I think what you are after is:
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str4 var1 str2 var2
        "a1" "a2"
        "a3" "a5"
        "a2" "a1"
        "a10" "a6"
        end
        
        gen `c(obs_t)' obs_no = _n
        reshape long var, i(obs_no) j(source)
        by var (source), sort: gen byte wanted = (source[1] != source[_N]) & source == 1
        by obs_no (wanted), sort: replace wanted = wanted[_N]
        reshape wide

        Comment


        • #5
          #3 is wrong in so far as rowsort can sort string variables too. Ken Chui explicitly referenced pr0046, namely the Stata Journal entry

          Code:
          . search pr0046, entry
          
          Search of official help files, FAQs, Examples, and Stata Journals
          
          SJ-20-2 pr0046_1  . . . . . . . . . . .  Speaking Stata: More ways for rowwise
                  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                  Q2/20   SJ 20(2):481--488                                (no commands)
                  focuses on returning which variable or variables are equal
                  to the maximum or minimum in a row
          
          SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .  Speaking Stata: Rowwise
                  (help rowsort, rowranks if installed) . . . . . . . . . . .  N. J. Cox
                  Q1/09   SJ 9(1):137--157
                  shows how to exploit functions, egen functions, and Mata
                  for working rowwise; rowsort and rowranks are introduced
          where pr0046_1 is a sequel, not an update. The 2009 version of rowsort, as said, can cope with string variables. Otherwise how could Ken have used it successfully? Perhaps you used the previous version of rowsort, which remains on SSC.

          That said, the problem in #1 was, it seems, not fully explained, so #4 is more likely to be a solution.

          Comment


          • #6
            Thanks Clyde Schechter. That solution solved my issue.

            Comment

            Working...
            X