Announcement

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

  • Indexing Panel Units to Their Numeric IDs

    Hey everyone. Say we have a dataset like this

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(time gdp id)
     1        .062  9
     2        .059  9
     3        .058  9
     4        .062  9
     5        .079  9
     6        .068  9
     7        .046  9
     8        .052  9
     9        .037  9
    10        .029  9
    11        .012  9
    12        .015  9
    13        .025  9
    14        .036  9
    15        .047  9
    16        .059  9
    17        .058  9
    18        .072  9
    19        .061  9
    20        .014  9
    21       -.032  9
    22       -.061  9
    23       -.081  9
    24       -.065  9
    25       -.029  9
    26        .005  9
    27        .039  9
    28        .083  9
    29        .107  9
    30        .075  9
    31        .076  9
    32        .063  9
    33        .027  9
    34        .015  9
    35       -.001  9
    36       -.017  9
    37        -.01  9
    38        .005  9
    39        .028  9
    40        .048  9
    41        .041  9
    42       -.009  9
    43        .038  9
    44        .047  9
    45        .077  9
    46         .12  9
    47        .066  9
    48        .079  9
    49        .062  9
    50        .071  9
    51        .081  9
    52        .069  9
    53         .09  9
    54        .062  9
    55        .064  9
    56        .066  9
    57        .055  9
    58        .062  9
    59        .068  9
    60        .069  9
    61        .073  9
     1   .06402444 10
     2    .0660682 10
     3   .05795939 10
     4   .06236489 10
     5   .04974312 10
     6   .07198844 10
     7  .069357224 10
     8   .07034696 10
     9   .10377815 10
    10   .08325198 10
    11   .07087339 10
    12   .07690515 10
    13   .03886444 10
    14    .0663798 10
    15   .08834065 10
    16   .12930773 10
    17   .13033853 10
    18   .10046565 10
    19    .1139613 10
    20  .071993686 10
    21   .12910426 10
    22 -.003199045 10
    23  -.08540604 10
    24  -.16064288 10
    25   -.1959457 10
    26  -.07136205 10
    27 -.014795235 10
    28   .05213065 10
    29   .18423697 10
    30    .2027994 10
    31   .20656615 10
    32    .2067867 10
    33   .11551335 10
    34   .12611313 10
    35   .06446089 10
    36   .03153899 10
    37 -.014847404 10
    38   -.0370046 10
    39  .003776353 10
    end
    label values id id
    label def id 9 "hongkong", modify
    label def id 10 "indonesia", modify
    Say this is in our default frame. In another frame, called 'reshaped', we have this


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(gdp time) byte reshaped float(gdp9 gdp19 gdp20 gdp23 gdp18 gdp15 gdp13 gdp10 gdp17 gdp14)
     .062  1  1  .062 -.004380854   .08714474         .08  .004946546    .0437463   .0585864   .06402444   .04724391   .08593838
     .059  2  2  .059  .016635614   .11807464         .08   .02074335  .012290427  .06952104    .0660682   .03875869   .13118862
     .058  3  3  .058   .03150428   .11112953         .08   .03887128  .004462324   .0816463   .05795939   .08991753    .1096664
     .062  4  4  .062  .034007482    .1253237         .08   .05340292  .015492945  .08553336   .06236489   .06975085   .07580058
     .079  5  5  .079   .04934406   .13070899   .11250878   .03100154  .034447584   .0859223   .04974312   .06019911   .04914714
     .068  6  6  .068   .05912968   .10098694    .0926132   .05131298   .06391147  .08841456   .07198844   .06255518   .06117326
     .046  7  7  .046   .06286961    .1159838   .05037503  .024323747   .06432155  .09279576  .069357224   .04292477   .10111016
     .052  8  8  .052   .06209084   .09204701   .09395156   .02752356  .066942185  .11746117   .07034696   .04760897   .13497774
     .037  9  9  .037   .05222844   .06601266   .10677946   .06527195   .04341815  .11538751   .10377815  .022149924   .13036306
     .029 10 10  .029   .04167756   .07901964   .11203063   .03922234  -.05994282  .12188892   .08325198  .023302693   .11914025
     .012 11 11  .012  .032848697   .08751852   .08397228   .04436925   -.0790153  .13227351   .07087339   .03045487   .06508452
     .015 12 12  .015   .03655749   .07992998   .04567892   .03511129 -.067619264  .09235092   .07690515   .03069211  .074478604
     .025 13 13  .025   .03269173   .11170594  .005896824  .064231016  -.05592186    .094772   .03886444   .04089288   .10384238
     .036 14 14  .036   .04689996   .08751257   .04958628   .06247909  .022376847  .07235756    .0663798   .03584749   .09273203
     .047 15 15  .047   .07427855   .04366945   .06057989   .08666218   .05411856  .05540767   .08834065   .03766511   .09690417
     .059 16 16  .059    .0286998   .06047118   .04325939   .09309184   .05555995  .05729404   .12930773  .025593406    .0958561
     .058 17 17  .058   .05247467   .05583632 -.006867101   .05371245   .02802843  .03170797   .13033853  .005592703  .068247624
     .072 18 18  .072   .05160475   .08987325  -.02481162    .0739128   .05659474  .05336402   .10046565  .034571752   .06503466
     .061 19 19  .061   .03768199    .1043166  -.03598934   .04958734   .03932801  .04771919    .1139613   .03353216   .07327511
     .014 20 20  .014   .05894723   .06933789  -.04505816    .0479107   .04183675  .05488339  .071993686  .018211482   .10505208
    -.032 21 21 -.032  .018309548   .02608244  -.04164755  .007999274   .07194273 -.02829138   .12910426  .018747104   .04308073
    -.061 22 22 -.061  .010905336 -.016480034  -.14118274  .003761194   .04357136 -.09490907 -.003199045 -.017352613   -.0204842
    -.081 23 23 -.081  .008935778   -.0428804   -.1396016 -.007978701   .04992892  -.1081081  -.08540604 -.019380176  -.05341627
    -.065 24 24 -.065 -.009510532  -.02025388  -.08158585  -.01652179   .01218324 -.11455178  -.16064288   .02692859  -.14588992
    -.029 25 25 -.029   .01564014  .031149296 -.068189315  .003396671 -.003817665 .007458419   -.1959457   .03889057  -.08526126
     .005 26 26  .005   .04109848   .06821197  -.00336661   .02232363  .024232743  .07264826  -.07136205   .05541473  .022742623
     .039 27 27  .039   .05302346   .08971149   .04479968   .08477581   .02418166   .1173992 -.014795235   .06664848   .05839044
     .083 28 28  .083   .06136366   .08718824  .024073415   .12395878   .05324135  .12592347   .05213065   .04101127   .12636611
     .107 29 29  .107   .11798558   .09585237   .05079921    .1856845   .10371514  .10581615   .18423697   .06054202     .171468
     .075 30 30  .075    .1099192   .08674224   .05483068     .158934   .09631544  .08233732    .2027994  .034375284   .12932135
     .076 31 31  .076   .10786364   .10383152   .02973485   .13492529   .09121797  .05185413   .20656615    .0207593   .10589152
     .063 32 32  .063    .0923765   .09664023   .04196519    .1146906   .06044572 .028516136    .2067867   .03254855   .06781295
     .027 33 33  .027   .02766804   .04488511  .028546477   .04759685   .02244058  .03457192   .11551335  .015890202 -.006077476
     .015 34 34  .015   .02614486 -.007359081   .03025852   .04178948 -.006739162 .033580326   .12611313   .05496934  -.03904002
    -.001 35 35 -.001   .03024964  -.06804513   .03049684 -.006837184  -.01805425 .029444976   .06446089  .036681067  -.04859371
    -.017 36 36 -.017   .03725115   -.0643221   .01451582  -.04528562  -.02201885 .032666683   .03153899   .04987184  -.06475644
     -.01 37 37  -.01   .03923289 -.005363067   .04803302  -.04280226 -.028404344  .06034927 -.014847404   .05044868  -.01039387
     .005 38 38  .005   .05525857   .04738288   .05030452  -.02080591   .03831596 .067838214   -.0370046  .011498967   .04484413
     .028 39 39  .028   .01908033    .0632176   .05393544 -.014044139   .03606899  .06392864  .003776353   .04828115    .0825247
     .048 40 40  .048   .07434293   .05741051   .06194266  .006212187   .05542101  .08295108  .002348844  .015707554   .12176775
     .041 41 41  .041  .064691864   .03057485  .062935114   .01405916   .08698975  .02460097  .029827133   .02053765    .1204974
    -.009 42 42 -.009   .04307844  -.02194461   .05503646 -.012477424  .027076706 .016111843   .02110755    .0400604   .06602504
     .038 43 43  .038    .0781006  .036523588    .0624509  .025473913   .03905525  .02175231  .025726397   .04220926   .05005969
     .047 44 44  .047   .04729546   .07487105   .07594121  .030852726   .05243143  .02510994   .02373941   .05431711   .07801466
     .077 45 45  .077   .06439665   .08466503   .05409686  .069413625   .05931818  .04305736   .02590298  .066612795    .0953926
      .12 46 46   .12   .07929841   .11738709    .0704565     .089488   .09075832  .04811755   .05061197   .06818556   .13319568
     .066 47 47  .066   .05873044   .07103565   .06435446    .0820782    .0913188  .03926154   .06142804   .05111898   .13531557
     .079 48 48  .079   .04461386   .06638691   .07069945   .10152618    .0782785 .020118633   .09638824   .04093498   .09613124
     .062 49 49  .062   .03236994  .036235485   .05057577   .08256879   .04635276 .001002505    .0940804   .01501046   .07891574
     .071 50 50  .071   .03732966   .05912498   .03845144   .08819184   .02397523 .002856359   .10347588   .02387351    .0545625
     .081 51 51  .081  .037029423   .07894655   .04996326   .10685124  .031832933 .015429093   .10469954  .011341385   .06851774
     .069 52 52  .069   .04550384     .080031  .035448156    .1002058   .03841446  .02824962   .07281439  .008914476  .063078865
      .09 53 53   .09   .04600557   .09651806   .06622412   .13118097   .07014684 .033213116   .05308633  .018647296   .11299311
     .062 54 54  .062  .036119644    .0767207   .05481347    .0967139   .10033782  .02525387   .04339136 -.009260945   .11634029
     .064 55 55  .064    .0430906   .06774659   .04864842   .07105601   .08364902 .019649645   .05418941  .011500795    .1102278
     .066 56 56  .066   .04839084    .0638802    .0421417   .03430307   .05882088 .014679614   .08088535  .036755715   .10011628
     .055 57 57  .055   .06557317   .06309225   .04984603   .00802016   .03822222  .03006762    .0952969   .03946248   .03988392
     .062 58 58  .062   .08189134   .10137684   .05119674  .033556234   .02222606  .03789796       .1109   .05829326    .0802763
     .068 59 59  .068   .06435867   .10556202   .05012558   .03977032   .03116201  .03938037       .1101   .05114701   .09336115
     .069 60 60  .069    .0654698   .06973857   .07958734   .08985502   .06271445 .035359476       .1107   .04590492    .1517392
     .073 61 61  .073   .04794496   .07933874   .04914357   .09156769   .05649333  .02846235       .1334  .031214973   .16703407
    end

    We can see per the first dataex that hongkong and indonesia have value labels attached to them. Suppose now we have a macro called `best_model', which, when we print it,
    Code:
    di "`best_model'"
    looks like "gdp9 gdp10" (without the quotations). My question is: how do we make another macro that consists of the strings that are indexed to the values? What I'd expect is a macro, say "selected" which, when printed, looks like "hongkong, indonesia"?


    I think the key would be to extract the numeric part of the variable name (that is, 9, 10) and then do something like
    Code:
    loc selunit: display "`: label (id) 9"
    loc selunit: display "`: label (id) 10"
    , looping over the elements of `best_model' to build the list of units that are selected.




    EDIT: I think this is a place to start from

    Code:
    local n = ustrregexra("`best_model'","\D"," ")
    display "`best_model' becomes `n'"
    Last edited by Jared Greathouse; 12 Jul 2024, 08:03.

  • #2
    The obstacle here is that -reshape wide- obliterates the id variable, and its label disappears with it. So you have to save the label before you -reshape- and then re-create it after. Like this:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(time gdp id)
     1        .062  9
     2        .059  9
     3        .058  9
     4        .062  9
     5        .079  9
     6        .068  9
     7        .046  9
     8        .052  9
     9        .037  9
    10        .029  9
    11        .012  9
    12        .015  9
    13        .025  9
    14        .036  9
    15        .047  9
    16        .059  9
    17        .058  9
    18        .072  9
    19        .061  9
    20        .014  9
    21       -.032  9
    22       -.061  9
    23       -.081  9
    24       -.065  9
    25       -.029  9
    26        .005  9
    27        .039  9
    28        .083  9
    29        .107  9
    30        .075  9
    31        .076  9
    32        .063  9
    33        .027  9
    34        .015  9
    35       -.001  9
    36       -.017  9
    37        -.01  9
    38        .005  9
    39        .028  9
    40        .048  9
    41        .041  9
    42       -.009  9
    43        .038  9
    44        .047  9
    45        .077  9
    46         .12  9
    47        .066  9
    48        .079  9
    49        .062  9
    50        .071  9
    51        .081  9
    52        .069  9
    53         .09  9
    54        .062  9
    55        .064  9
    56        .066  9
    57        .055  9
    58        .062  9
    59        .068  9
    60        .069  9
    61        .073  9
     1   .06402444 10
     2    .0660682 10
     3   .05795939 10
     4   .06236489 10
     5   .04974312 10
     6   .07198844 10
     7  .069357224 10
     8   .07034696 10
     9   .10377815 10
    10   .08325198 10
    11   .07087339 10
    12   .07690515 10
    13   .03886444 10
    14    .0663798 10
    15   .08834065 10
    16   .12930773 10
    17   .13033853 10
    18   .10046565 10
    19    .1139613 10
    20  .071993686 10
    21   .12910426 10
    22 -.003199045 10
    23  -.08540604 10
    24  -.16064288 10
    25   -.1959457 10
    26  -.07136205 10
    27 -.014795235 10
    28   .05213065 10
    29   .18423697 10
    30    .2027994 10
    31   .20656615 10
    32    .2067867 10
    33   .11551335 10
    34   .12611313 10
    35   .06446089 10
    36   .03153899 10
    37 -.014847404 10
    38   -.0370046 10
    39  .003776353 10
    end
    label values id id
    label def id 9 "hongkong", modify
    label def id 10 "indonesia", modify
    
    tempfile labeler
    label save id using `labeler'
    
    local best_model gdp9 gdp10
    
    reshape wide gdp, i(time) j(id)
    run `labeler'
    
    local wanted
    foreach b of local best_model {
        local bb: subinstr local b "gdp" ""
        local wanted `wanted' `:label id `bb'' // N.B.  NO PARENTHESES AROUND id
    }
    
    display `"`wanted'"'

    Comment

    Working...
    X