Announcement

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

  • Generate dummy variable with inlist

    Hi all,

    I have a string variable splti in data set that denotes the credit rating of a firm's debt. This rating variable can take values such as "AAA" "A" etc, see below.

    I want to have a dummy variable LowRate set equal to 1 if the debt rating is below investment grade , which includes ratings such as "BBB-", "CC","CCC","CCC+", "CCC-","D", "SD".

    I used the script below, but it is incomplete, I do not have the value of 0, only value of 1

    Code:
    gen LowRate=1 if inlist(splti,"BBB-", "CC","CCC","CCC+", "CCC-","D", "SD")
    
    replace LowRate=. if splti==""  // for firms having missing value for splti

    my sample has 50000 observations, but only a fraction has rating data as shown below
    Code:
    /*
              A |        296        6.80        6.80
             A+ |        131        3.01        9.82
             A- |        386        8.87       18.69
             AA |         29        0.67       19.36
            AA+ |          9        0.21       19.56
            AA- |         87        2.00       21.56
            AAA |          9        0.21       21.77
              B |        266        6.11       27.89
             B+ |        291        6.69       34.57
             B- |        139        3.20       37.77
             BB |        297        6.83       44.60
            BB+ |        288        6.62       51.22
            BB- |        334        7.68       58.90
            BBB |        560       12.87       71.77
           BBB+ |        540       12.41       84.18
           BBB- |        570       13.10       97.29
             CC |          6        0.14       97.43
            CCC |         18        0.41       97.84
           CCC+ |         69        1.59       99.43
           CCC- |          8        0.18       99.61
              D |         10        0.23       99.84
             SD |          7        0.16      100.00
    */

    could you help with the correct coding of LowRate?

    Thank you,

    Rochelle

  • #2

    Code:
     
     gen LowRate=1 if inlist(splti,"BBB-", "CC","CCC","CCC+", "CCC-","D", "SD")
    generates 1 if the string variable splti takes on any of those values and missing otherwise.


    Code:
     
     replace LowRate=. if splti==""  // for firms having missing value for splti
    does no harm, but it changes nothing as if the variable is missing then the indicator is already missing.

    If you want a (0, 1) indicator that is simply produced by

    Code:
     
     gen LowRate= inlist(splti,"BBB-", "CC","CCC","CCC+", "CCC-","D", "SD")
    If you want (0, 1, missing) indicator that is missing if the string variable then that is


    Code:
     
     gen LowRate= inlist(splti,"BBB-", "CC","CCC","CCC+", "CCC-","D", "SD") if !missing(splti)
    The basic ideas were all covered in https://www.stata.com/support/faqs/d...rue-and-false/

    A longer discussion by Clyde Schechter and friend was published in https://journals.sagepub.com/doi/abs...36867X19830921

    A way to think about this is: Where does your code instruct Stata ever to return a value of 0?

    Comment


    • #3
      Thank you Nick. This is very helpful !!!

      Comment

      Working...
      X