Announcement

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

  • Repeated string variables

    Hi All,

    This is my first post. I am a new user of Stata. I have this unique problem. As from the output below, credit rating of securities are getting repeated, throwing the result in absolute jeopardy. The ratings "AAA", "AA+", "AA-" and "BBB+" are getting repeated. I am using Stata 14.2.

    Earlier there were more repeated instances. I tried using the command: replace rating = strrtrim(rating). (23 real changes made). Now, when I am repeating the command, I am getting the message (0 real changes made). However, there are still repeats.

    Please help.

    Thanks in advance.



    | YEAR
    rating | 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 | Total
    -----------+--------------------------------------------------------------------------------------------------------------+----------
    AA+ | 0 0 0 0 0 0 0 1 0 0 | 1
    AA- | 0 0 0 0 0 0 9 3 4 0 | 16
    AAA | 0 0 0 0 0 0 0 0 4 0 | 4
    BBB+ | 0 0 0 0 0 0 0 0 0 4 | 4
    A | 16 84 112 133 171 171 145 131 183 197 | 1,343
    A+ | 12 112 132 170 180 232 222 198 175 183 | 1,616
    A- | 8 62 147 271 188 232 189 161 270 262 | 1,790
    AA | 15 60 101 104 121 173 133 124 165 152 | 1,148
    AA+ | 37 74 35 38 95 124 119 130 150 170 | 972
    AA- | 28 80 138 78 175 181 150 147 156 209 | 1,342
    AAA | 109 222 179 211 242 223 206 244 308 302 | 2,246
    AAA+ | 0 0 0 1 1 0 14 0 0 0 | 16
    B | 0 0 12 24 20 15 34 38 26 41 | 210
    B+ | 0 0 7 7 13 24 19 38 59 47 | 214
    B- | 0 0 0 9 18 18 12 4 16 17 | 94
    BB | 0 0 17 34 34 40 78 82 72 91 | 448
    BB+ | 0 0 8 52 75 90 146 141 112 99 | 723
    BB- | 0 0 11 8 20 28 46 47 55 36 | 251
    BBB | 0 8 73 151 152 159 186 186 163 166 | 1,244
    BBB+ | 4 40 90 128 145 187 259 209 193 174 | 1,429
    BBB- | 0 0 66 107 169 197 183 172 210 206 | 1,310
    C | 0 0 0 10 12 17 27 24 30 20 | 140
    D | 0 8 15 23 65 73 138 133 157 181 | 793
    -----------+--------------------------------------------------------------------------------------------------------------+----------
    Total | 229 750 1,143 1,559 1,896 2,184 2,315 2,213 2,508 2,557 | 17,354
    Last edited by Avik Mukherjee; 17 Jul 2017, 11:39.

  • #2
    Do read the FAQ for excellent advice on how to ask clear questions and how to post code or Stata output in ways that enhance their readability. A sample of your data that reproduces this problem would also have been helpful here (use -dataex-; see FAQ #12 for instructions).

    That said, I'm guessing that the above output is what you got from running -tab rating year- and you are perplexed that you are seeing multiple rows that have the same stub. This can only happen if the actual contents of variable rating are not what they look like to the human eye. Most likely the values there are "contaminated" with non-printing characters, so while the first instance of AAA looks just like the second one, they in fact differ by containing additional characters that do not show up in listings and displays. Here's what I suggest to help diagnose the problem.

    Code:
    charlist rating
    return list
    If your data contain only what you see the output will look like this:

    Code:
    . charlist rating
    +-ABCD
    
    . return list
    
    macros:
                  r(chars) : "+-ABCD"
               r(sepchars) : "+ - A B C D "
                  r(ascii) : "43 45 65 66 67 68 "
    Now, non-printing charcters will not show up in the -charlist- output, and they also won't show up in r(chars) or r(sepchars) because, well, they do not print. But you will see additional numbers in r(ascii) that are not present in what I show just above. (Well, you might also see 32, which is a blank space, but which should have been removed by your strtrim() activities.) Whatever other numbers show up there are extraneous non-printing characters and you will need to remove them from the variable using a series of -replace rating = subinstr(rating,...)- commands.

    Comment


    • #3
      Clyde gives excellent advice. Note that you must install charlist before you can use it.

      Code:
      ssc inst charlist

      Comment


      • #4
        Thank you so much for your advice. I have followed your guidance and have overcome the problem. Thank you so much.

        Comment

        Working...
        X