Announcement

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

  • Sort order for strings with blanks - apply only observed value to all subjects by id

    Hello,
    I have data in this form
    id string string2
    1
    1 N
    1 N
    2
    2
    2
    3
    3
    3 N
    3
    3
    here is the dataex

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1 id str1 string str1 string2
    "1" "" ""
    "1" "N" ""
    "1" "" "N"
    "2" "" ""
    "2" "" ""
    "2" "" ""
    "3" "" ""
    "3" "N" ""
    "3" "" ""
    end

    and I want to convert string and string2 to be:
    id string string2
    1 N N
    1 N N
    1 N N
    2
    2
    2
    3 N
    3 N
    3 N
    3 N
    3 N

    But when I sort by id and string and try to apply to value to all id, the string is sorted with missing values first

    Code:
    bysort id (string): replace string=string[1]
    but as I said the blanks are sorted first and this applies the blank to all each id. Is there a way to reverse the sort order? gsort does not seem to work for strings
    any help would be great



  • #2
    Code:
    foreach var of varlist string*{
          bys id (`var'): replace `var'=`var'[_N]
    }
    Res:

    Code:
    . l, sepby(id)
    
         +-----------------------+
         | id   string   string2 |
         |-----------------------|
      1. |  1        N         N |
      2. |  1        N         N |
      3. |  1        N         N |
         |-----------------------|
      4. |  2                    |
      5. |  2                    |
      6. |  2                    |
         |-----------------------|
      7. |  3        N           |
      8. |  3        N           |
      9. |  3        N           |
         +-----------------------+

    Comment


    • #3
      Yes, that worked thank you

      Comment

      Working...
      X