Announcement

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

  • Creating a new Time variable out of another variable

    Hello Stata Community;

    I have this kinda of data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 country_code str35 country float(gii_1995 gii_2000 gii_2005 gii_2010 gii_2011 gii_2012 gii_2013 gii_2014 gii_2015)
    "AFG" "Afghanistan"                           .    . .745 .751 .743 .734 .724 .714 .702
    "AGO" "Angola"                                .    .    .    .    .    .    . .537 .534
    "ALB" "Albania"                               . .334 .306 .242 .249  .23 .221 .217 .207
    "AND" "Andorra"                               .    .    .    .    .    .    .    .    .
    "ARE" "United Arab Emirates"               .635 .609 .592 .233 .236 .226 .215 .203 .175
    "ARG" "Argentina"                          .427 .417 .372 .358 .354 .351 .348 .345    .
    "ARM" "Armenia"                            .479 .473 .392 .342 .337  .32 .307 .308 .309
    "ATG" "Antigua and Barbuda"                   .    .    .    .    .    .    .    .    .
    "AUS" "Australia"                           .18  .16 .139 .138 .132 .127 .119 .116  .11
    "AUT" "Austria"                            .186  .15 .118 .111 .106 .102 .093  .09 .089
    "AZE" "Azerbaijan"                            .    . .341 .313 .314 .315 .318 .321 .316
    "BDI" "Burundi"                               . .624 .567 .532 .528 .523 .519 .515 .509
    "BEL" "Belgium"                            .162 .122 .104 .091 .086 .081 .074 .063 .057
    "BEN" "Benin"                              .681 .673 .655 .625 .633 .628 .624 .619 .624
    "BFA" "Burkina Faso"                          .    . .641 .614  .61 .605 .601    . .619
    "BGD" "Bangladesh"                         .708 .683 .638 .592 .591 .585 .579  .57 .562
    "BGR" "Bulgaria"                            .37 .329 .253 .244 .243 .229 .217 .236 .227
    "BHR" "Bahrain"                               .    . .321 .247 .221 .234  .23 .226 .236
    "BHS" "Bahamas"                               . .403 .363  .38 .378 .379 .376 .373 .367
    "BIH" "Bosnia and Herzegovina"                .    .    . .228 .201 .193 .183    . .168
    "BLR" "Belarus"                               .    .    .  .15 .149 .154 .153 .153 .153
    "BLZ" "Belize"                             .554 .516 .484 .459 .455  .44 .435  .43 .423
    "BOL" "Bolivia (Plurinational State of)"      . .587  .55 .491 .487 .483 .477  .44  .44
    "BRA" "Brazil"                             .532  .51 .474 .456 .452 .448 .448 .442 .439
    "BRB" "Barbados"                           .392 .359 .351 .327 .323 .319 .306 .301 .306
    "BRN" "Brunei Darussalam"                     .    .    .    .    .    .    .    .    .
    "BTN" "Bhutan"                                .    .    .    .    . .507 .536 .512 .492
    "BWA" "Botswana"                           .568 .519 .512 .506 .504   .5 .496 .483 .481
    "CAF" "Central African Republic"           .743 .711 .693 .693  .68 .678    .    .    .
    "CAN" "Canada"                             .184    . .139 .132 .124 .121 .119 .116 .114
    "CHE" "Switzerland"                        .117 .094 .073 .062 .061 .058 .052 .049 .047
    "CHL" "Chile"                              .483 .433 .377 .342 .331 .325 .309 .303   .3
    "CHN" "China"                                 . .269 .227 .194 .189 .186 .178 .175 .172
    "CIV" "Côte d'Ivoire"                     .707    . .688 .678 .667 .667 .664 .665 .664
    "CMR" "Cameroon"                           .706 .696 .654 .617 .614 .611 .577 .575 .572
    "COD" "Congo (Democratic Republic of the)"    .    . .654 .661    . .655 .626 .637 .641
    "COG" "Congo"                                 . .623 .625 .613 .608 .605 .602  .59 .588
    "COL" "Colombia"                           .524 .501 .494 .468 .466 .463 .461 .436 .433
    "COM" "Comoros"                               .    .    .    .    .    .    .    .    .
    "CPV" "Cabo Verde"                            .    .    .    .    .    .    .    . .416
    "CRI" "Costa Rica"                         .467 .427 .349 .331 .323 .313 .313 .321 .316
    "CUB" "Cuba"                               .422 .383  .34  .32 .313 .317 .316 .316 .315
    "CYP" "Cyprus"                             .331 .253  .14 .132 .139 .137 .134 .118 .114
    "CZE" "Czechia"                            .252    . .153 .135 .133 .133 .138 .139 .134
    "DEU" "Germany"                            .156  .13 .117 .097 .093 .091 .083 .081 .076
    "DJI" "Djibouti"                              .    .    .    .    .    .    .    .    .
    "DMA" "Dominica"                              .    .    .    .    .    .    .    .    .
    "DNK" "Denmark"                            .096 .076 .064 .056 .052 .049 .046 .044 .044
    "DOM" "Dominican Republic"                  .55 .494 .494 .488 .483  .48 .478 .477 .472
    "DZA" "Algeria"                            .682 .624 .552 .507   .5 .419 .416 .424 .427
    "ECU" "Ecuador"                            .592 .502 .475 .425 .424 .417 .403 .399 .391
    "EGY" "Egypt"                              .668 .637 .579    .    . .574    .    .    .
    "ERI" "Eritrea"                               .    .    .    .    .    .    .    .    .
    "ESP" "Spain"                              .157 .119 .117 .115 .108 .103 .097  .09 .084
    "EST" "Estonia"                            .392 .291 .227 .176 .192  .18 .162 .171 .144
    "ETH" "Ethiopia"                              .    . .622 .587 .583 .579  .57 .561 .534
    "FIN" "Finland"                            .101 .085 .088 .077 .073  .07 .067 .064 .063
    "FJI" "Fiji"                               .506    . .435    .    .    .    . .377 .371
    "FRA" "France"                             .185 .162 .138 .107 .105 .091 .089 .087 .085
    "FSM" "Micronesia (Federated States of)"      .    .    .    .    .    .    .    .    .
    "GAB" "Gabon"                              .622 .603 .581 .553 .551 .545 .541 .541 .537
    "GBR" "United Kingdom"                      .23 .201   .2 .183 .176 .172 .167 .161 .147
    "GEO" "Georgia"                            .431 .409 .391 .406 .406 .364 .366 .362 .359
    "GHA" "Ghana"                              .632    . .574 .577 .576    . .559 .557 .556
    "GIN" "Guinea"                                .    .    .    .    .    .    .    .    .
    "GMB" "Gambia"                             .757  .75 .654 .658 .654 .651 .647 .634 .631
    "GNB" "Guinea-Bissau"                         .    .    .    .    .    .    .    .    .
    "GNQ" "Equatorial Guinea"                     .    .    .    .    .    .    .    .    .
    "GRC" "Greece"                             .284 .221 .179 .157 .148 .135 .132 .127 .124
    "GRD" "Grenada"                               .    .    .    .    .    .    .    .    .
    "GTM" "Guatemala"                           .59  .59 .585 .549 .539 .528 .528 .511 .509
    "GUY" "Guyana"                                .  .55 .525 .498 .492 .491 .483 .479 .477
    "HKG" "Hong Kong, China (SAR)"                .    .    .    .    .    .    .    .    .
    "HND" "Honduras"                              . .521 .464  .47 .469 .466    . .434 .427
    "HRV" "Croatia"                            .285 .205 .157 .146 .144 .139 .133 .127 .151
    "HTI" "Haiti"                                 .    . .593    . .632 .637 .635 .633 .632
    "HUN" "Hungary"                            .322 .293 .265 .244 .247 .241 .243 .235 .235
    "IDN" "Indonesia"                          .574 .563  .55 .508 .504   .5 .498   .5 .494
    "IND" "India"                              .696    . .624  .59 .585 .581 .573 .561  .55
    "IRL" "Ireland"                            .203 .198 .191  .17 .153 .148 .139 .135 .128
    "IRN" "Iran (Islamic Republic of)"         .657 .593 .511 .519 .516 .511 .512 .516 .507
    "IRQ" "Iraq"                               .707 .664    . .575 .572 .571 .572 .578 .566
    "ISL" "Iceland"                            .184 .148 .125 .099 .098 .093 .088 .083 .075
    "ISR" "Israel"                             .244 .197 .173 .149 .144  .14  .13 .127 .114
    "ITA" "Italy"                              .198 .178 .176 .124  .12 .116  .09 .087 .086
    "JAM" "Jamaica"                            .474 .457 .458 .443 .431 .428 .424 .417 .414
    "JOR" "Jordan"                             .667 .627  .56 .487 .488 .487 .485 .481 .481
    "JPN" "Japan"                              .156 .135 .146 .121  .12 .129  .13 .127 .121
    "KAZ" "Kazakhstan"                         .444   .4 .358 .275 .265  .24 .227 .218 .212
    "KEN" "Kenya"                              .707 .692 .645 .594 .587 .582 .544  .54 .537
    "KGZ" "Kyrgyzstan"                         .517 .481 .676 .361 .367  .37 .375 .375 .389
    "KHM" "Cambodia"                           .658 .598 .568 .506 .502 .497 .493 .488 .483
    "KIR" "Kiribati"                              .    .    .    .    .    .    .    .    .
    "KNA" "Saint Kitts and Nevis"                 .    .    .    .    .    .    .    .    .
    "KOR" "Korea (Republic of)"                 .28 .194  .11 .098 .094 .086 .085 .078 .076
    "KWT" "Kuwait"                             .588 .586 .381 .228 .224 .241 .235  .33 .328
    "LAO" "Lao People's Democratic Republic"      .  .58 .544 .509 .503 .496  .49 .483 .478
    "LBN" "Lebanon"                               .    .    . .424  .43 .432 .439 .444 .448
    "LBR" "Liberia"                               . .687 .669 .656 .665 .659 .657 .657  .66
    "LBY" "Libya"                                 .    . .339 .287    . .238 .241 .248 .256
    end
    The "gii" variables represent the Gini index for each country of each year, and as you can see, they are definied by each year, from 1995 to 2015, and with a 5 year step.

    My goal is to have a variable called "Year" which is basically, as its name suggests, the year in which the Gini index was measured, for each country, so in other words, I wanna have the values of the "gii" variables in one column as one variable, and the year in which that data was measured in another one variable definied as "Year". I wanna have, for example, I wanna have 1995 as the "Year" variable, and for each country, the "gii" variable would show the value of the Gini index for that same year and for the country in question.

    I hope my explanation is clear and that what I wanna have as a result is not too complicated.

    Thanks for the help.

  • #2
    Perhaps reshape long is what you want.

    Comment


    • #3
      I agree with Nick Cox and here is an example using your dataex:
      Code:
      reshape long gii_, i(country_code country) j(year)

      Comment


      • #4
        Elaborating on Nick's pointer,
        Code:
        reshape long gii_, i(country_code country) j(year)
        rename gii_ gii
        Code:
        . list if country_code=="HND", separator(0) abbreviate(12)
        
             +---------------------------------------+
             | country_code    country   year    gii |
             |---------------------------------------|
        658. |          HND   Honduras   1995      . |
        659. |          HND   Honduras   2000   .521 |
        660. |          HND   Honduras   2005   .464 |
        661. |          HND   Honduras   2010    .47 |
        662. |          HND   Honduras   2011   .469 |
        663. |          HND   Honduras   2012   .466 |
        664. |          HND   Honduras   2013      . |
        665. |          HND   Honduras   2014   .434 |
        666. |          HND   Honduras   2015   .427 |
             +---------------------------------------+

        Comment


        • #5
          That was very helpful, Thank you very mucf for you all

          Comment

          Working...
          X