Announcement

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

  • How to re-shape a wide large data set to a long data with multiple id variables?

    Dear All,

    I am working with a wide data set consisting of 391 and four ID variables. Since the hyphen and star do not work when re-shaping it to long, I have tried using the greshape command with @ option. Still, the code seems too long.

    I can put in all the variables names in the command but is there an efficient way to do this?

    The code that I can run is:

    Code:
    reshape long m1_q19_age m1_q23_edu m1_q29_no_children m1_q30_no_children_school m1_q32_month_expenditure m1_q22_rw_lang, i(HHID m1_q8_state_code m1_q9_district_code m1_q11_village_code) j(year)
    I have attached a few variables from the data set along with the ids. The year variable in time takes two values: 2015 and 2018.

    Example of the data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(m1_q19_age2015 m1_q22_rw_lang2015 m1_q23_edu2015 m1_q19_age2018 m1_q22_rw_lang2018 m1_q23_edu2018) long HHID byte m1_q8_state_code int m1_q9_district_code double m1_q11_village_code
    35 0 1 25 0 1   1 9 133 110499
    22 1 1 25 1 2   2 9 133 110499
    70 0 1 75 0 1   3 9 133 110499
    40 1 2 46 1 2   4 9 133 110499
    60 1 3 63 1 2   5 9 133 110499
    65 1 3 64 1 3   6 9 133 110499
    40 1 5 54 1 5   7 9 133 110499
    74 1 3 33 1 2   8 9 133 110499
    50 1 2 19 1 2   9 9 133 110499
    60 0 1 29 1 4  10 9 133 110499
    38 1 3 43 1 2  11 9 133 110499
    40 1 2 45 1 2  12 9 133 110499
    22 1 5 25 1 5  13 9 133 110615
    25 0 1 25 1 2  14 9 133 110615
    80 0 1 34 1 4  15 9 133 110615
    20 1 1 65 1 2  16 9 133 110615
    65 1 2 38 1 5  17 9 133 110615
    23 1 1 65 0 1  18 9 133 110615
    41 1 4 38 0 1  19 9 133 110615
    32 1 2 30 1 2  20 9 133 110615
    45 0 1 48 0 1  21 9 133 110615
    28 1 3 45 0 1  22 9 133 110615
    32 1 2 30 1 2  23 9 133 110615
    30 1 3 34 1 2  24 9 133 110615
    32 1 3 55 1 2  25 9 133 110907
    80 1 2 80 1 2  26 9 133 110907
    27 1 3 30 1 4  27 9 133 110907
    40 1 2 40 1 2  28 9 133 110907
    29 1 2 42 1 1  29 9 133 110907
    63 1 2 75 0 2  30 9 133 110907
    58 1 2 64 1 2  31 9 133 110907
    38 1 4 35 0 1  32 9 133 110907
    46 1 2 45 1 2  33 9 133 110907
    35 0 1 45 0 1  34 9 133 110907
    45 0 2 25 0 1  35 9 133 110907
    45 0 1 48 0 1  36 9 133 110907
    47 1 2 46 1 2  37 9 133 110913
    85 0 1 49 0 1  38 9 133 110913
    44 1 4 38 1 4  39 9 133 110913
    24 1 3 55 1 3  40 9 133 110913
    50 1 1 54 1 2  41 9 133 110913
    55 1 2 19 1 4  42 9 133 110913
    25 1 4 24 1 3  43 9 133 110913
    48 1 2 51 1 2  44 9 133 110913
    55 1 4 26 1 3  45 9 133 110913
    55 1 4 61 1 1  46 9 133 110913
    75 1 3 21 1 3  47 9 133 110913
    70 1 2 18 1 4  48 9 133 110913
    81 1 3 82 1 3  49 9 133 110934
    55 0 1 65 0 1  50 9 133 110934
    60 0 2 60 0 1  51 9 133 110934
    45 0 1 24 1 4  52 9 133 110934
    61 1 3 65 1 2  53 9 133 110934
    32 1 1 36 1 5  54 9 133 110934
    20 1 1 22 1 5  55 9 133 110934
    65 0 2 70 0 1  56 9 133 110934
    55 1 3 65 1 3  57 9 133 110934
    30 1 2 28 1 4  58 9 133 110934
    68 1 2 71 1 1  59 9 133 110934
    65 1 3 68 1 2  60 9 133 110934
    50 0 2 32 1 5  61 9 133 110972
    32 1 3 35 1 3  62 9 133 110972
    38 1 2 18 1 2  63 9 133 110972
    50 0 1 54 0 1  64 9 133 110972
    45 0 1 52 0 1  65 9 133 110972
    26 1 3 29 0 1  66 9 133 110972
    38 1 1 42 1 2  67 9 133 110972
    60 0 1 63 0 1  68 9 133 110972
    40 0 1 48 0 1  69 9 133 110972
    60 1 2 35 1 3  70 9 133 110972
    23 1 1 65 0 1  71 9 133 110972
    25 1 2 35 0 1  72 9 133 110972
    42 1 4 44 0 2  73 9 133 111006
    36 1 2 65 1 2  74 9 133 111006
    40 0 1 70 0 1  75 9 133 111006
    42 1 3 18 1 3  76 9 133 111006
    40 0 2 43 0 2  77 9 133 111006
    60 0 1 40 1 1  78 9 133 111006
    38 1 3 42 1 2  79 9 133 111006
    45 1 1 65 0 1  80 9 133 111006
    80 1 3 86 1 2  81 9 133 111006
    43 1 3 44 1 3  82 9 133 111006
    43 1 3 40 1 5  83 9 133 111006
    19 1 4 50 0 1  84 9 133 111006
    35 0 2 29 1 3  85 9 133 111079
    48 1 2 50 1 2  86 9 133 111079
    45 0 1 30 1 3  87 9 133 111079
    50 1 3 60 1 1  88 9 133 111079
    67 1 2 70 1 2  89 9 133 111079
    45 1 2 38 1 4  90 9 133 111079
    34 1 5 37 1 5  91 9 133 111079
    40 1 4 42 1 3  92 9 133 111079
    44 1 4 50 1 4  93 9 133 111079
    28 1 3 41 1 3  94 9 133 111079
    70 1 1 40 0 1  95 9 133 111079
    42 0 1 45 0 1  96 9 133 111079
    32 1 2 35 1 4  97 9 133 111096
    22 1 4 66 1 2  98 9 133 111096
    65 1 4 21 1 5  99 9 133 111096
    52 0 1 22 1 5 100 9 133 111096
    end

  • #2
    Code:
    ds *2015 *2018
    local stubs `r(varlist)'
    local stubs: subinstr local stubs "2015" "", all
    local stubs: subinstr local stubs "2018" "", all
    local stubs: list uniq stubs
    
    reshape long `stubs', i(HHID m1_q8_state_code m1_q9_district_code m1_q11_village_code) ///
        j(year)
    Note: This code contemplates the possibility that there are some variables that occur in 2015 but not 2018, or vice versa. If, however, every variable occurs in both years, you can simplify the code to:
    Code:
    ds *2015
    local stubs `r(varlist)'
    local stubs: subinstr local stubs "2015" "", all
    
    reshape long `stubs', i(HHID m1_q8_state_code m1_q9_district_code m1_q11_village_code) ///
        j(year)
    Last edited by Clyde Schechter; 19 Feb 2023, 20:46.

    Comment

    Working...
    X