Announcement

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

  • switching column with cond()?

    Dear All, I have the following data
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id str3 rating1 str1 inst1 str3 rating2 str1 inst2
    1 "AA+" "A" "AA+" "B"
    2 "AA"  "C" "AAA" "B"
    3 "AAA" "B" "AA"  "C"
    4 "AAA" "C" "AA+" "B"
    5 "AAA" "B" "AAA" "A"
    6 "AA"
    where `rating1' is the credit rating given by the institution 1 (`inst1'), and `rating2' is the credit rating given by the institution 2 (`inst2'). I all cases, there are two institutions. B institution is always included, along with another institution (say , A or C).
    My question is that: How can I obtain a variable (say `i1') with all B institution along with its credit rating (say, a new variable `r1'). Similarly, another variable (say, `i2', containing the other institution) contains their ratings in `r2'? Thanks.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Is this what you want?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id str3 rating1 str1 inst1 str3 rating2 str1 inst2
    1 "AA+" "A" "AA+" "B"
    2 "AA"  "C" "AAA" "B"
    3 "AAA" "B" "AA"  "C"
    4 "AAA" "C" "AA+" "B"
    5 "AAA" "B" "AAA" "A"
    end 
    
    gen ratingB = cond(inst1 == "B", rating1, rating2) 
    gen ratingAC = cond(inst1 == "B", rating2, rating1) 
    
    list 
    
         +-------------------------------------------------------------+
         | id   rating1   inst1   rating2   inst2   ratingB   ratingAC |
         |-------------------------------------------------------------|
      1. |  1       AA+       A       AA+       B       AA+        AA+ |
      2. |  2        AA       C       AAA       B       AAA         AA |
      3. |  3       AAA       B        AA       C       AAA         AA |
      4. |  4       AAA       C       AA+       B       AA+        AAA |
      5. |  5       AAA       B       AAA       A       AAA        AAA |
         +-------------------------------------------------------------+

    Comment


    • #3
      Dear Nick, Thanks. We are almost there. What I want is (according to your helpful codes)
      Code:
      gen ratingB = cond(inst1 == "B", rating1, rating2) 
      gen ratingO = cond(inst1 == "B", rating2, rating1) 
      gen instB = cond(inst1 == "B", inst1, inst2) 
      gen instO = cond(inst1 == "B", inst2, inst1)
      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment


      • #4
        I don't see the point of instB

        Comment

        Working...
        X