Announcement

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

  • Renaming Variables From Specific Number

    Consider the following dataset I am entasked with wrangling. As we can see, the variable label denotes the year of interest (i.e., 1213 == 2013, F1112 == 2012 .....). What I want to do, is rename each variable Privategiftsgrantsandcontr - AK to. for example, gifts2013, gifts2012 .... and so on and so forth so that I may reshape.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long UnitID str38 InstitutionName long(Privategiftsgrantsandcontr M U AC AK)
    217624 "Allen University"                        3031988  1639148   405383   529391  3238446
    219505 "American Baptist College"                 481083   264054  1028052   353950  1472818
    106306 "Arkansas Baptist College"                 904112   862613  6836677  1416019   912856
    217721 "Benedict College"                        1460963  1476220  1658713  1625984  2020700
    197993 "Bennett College"                         3896390  3363054  4032801  3479960  4117296
    132602 "Bethune-Cookman University"              4789913  4869150  4036027  4867202  5516953
    217873 "Claflin University"                      5263106  3181175  3448334  4255868  4568471
    138947 "Clark Atlanta University"                3507856  4231615  4302658  9986419  5875862
    217891 "Clinton College"                          271472   115939   263838   204890   250745
    158802 "Dillard University"                      2945583  4650897  3971186  4935423  5289934
    133526 "Edward Waters University"                2107076  5558981  3407204  4246513  1822849
    220181 "Fisk University"                         6104495  3305410  3693296  2802935  5282935
    133979 "Florida Memorial University"              707197   824997   677855   597684   814227
    232265 "Hampton University"                     21731742 33902113 18364937 13823516 14944561
    131520 "Howard University"                             0        0        0        0        0
    225575 "Huston-Tillotson University"             2880576  3035852  3103352  4736231  4126035
    140146 "Interdenominational Theological Center"  1395597  1675484  1953447  3509156  3767585
    225885 "Jarvis Christian University"             1087578   962409  1138276  1474537   957333
    198756 "Johnson C Smith University"              5203083  7996780  6782318 10248899  5675112
    220598 "Lane College"                             835094   722574   779911   628548   661577
    220604 "Le Moyne-Owen College"                   2455522  3152670  1483070  2148206  1622636
    198862 "Livingstone College"                     3074472  3636457  3353768  3732147  2488071
    220792 "Meharry Medical College"                17424872 18493334 15528027 15713945 26101127
    101675 "Miles College"                           2465662  1740532  1490499  2123410  3698748
    140553 "Morehouse College"                      16346215 11275865  9218526 19115842  7675308
    140562 "Morehouse School of Medicine"            2808914  8980104  6024024 10482009 13744808
    218399 "Morris College"                          1062315  1425056  1083996 11122373  1443575
    101912 "Oakwood University"                      3390937  2766162  4555470  6325535 11986302
    140720 "Paine College"                           2006575  3189748  2602788  2109864  2288466
    227429 "Paul Quinn College"                      1241262   716247   779876  3968532  2356395
    107600 "Philander Smith College"                 4991856  4444599  4933355  4114283  4699443
    176318 "Rust College"                            1959808  2485395  2159984  1761718  2168689
    199582 "Saint Augustine's University"            2308612  2452721  1436982  1565448  2076257
    102058 "Selma University"                         420000   435422   730259   730259   613250
    199643 "Shaw University"                         2499730  3158950  3006536  2745859  4003413
    107840 "Shorter College"                               0   809729        .        .        .
    461759 "Simmons College of Kentucky"              517040   293140   575666        .        .
    228486 "Southwestern Christian College"          1361289  1428224  1357743  1534710  2409555
    141060 "Spelman College"                        15713302 26662606 15335358 11851747 11828286
    102270 "Stillman College"                        1561706  1335399  1727528  1442610  1700790
    102298 "Talladega College"                       1915882  2842454  2309668   962949  2079475
    228884 "Texas College"                            368061   792038   522825   556063   549993
    176406 "Tougaloo College"                        3545880  3208009  5826812  4970564  4140527
    102377 "Tuskegee University"                    11775017 11442097  9267472  7958837  7531162
    234164 "Virginia Union University"               2367951  3418917  2212660  2329489  4170017
    234137 "Virginia University of Lynchburg"         214716   425070   259026   259026   389333
    218919 "Voorhees College"                        1073191   955291   970564   608435   766623
    206491 "Wilberforce University"                  1238891  1187974  1268215  1952767  2128959
    229887 "Wiley College"                           4926259  4386707  2681368  1434364  4077656
    160904 "Xavier University of Louisiana"         10568757 10654871  7912883  7392805 13458250
    end
    label var UnitID "UnitID"
    label var InstitutionName "Institution Name"
    label var Privategiftsgrantsandcontr "Private gifts  grants  and contracts - Total (F1213_F2_RV)"
    label var M "Private gifts  grants  and contracts - Total (F1112_F2_RV)"
    label var U "Private gifts  grants  and contracts - Total (F1011_F2_RV)"
    label var AC "Private gifts  grants  and contracts - Total (F0910_F2_RV)"
    label var AK "Private gifts  grants  and contracts - Total (F0809_F2_RV)"
    What I've done so far is
    Code:
    rename Privategiftsgrantsandcontr-AK gifts#, addnumber dryrun
    And this gives me gifts1, gifts2... Is there a way to start the renumbering from a specific number such that it is applied to every following variable in the varlist, so that I don't need to add 1998 afterwards to the resulting year column in order to get the actual years? In other words, I want
    Code:
    rename (Privategiftsgrantsandcontr M U) (gifts2013 gifts2012 gifts2011)
    and so on. How might I do this?


    EDIT: Welp, the way you'd do it is
    Code:
    rename Privategiftsgrantsandcontr-AK gifts#, addnumber(1998)
    Last edited by Jared Greathouse; 19 Jan 2023, 11:55.

  • #2
    There may well be a more elegant way to do this, but the following works in your example:
    Code:
    ds UnitID InstitutionName, not
    local current_order `r(varlist)'
    
    local revised_order
    foreach v of local current_order {
        local revised_order `v' `revised_order'
    }
    
    local starter = 2014 - `:word count `revised_order''
    
    rename (`revised_order') gifts#, addnumber(`starter')

    Comment

    Working...
    X