Announcement

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

  • Extract strings from value labels


    I have a variable (crds) that is numeric but has string value labels (AAA, AA+, AA, AA- .....). I would like to extract the string labels when I use the "levelsof" command. However, the command returns the numeric values (1, 2, 3,....).
    Ultimately, I want the local macro 'rates' to contain just the labels that exist for that given year.

    Code:
    levelsof fyear, local(years)
    foreach x of local years  {
    levelsof crds if fyear==`x', local(rates)
       oprobit crds size profitability if fyear==`x'
      if c(rc) == 0 {    // SUCCESSFUL REGRESSION, PREDICT
    predict `rates' if fyear==`x',p
    egen double m2= rowmax( `rates' )
    gen wanted=""
    foreach y of local rates {
    replace wanted = "`var'" if m2==`y'
    }
    drop m2
    drop `rates'
    
    
    
      }
              else if !inlist(c(rc), 2000, 2001) {    // UNANTICIPATED PROBLEM -- COMPLAIN AND HALT!
                display as error `"Unexpected error performing regression with FYear == `x'"'
                exit c(rc)
            }
            
            
              
    
    }

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float crds double fyear float(size profitability)
     8 1986  5.697738    .14509776
     8 1987  5.851168    .14861053
     8 1988  6.007239    .15657243
     8 1989  6.097794    .13145955
     8 1990  6.145348    .08674178
     8 1991  6.046561     .0698235
     8 1992   5.94746    .04133795
     8 1993  6.010664    .05976706
     8 1994  6.112343    .05851647
     7 1995  6.224539     .0761882
     7 1996  6.378983    .09795681
     7 1997  6.662012    .12220158
     8 1998  6.822237    .11539774
     8 1999  6.931797    .09724069
     8 2000  6.773372    .06179504
     8 2001  6.459468   .006712222
     7 2002  6.407436   .005030985
     4 2003   6.47998     .0303093
     4 2004    6.6172    .04768699
     4 2005  6.799372    .08556738
     4 2006  6.967126     .0885843
     5 2007  7.233397    .12042527
     5 2008  7.261208    .09216452
     5 2009  7.209452    .06926624
     5 2010  7.481996     .0912806
     5 2011  7.637475    .08355799
     5 2012  7.681145    .06221383
     5 2013  7.618251    .06673218
     5 2014   7.37419 -.0039099795
     6 2015  7.416138    .04363036
     6 2016  7.477378    .05353304
     7 1986   6.00607    .19869854
     7 1987  6.191094    .15719526
     5 1990  5.618432    .11621958
     5 1991  5.671741    .04589111
     5 1992  5.687355    .07940808
     4 1998  6.404541    .10856099
     4 1999  6.596385    .10729799
     4 2000  6.803277    .10833464
     4 2001  6.882427    .05339551
     4 2002  7.115389    .05970649
     4 2003  7.168029    .04595145
     3 2004  7.200037   .028598255
     2 2005  6.316473    .04790547
     2 2006  6.482844    .05916102
     3 2007  6.582613  -.005955315
     8 2000  7.769051    .09020176
     3 1989  6.047755    .04169867
     3 1990  6.105851    .05271026
     3 1991  6.007547   .029315056
     3 1992  6.002565     .0772308
     3 1993  6.375796    .16235144
     4 1994  6.336231    .10179821
     4 1995   6.48901    .13495636
     4 1996  6.619536    .12488365
     4 1997  6.741458      .051216
     4 1998  6.934125   .016725728
     3 1999  7.062142 -.0018550215
     3 2000  7.102335 -.0020507989
     1 2001  7.201548     .0519506
     1 2002  7.490848    .05680793
     2 2003  7.485951   .071463525
    10 1986  8.702539    .06399575
    11 1987  8.881555     .0612468
    11 1988  9.085264    .09574577
    11 1989  9.257186     .0765243
    11 1990  9.369018   .011399782
    10 1991  9.463974  .0003744309
     9 1992  9.574706 -.0015424482
     8 1993   9.66148     .0307388
     6 1994   9.68887   .066438995
     6 1995  9.735661    .07944165
     6 1996  9.784309    .09536715
     6 1997  9.829303    .09396497
     7 1998  9.862926     .1117858
     7 1999  9.783013    .05183159
     7 2000  9.888526    .05665873
     7 2001  9.850245   -.07095716
     5 2002  9.758404   -.07983922
     4 2003  9.766521   -.02626623
     1 2004  9.833333 -.0045346064
     1 2005  9.938469    .00354499
     1 2006 10.024067   .035938296
     2 2007 10.038717   .035306226
     2 2008 10.076012    -.0200553
     1 2009  9.899329   -.02959285
     1 2010 10.006495    .01210787
     1 2011 10.086725  -.011399873
     4 1994  7.769495    .04681891
     4 1995  7.811076   -.10509934
     3 1996  7.651426    -.2053701
     3 1986  3.988095      .088161
     3 1987 4.1050043    .10455878
     3 1988  4.462396    .06977539
     2 1986   5.59917    .05506955
     5 1986  5.274839    .02453905
     5 1987   5.60539    .12987158
     5 1988  6.013566    .14057508
     5 1989  6.022285    .13289216
     6 1992   7.43227      .084452
     7 1993  7.449229    .08250108
     7 1994   7.42977     .0767264
     7 1995  7.420458     .0813957
     7 1996   7.50536     .0777269
     8 1997  7.598413    .07976581
     8 1998  7.664152    .08278737
     8 1999  7.792907    .08480813
     8 2000  8.213429    .10228802
     8 2001  8.423184    .09436464
     8 2002  7.877503    .06465338
     8 2003   7.94373     .0572115
     8 2004  7.972371    .05308714
     8 2005  8.002344    .06606726
     7 2006  8.132045    .05465773
     7 2007  8.167244      .054055
     7 2008    8.1218    .04718602
     7 2009  8.100799    .04993299
     7 2010    8.0906    .06130373
     7 2011  8.083755    .06038388
     8 2012  8.102224    .06496483
     9 2013   8.14747    .06325466
    10 2014  8.158125    .06005336
    10 2015  8.159215    .05970588
    10 2016  8.160142     .0569583
    10 2017  8.179003    .05838617
    15 1986  8.244763    .25332078
    15 1987 8.3866005     .2601585
    15 1988  8.504511    .25127295
    15 1989  8.590398     .2530743
    15 1990  8.725618    .28980768
    15 1991  8.835877    .27988276
    15 1992  8.968513     .2783274
    16 1993  9.036921    .28207743
    16 1994  9.122166    .27888247
    16 1995  9.211559     .2795614
    16 1996  9.306873    .28196955
    16 1997  9.382903    .25620425
    16 1998   9.43171    .25851133
    16 1999  9.486276    .25223374
    16 2000  9.528497    .22542037
    16 2001  9.698015    .23149076
    14 2002  9.780453    .16916077
    14 2003  9.887386    .17167874
    14 2004  9.887359     .1667703
    14 2005 10.011795     .1680855
    14 2006 10.020218     .1667817
    14 2007 10.162548     .1526762
    14 2008  10.29308     .1630938
    14 2009 10.334124    .16275036
    14 2010 10.467855     .1533632
    14 2011 10.567495    .15129676
    14 2012 10.593477    .16230954
    12 2013  9.991864    .03977098
    12 2014  9.915762   .068144254
    12 2015  9.923535    .08169594
    12 2016  9.945253    .08781245
     8 2017 10.217934    .07587438
     5 1993  7.533159   -.02289857
     5 1994  7.654917   .015514977
     7 1997  8.228711    .04761905
     7 1998  8.114025   .018268468
     7 1999  8.303752    .02355523
     7 2000  8.644178    .14264792
     7 2001  8.704834    .08387383
     7 2002    8.5413   .015546255
     7 2003  8.473451   -.01527765
     6 2004  8.665786   .017378498
     4 2005  8.583355    .01348728
     3 2006   8.48694   .016907012
     3 1998  5.547717    .11485256
     4 1999  5.628297    .06827657
     4 2000  5.414161   .006924207
     4 2001  5.292586     -.039039
     4 2004  7.248403     .1905117
     4 2005  7.291656   .011649908
     4 2006  7.321857    .05157535
     4 2007  7.971823    .26735127
     4 1986 4.1470003    .12500253
     3 1986  6.448859   -.08427946
     3 1987  6.904831   -.01930785
     3 1988  7.026298   .028231615
     3 1989  7.007244   .035542335
     3 1990  6.965309   -.04554465
     3 1991  7.112041    .09819266
     3 1992  7.322834     .2089749
     5 1993  7.407487    .21065815
     3 1994  7.666062     .2659811
     4 1995  7.795533     .1424037
     5 1996  7.577132    -.0806956
     4 1997   7.76488   -.02882189
     4 1998  7.840762   -.04655175
     2 1999  7.957739   -.06646794
     2 2000  8.443372    .20301445
     2 2001  8.266615    .00657936
     2 2002  7.899906    -.1510137
     1 2003   8.16598   -.04400588
     1 2004   8.51748    .03203467
     2 2005  8.673782   .031571694
     3 2006  8.639234    .05502363
     3 2007  8.701679   -.09211227
    end
    label values crds mylabel1
    label def mylabel1 1 "B-", modify
    label def mylabel1 2 "B", modify
    label def mylabel1 3 "B+", modify
    label def mylabel1 4 "BB-", modify
    label def mylabel1 5 "BB", modify
    label def mylabel1 6 "BB+", modify
    label def mylabel1 7 "BBB-", modify
    label def mylabel1 8 "BBB", modify
    label def mylabel1 9 "BBB+", modify
    label def mylabel1 10 "A-", modify
    label def mylabel1 11 "A", modify
    label def mylabel1 12 "A+", modify
    label def mylabel1 14 "AA", modify
    label def mylabel1 15 "AA+", modify
    label def mylabel1 16 "AAA", modify

  • #2
    Code:
    levelsof fyear, local(years)
    foreach x of local years {
        levelsof crds if fyear==`x', local(n_rates)
        local rates
        foreach r of local n_rates {
            local rates `rates' `:label (crds) `r''
        }
        display `"`rates'"'
        // ETC>
    }
    Added: It just dawns on me it might be simpler and more transparent to just do this:

    Code:
    decode crds, gen(alpha_crds)
    levelsof fyear, local(years)
    foreach x of local years {
        levelsof alpha_crds if fyear==`x', local(rates)
        display `"`rates'"'
        // ETC.
    }
    Unless your data set is huge and you are tight on memory, this is probably better than my first solution.
    Last edited by Clyde Schechter; 10 Jul 2022, 12:50.

    Comment


    • #3

      Thank you Clyde. Your second solution seems to be moving me in the right direction. One last problem: The strings contained in local macro 'rates' are surrounded by quotation marks. Is there any way to remove them?

      Comment


      • #4
        The output of
        Code:
        help levelsof
        suggests that adding the clean option to the levelsof command will do what you want.

        Comment


        • #5
          Thank you both. Your recommendations worked.

          Comment

          Working...
          X