Announcement

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

  • Reshape

    Hello all, thank you in advance for any advice. I would like to reshape the data I have sampled below from wide-->long having one variable called enroll for all enrollment levels. Here, vars g0, g00 and so on are preK, Kindergarten, etc.). The command I have used which is definitely flawed is:
    Code:
    reshape long g, i(id) j(grade)
    I am not sure what is wrong, but I know something is wrong. Can anyone suggest proper i & j variables? I think that is my main problem and the fact that I encoded several variables in the cleaning process.

    I have never had a great understanding of reshape and what i and j mean so I am glad I have this task at hand, hoping this helps me conquer it.

    data sample:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long(_fips district_fill _school _city) int year long(g0 g00) int(g1 g2 g3 g4)
      . 1 99999   . 2015 132 151   43   31   33   31
    188 1    19 202 2015   1 221    7    7    8    2
    184 1   147  52 2015   1   1    1    0    1    0
    185 1   151  54 2015   1 153    1    3    2    3
     52 1   329 121 2015   1 113    3    1    4    3
    187 1   481 195 2015   1   2    3    2    0    2
    188 1   489 202 2015   1 113    5    2    2    0
    188 1   496 202 2015 132  59   18   11   13   17
    186 1   524 144 2015   1 153    5    5    3    4
      . 2 99999   1 2015   2 153    6    2    2    5
      6 2     6   4 2015   1 113    3    2    2    4
      7 2   549  21 2015   2   2    3    0    0    1
      . 3 99999   1 2015  90  93   12   19   19   17
      2 3    13   8 2015  92 113    1    0    3    1
      . 3   108   . 2015   1   1    1    0    0    0
      3 3   171  62 2015   2   2    0    2    1    1
      4 3   282  98 2015  14 164    6    6    7    6
      5 3   436 173 2015  17  24    4   11    8    9
      . 4 99999   1 2015 155 152 3930 3841 3707 3649
     11 4     4  15 2015   2 177   39   55   54   46
     11 4     7  15 2015   1 166   55   49   40   41
     11 4    18  15 2015  32 160   41   40   47   34
     11 4    21  15 2015 152 113    4    1    2    2
      . 4    25  55 2015   2 202   74   75   91   83
     11 4    42  15 2015   1 175   61   55   55   55
      . 4    49  88 2015 118 166   37   35   44   32
     11 4    50  15 2015   1   1    0    0    0    0
     11 4    59  15 2015   1   1    0    0    0    0
     11 4    60  15 2015   1 175   62   45   55   64
     11 4    61  15 2015  92 186   58   63   64   70
     11 4    62  15 2015   2 157   56   48   54   67
     11 4    64  15 2015   1   1    0    0    0    0
      . 4    71  55 2015   1 171   53   55   62   53
     11 4    75  15 2015  77 203   80   77   72   58
     11 4    82  15 2015  98 188   44   51   48   49
     11 4    85  15 2015   1   1    0    0    0    0
     11 4    92  15 2015  32 132   45   43   37   37
     11 4   100  15 2015   1 206   81   62   71   64
     11 4   102  15 2015   1 154   36   35   35   39
      . 4   103  39 2015  55 198   80   82   69   59
      . 4   104  39 2015   1   1    0    0    0    0
     11 4   106  15 2015   1   1    0    0    0    0
     11 4   109  15 2015   2 168   61   50   47   57
     11 4   121  15 2015 119 188   72   81   70   61
      . 4   122   . 2015   1   1    0    0    0    0
     11 4   137  15 2015  32 191   58   60   61   63
     11 4   143  15 2015   1   1    0    0    0    0
      . 4   150  55 2015   1 105   25   26   26   27
      . 4   152  55 2015  56 183   63   67   61   51
      . 4   153  55 2015   1   1    0    0    0    0
     11 4   156  15 2015   1   1    0    0    0    0
     11 4   169  15 2015  76 205   61   67   54   56
     11 4   172  15 2015   1  83   23   28   23   30
      . 4   176  55 2015 132 174   44   60   54   61
     11 4   184  15 2015   1 208   10    4    9    3
      . 4   191  68 2015   1  96   28   20   28   25
     11 4   196  15 2015  39 181   48   57   44   57
     11 4   200  15 2015   1   1    0    0    0    0
     11 4   202  15 2015   2 211   67   59   55   70
      . 4   203  55 2015   1   1    0    0    0    0
     11 4   209  15 2015   1   1    0    0    0    0
     11 4   214  15 2015   1   1    0    0    0    0
      . 4   222  55 2015   2 145   33   48   44   53
     11 4   234  15 2015  55 160   54   47   47   68
     11 4   242  15 2015   1 154   35   42   27   29
     11 4   269  15 2015  88 177   68   51   57   55
     11 4   281  15 2015  55 218   79   82   62   75
     11 4   283  15 2015   1 180   70   59   52   48
     11 4   301  15 2015  44 161   52   57   56   47
     11 4   302  15 2015  39 194   59   67   57   61
     11 4   322  15 2015   1   1    0    0    0    0
     11 4   327  15 2015   1   1    0    0    0    0
      . 4   338  39 2015   1   1    0    0    0    0
     11 4   342  15 2015  97 173   67   57   53   57
      . 4   346   . 2015  92 171   48   47   36   43
     11 4   348  15 2015   2 210   71   73   64   64
     11 4   359  15 2015   1   1    0    0    0    0
     11 4   373  15 2015  56 175   78   49   61   54
     11 4   374  15 2015   1 187   65   71   75   74
     11 4   375  15 2015 120 159   49   42   44   41
     11 4   377  15 2015 115 163   40   46   35   32
     11 4   382  15 2015  72 186   70   62   55   43
     11 4   381  15 2015   2 157   48   51   49   46
      . 4   384  88 2015   2 188   66   59   42   42
     11 4   403  15 2015   1 140   29   40   34   38
     11 4   409  15 2015  92 202   55   56   53   76
     11 4   412  15 2015 118 175   62   74   65   52
      . 4   418  55 2015 103 171   43   45   68   57
     11 4   423  15 2015   1 188   87   85   60   57
     11 4   427  15 2015   1 186   94   75   92   86
     11 4   428  15 2015   1   1    0    0    0    0
     11 4   429  15 2015  76 180   65   66   50   74
     11 4   432  15 2015   1   1    0    0    0    0
     11 4   435  15 2015   2  13  101   93  100   84
     11 4   440  15 2015   2 194   59   72   56   74
     11 4   443  15 2015   1   1    0    0    0    0
     11 4   464  15 2015   1   1    0    0    0    0
     11 4   465  15 2015   1 180   58   49   64   52
     11 4   469  15 2015   1   1    0    0    0    0
     11 4   473  15 2015   1 191   83   75   73   79
    end
    Below is my output followed by a sample of the data. First, I know g00 needs to be renamed, I would like for the grades to be numerically ordered but logical: prek is 0, kindergaten is ??, grade 1 is 1... and so on. My j var should have 16 values (0, 00,1,2,3,...,14) not 15 but g00 isn't getting included in the reshape for some reason. Any suggestions on what can be a varname between 0 and 1 that will be numerically ordered? Or any varnames that can be <0?


    Output:

    HTML Code:
    . reshape long g, i(id) j(grade)
    
    (j = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14)
    
    Data                               Wide   ->   Long
    -----------------------------------------------------------------------------
    Number of observations            4,455   ->   66,825      
    Number of variables                  22   ->   9          
    j variable (15 values)                    ->   grade
    xij variables:
                              g0 g1 ... g14   ->   g
    -----------------------------------------------------------------------------
    Data after reshape:



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float id byte grade long(_fips district_fill _school _city) int year long(g00 g)
    1  0   . 1 99999   . 2015 151 132
    1  1   . 1 99999   . 2015 151  43
    1  2   . 1 99999   . 2015 151  31
    1  3   . 1 99999   . 2015 151  33
    1  4   . 1 99999   . 2015 151  31
    1  5   . 1 99999   . 2015 151  26
    1  6   . 1 99999   . 2015 151  32
    1  7   . 1 99999   . 2015 151  36
    1  8   . 1 99999   . 2015 151  36
    1  9   . 1 99999   . 2015 151  37
    1 10   . 1 99999   . 2015 151  20
    1 11   . 1 99999   . 2015 151  20
    1 12   . 1 99999   . 2015 151  15
    1 13   . 1 99999   . 2015 151 523
    1 14   . 1 99999   . 2015 151 521
    2  0 188 1    19 202 2015 221   1
    2  1 188 1    19 202 2015 221   7
    2  2 188 1    19 202 2015 221   7
    2  3 188 1    19 202 2015 221   8
    2  4 188 1    19 202 2015 221   2
    2  5 188 1    19 202 2015 221   5
    2  6 188 1    19 202 2015 221   4
    2  7 188 1    19 202 2015 221   8
    2  8 188 1    19 202 2015 221   6
    2  9 188 1    19 202 2015 221   7
    2 10 188 1    19 202 2015 221   7
    2 11 188 1    19 202 2015 221  10
    2 12 188 1    19 202 2015 221   7
    2 13 188 1    19 202 2015 221 909
    2 14 188 1    19 202 2015 221 910
    3  0 184 1   147  52 2015   1   1
    3  1 184 1   147  52 2015   1   1
    3  2 184 1   147  52 2015   1   0
    3  3 184 1   147  52 2015   1   1
    3  4 184 1   147  52 2015   1   0
    3  5 184 1   147  52 2015   1   1
    3  6 184 1   147  52 2015   1   0
    3  7 184 1   147  52 2015   1   1
    3  8 184 1   147  52 2015   1   2
    3  9 184 1   147  52 2015   1   1
    3 10 184 1   147  52 2015   1   1
    3 11 184 1   147  52 2015   1   0
    3 12 184 1   147  52 2015   1   3
    3 13 184 1   147  52 2015   1  33
    3 14 184 1   147  52 2015   1  34
    4  0 185 1   151  54 2015 153   1
    4  1 185 1   151  54 2015 153   1
    4  2 185 1   151  54 2015 153   3
    4  3 185 1   151  54 2015 153   2
    4  4 185 1   151  54 2015 153   3
    4  5 185 1   151  54 2015 153   1
    4  6 185 1   151  54 2015 153   5
    4  7 185 1   151  54 2015 153   1
    4  8 185 1   151  54 2015 153   5
    4  9 185 1   151  54 2015 153   2
    4 10 185 1   151  54 2015 153   0
    4 11 185 1   151  54 2015 153   0
    4 12 185 1   151  54 2015 153   0
    4 13 185 1   151  54 2015 153 367
    4 14 185 1   151  54 2015 153 370
    5  0  52 1   329 121 2015 113   1
    5  1  52 1   329 121 2015 113   3
    5  2  52 1   329 121 2015 113   1
    5  3  52 1   329 121 2015 113   4
    5  4  52 1   329 121 2015 113   3
    5  5  52 1   329 121 2015 113   1
    5  6  52 1   329 121 2015 113   3
    5  7  52 1   329 121 2015 113   2
    5  8  52 1   329 121 2015 113   5
    5  9  52 1   329 121 2015 113   0
    5 10  52 1   329 121 2015 113   4
    5 11  52 1   329 121 2015 113   2
    5 12  52 1   329 121 2015 113   0
    5 13  52 1   329 121 2015 113 413
    5 14  52 1   329 121 2015 113 417
    6  0 187 1   481 195 2015   2   1
    6  1 187 1   481 195 2015   2   3
    6  2 187 1   481 195 2015   2   2
    6  3 187 1   481 195 2015   2   0
    6  4 187 1   481 195 2015   2   2
    6  5 187 1   481 195 2015   2   0
    6  6 187 1   481 195 2015   2   1
    6  7 187 1   481 195 2015   2   3
    6  8 187 1   481 195 2015   2   0
    6  9 187 1   481 195 2015   2   0
    6 10 187 1   481 195 2015   2   0
    6 11 187 1   481 195 2015   2   0
    6 12 187 1   481 195 2015   2   0
    6 13 187 1   481 195 2015   2  58
    6 14 187 1   481 195 2015   2  58
    7  0 188 1   489 202 2015 113   1
    7  1 188 1   489 202 2015 113   5
    7  2 188 1   489 202 2015 113   2
    7  3 188 1   489 202 2015 113   2
    7  4 188 1   489 202 2015 113   0
    7  5 188 1   489 202 2015 113   4
    7  6 188 1   489 202 2015 113   2
    7  7 188 1   489 202 2015 113   1
    7  8 188 1   489 202 2015 113   3
    7  9 188 1   489 202 2015 113   5
    end
    Last edited by raniyah bakr; 22 Feb 2022, 16:59.

  • #2
    As an aside, I do realize I need to change my stub to enroll in order to create and enroll var.

    Comment


    • #3
      I'm a little confused by the example data you show in #1. The output has a variable, id, which you refer to in your -reshape- command. And it is actually quite crucial to doing the -reshape-. But the "before" data doesn't have that variable. I'm going to assume you just forgot to include it in your -dataex- command for the first example. If it really doesn't exist, you will have to create it. -gen long id = _n- will do the job for that.

      That said, your code is almost correct. The problem is that -reshape- is stumbling over 0 vs 00 because as numbers they are the same thing. The trick is to treat them as strings.

      Code:
      reshape long g, i(id) j(grade) string
      rename g enroll
      Now your code will distinguish grade 0 from grade 00 in the results. And maybe working with grade as a string variable will work out OK for whatever you need to do after this. If not, if you need a numeric variable, where 00 corresponds to something between 0 and 1, why not make it 0.5?
      Code:
      replace grade = "0.5" if grade == "00"
      destring grade, replace
      That leaves you with a numeric grade variable ranging from 0 through 14 (well, in your example data, 0 through 4) with a 0.5 value corresponding to what you originally called 00.

      Comment


      • #4
        Thankyou for the reply, Clyde! I wasn't sure I could use a"." in a varname so I went ahead and changed it to 15 but labeled Kindergarten. A few points:
        Yes, I just forgot to include the id in the before data sample. I had used
        Code:
        gen id= _n
        Unfortunately the data sample I included is using value labels instead of the observations. The same issue is popping up in my reshape results or when I -tostring- the variables. I am trying to resolve this. Maybe I should not have encoded all my str before reshaping...

        So the command worked, now I am struggling with value labels.
        here are my new results. Note: I changed my stub to enroll before reshaping

        Code:
        reshape long enroll, i(id) j(grade) string
        -----------------------
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float id str2 grade long(_fips district_fill _school _city) int year long enroll
        1 "0"    . 1 99999   . 2015 132
        1 "1"    . 1 99999   . 2015  43
        1 "10"   . 1 99999   . 2015  20
        1 "11"   . 1 99999   . 2015  20
        1 "12"   . 1 99999   . 2015  15
        1 "13"   . 1 99999   . 2015 523
        1 "14"   . 1 99999   . 2015 521
        1 "15"   . 1 99999   . 2015 151
        1 "2"    . 1 99999   . 2015  31
        1 "3"    . 1 99999   . 2015  33
        1 "4"    . 1 99999   . 2015  31
        1 "5"    . 1 99999   . 2015  26
        1 "6"    . 1 99999   . 2015  32
        1 "7"    . 1 99999   . 2015  36
        1 "8"    . 1 99999   . 2015  36
        1 "9"    . 1 99999   . 2015  37
        2 "0"  188 1    19 202 2015   1
        2 "1"  188 1    19 202 2015   7
        2 "10" 188 1    19 202 2015   7
        2 "11" 188 1    19 202 2015  10
        2 "12" 188 1    19 202 2015   7
        2 "13" 188 1    19 202 2015 909
        2 "14" 188 1    19 202 2015 910
        2 "15" 188 1    19 202 2015 221
        2 "2"  188 1    19 202 2015   7
        2 "3"  188 1    19 202 2015   8
        2 "4"  188 1    19 202 2015   2
        2 "5"  188 1    19 202 2015   5
        2 "6"  188 1    19 202 2015   4
        2 "7"  188 1    19 202 2015   8
        2 "8"  188 1    19 202 2015   6
        2 "9"  188 1    19 202 2015   7
        3 "0"  184 1   147  52 2015   1
        3 "1"  184 1   147  52 2015   1
        3 "10" 184 1   147  52 2015   1
        3 "11" 184 1   147  52 2015   0
        3 "12" 184 1   147  52 2015   3
        3 "13" 184 1   147  52 2015  33
        3 "14" 184 1   147  52 2015  34
        3 "15" 184 1   147  52 2015   1
        3 "2"  184 1   147  52 2015   0
        3 "3"  184 1   147  52 2015   1
        3 "4"  184 1   147  52 2015   0
        3 "5"  184 1   147  52 2015   1
        3 "6"  184 1   147  52 2015   0
        3 "7"  184 1   147  52 2015   1
        3 "8"  184 1   147  52 2015   2
        3 "9"  184 1   147  52 2015   1
        4 "0"  185 1   151  54 2015   1
        4 "1"  185 1   151  54 2015   1
        4 "10" 185 1   151  54 2015   0
        4 "11" 185 1   151  54 2015   0
        4 "12" 185 1   151  54 2015   0
        4 "13" 185 1   151  54 2015 367
        4 "14" 185 1   151  54 2015 370
        4 "15" 185 1   151  54 2015 153
        4 "2"  185 1   151  54 2015   3
        4 "3"  185 1   151  54 2015   2
        4 "4"  185 1   151  54 2015   3
        4 "5"  185 1   151  54 2015   1
        4 "6"  185 1   151  54 2015   5
        4 "7"  185 1   151  54 2015   1
        4 "8"  185 1   151  54 2015   5
        4 "9"  185 1   151  54 2015   2
        5 "0"   52 1   329 121 2015   1
        5 "1"   52 1   329 121 2015   3
        5 "10"  52 1   329 121 2015   4
        5 "11"  52 1   329 121 2015   2
        5 "12"  52 1   329 121 2015   0
        5 "13"  52 1   329 121 2015 413
        5 "14"  52 1   329 121 2015 417
        5 "15"  52 1   329 121 2015 113
        5 "2"   52 1   329 121 2015   1
        5 "3"   52 1   329 121 2015   4
        5 "4"   52 1   329 121 2015   3
        5 "5"   52 1   329 121 2015   1
        5 "6"   52 1   329 121 2015   3
        5 "7"   52 1   329 121 2015   2
        5 "8"   52 1   329 121 2015   5
        5 "9"   52 1   329 121 2015   0
        6 "0"  187 1   481 195 2015   1
        6 "1"  187 1   481 195 2015   3
        6 "10" 187 1   481 195 2015   0
        6 "11" 187 1   481 195 2015   0
        6 "12" 187 1   481 195 2015   0
        6 "13" 187 1   481 195 2015  58
        6 "14" 187 1   481 195 2015  58
        6 "15" 187 1   481 195 2015   2
        6 "2"  187 1   481 195 2015   2
        6 "3"  187 1   481 195 2015   0
        6 "4"  187 1   481 195 2015   2
        6 "5"  187 1   481 195 2015   0
        6 "6"  187 1   481 195 2015   1
        6 "7"  187 1   481 195 2015   3
        6 "8"  187 1   481 195 2015   0
        6 "9"  187 1   481 195 2015   0
        7 "0"  188 1   489 202 2015   1
        7 "1"  188 1   489 202 2015   5
        7 "10" 188 1   489 202 2015   0
        7 "11" 188 1   489 202 2015   0
        end

        Comment


        • #5
          -accidental double reply, deleted for tidiness-
          Last edited by raniyah bakr; 22 Feb 2022, 19:02.

          Comment


          • #6
            I wasn't sure I could use a"." in a varname
            You can't. And my code doesn't. The 0.5 doesn't appear until after the reshape. At that point it's no longer part of any variable name, it's a value of the variable grade.

            I don't understand what you are saying about value labels. None of the data examples you showed continue any value labels. If you want help with this issue, you'll have to show a new data example that includes value labels and that illustrates the problem(s) they are causing you. You should also explain clearly precisely in what sense they are causing problems.

            Comment


            • #7
              Originally posted by Clyde Schechter View Post
              I don't understand what you are saying about value labels. None of the data examples you showed continue any value labels. If you want help with this issue, you'll have to show a new data example that includes value labels and that illustrates the problem(s) they are causing you. You should also explain clearly precisely in what sense they are causing problems.
              You are correct. The value labels are what is missing from the -dataex-, apologies. I have included a screenshot below to more clearly illustrate what my issue is. The yellow highlighted 19 is what I was accidentally calling a value label. I have selected an observation from _school, we can see the value label is displayed 'Alaska REACH Academy'. But, when I supply the dataex you are shown the 19.

              This is also happening with some of my enroll observations. For example, the first observation is not 132 students, it is actually 5. This screenshot is post reshape but before, I had encoded the variables g0, g13, g14, and g15. These observations are now displaying the number that shows up in the box where you see the highlighted 19.

              if you don't mind, what is the 19 called? This will help me ask a more focused question next time or allow me to search the internet for help more efficiently. I am missing the proper terminology but I don't want that to be a barrier to learning something new. Thanks.

              Click image for larger version

Name:	Encoded.PNG
Views:	1
Size:	54.4 KB
ID:	1651504

              Comment


              • #8
                and a new dataex of my data before reshape.

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input float id long(_fips district_fill _school _city) int year long(enroll0 enroll15) int(enroll1 enroll2 enroll3 enroll4 enroll5 enroll6 enroll7 enroll8 enroll9 enroll10 enroll11 enroll12) long(enroll13 enroll14)
                  1   . 1 99999   . 2015 132 151   43   31   33   31   26   32   36   36   37   20   20   15 523 521
                  2 188 1    19 202 2015   1 221    7    7    8    2    5    4    8    6    7    7   10    7 909 910
                  3 184 1   147  52 2015   1   1    1    0    1    0    1    0    1    2    1    1    0    3  33  34
                  4 185 1   151  54 2015   1 153    1    3    2    3    1    5    1    5    2    0    0    0 367 370
                  5  52 1   329 121 2015   1 113    3    1    4    3    1    3    2    5    0    4    2    0 413 417
                  6 187 1   481 195 2015   1   2    3    2    0    2    0    1    3    0    0    0    0    0  58  58
                  7 188 1   489 202 2015   1 113    5    2    2    0    4    2    1    3    5    0    0    0 367 370
                  8 188 1   496 202 2015 132  59   18   11   13   17   12   12   16   15   15    7    8    4 174 164
                  9 186 1   524 144 2015   1 153    5    5    3    4    2    5    4    0    7    1    0    1 532 537
                 10   . 2 99999   1 2015   2 153    6    2    2    5    3    2    1    3    1    2    2    3 481 473
                 11   6 2     6   4 2015   1 113    3    2    2    4    3    1    1    1    0    2    2    1 345 348
                 12   7 2   549  21 2015   2   2    3    0    0    1    0    1    0    2    1    0    0    2  58  34
                 13   . 3 99999   1 2015  90  93   12   19   19   17   12   21   18   15   19   20   10   15 339 296
                 14   2 3    13   8 2015  92 113    1    0    3    1    1    1    1    0    0    3    0    3 249 182
                 15   . 3   108   . 2015   1   1    1    0    0    0    1    2    0    1    0    0    0    0 659 662
                 16   3 3   171  62 2015   2   2    0    2    1    1    1    3    1    1    2    2    0    0 158 136
                 17   4 3   282  98 2015  14 164    6    6    7    6    2    9    4    9    6    4    7    5 914 858
                 18   5 3   436 173 2015  17  24    4   11    8    9    7    6   12    4   11   11    3    7  51  18
                 19   . 4 99999   1 2015 155 152 3930 3841 3707 3649 3515 3522 3474 3635 3593 3602 3305 3691 636 631
                 20  11 4     4  15 2015   2 177   39   55   54   46   50   59    0    0    0    0    0    0 474 476
                 21  11 4     7  15 2015   1 166   55   49   40   41   48   37    0    0    0    0    0    0 427 430
                 22  11 4    18  15 2015  32 160   41   40   47   34   38   30   30   12    0    0    0    0 441 426
                 23  11 4    21  15 2015 152 113    4    1    2    2    5    2    3    1    2    3    1    5 532 451
                 24   . 4    25  55 2015   2 202   74   75   91   83   74   95    0    0    0    0    0    0 714 713
                 25  11 4    42  15 2015   1 175   61   55   55   55   50   45    0    0    0    0    0    0 491 494
                 26   . 4    49  88 2015 118 166   37   35   44   32   36   25    0    0    0    0    0    0 361 360
                 27  11 4    50  15 2015   1   1    0    0    0    0    0    0    0    0    0    1   24   36 749 749
                 28  11 4    59  15 2015   1   1    0    0    0    0    0    0    0    0  394  409  356  332 134 133
                 29  11 4    60  15 2015   1 175   62   45   55   64   46   52    0    0    0    0    0    0 495 498
                 30  11 4    61  15 2015  92 186   58   63   64   70   58   71    0    0    0    0    0    0 593 592
                 31  11 4    62  15 2015   2 157   56   48   54   67   53   59    0    0    0    0    0    0 494 496
                 32  11 4    64  15 2015   1   1    0    0    0    0    0    0    0    5   12   26   52  157 348 351
                 33   . 4    71  55 2015   1 171   53   55   62   53   52   30    0    0    0    0    0    0 469 472
                 34  11 4    75  15 2015  77 203   80   77   72   58   70   68    0    0    0    0    0    0 684 665
                 35  11 4    82  15 2015  98 188   44   51   48   49   49   55    0    0    0    0    0    0 508 476
                 36  11 4    85  15 2015   1   1    0    0    0    0    0    0  243  237    0    0    0    0 633 641
                 37  11 4    92  15 2015  32 132   45   43   37   37   39    0    0    0    0    0    0    0 347 327
                 38  11 4   100  15 2015   1 206   81   62   71   64   55   54    0    0    0    0    0    0 611 618
                 39  11 4   102  15 2015   1 154   36   35   35   39   35   38    0    0    0    0    0    0 354 357
                 40   . 4   103  39 2015  55 198   80   82   69   59   66    0    0    0    0    0    0    0 560 567
                 41   . 4   104  39 2015   1   1    0    0    0    0    0    0    0    0  294  296  239  257  31  32
                 42  11 4   106  15 2015   1   1    0    0    0    0    0  322  331  357    0    0    0    0  11  11
                 43  11 4   109  15 2015   2 168   61   50   47   57   38   49    0    0    0    0    0    0 465 467
                 44  11 4   121  15 2015 119 188   72   81   70   61   65    0    0    0    0    0    0    0 597 549
                 45   . 4   122   . 2015   1   1    0    0    0    0    0    0    0    0    1    2    6   21 402 405
                 46  11 4   137  15 2015  32 191   58   60   61   63   47   48    0    0    0    0    0    0 546 529
                 47  11 4   143  15 2015   1   1    0    0    0    0    0    0    0    0  464  430  411  393 179 181
                 48   . 4   150  55 2015   1 105   25   26   26   27   25   23    0    0    0    0    0    0 195 204
                 49   . 4   152  55 2015  56 183   63   67   61   51   45   39    0    0    0    0    0    0 531 508
                 50   . 4   153  55 2015   1   1    0    0    0    0    0    0    0    0  243  216  188  184 895 895
                 51  11 4   156  15 2015   1   1    0    0    0    0    0    0    0    0  613  566  514  468 289 295
                 52  11 4   169  15 2015  76 205   61   67   54   56   50   53    0    0    0    0    0    0 584 556
                 53  11 4   172  15 2015   1  83   23   28   23   30   26   42   33   49   37   66   69   99 697 700
                 54   . 4   176  55 2015 132 174   44   60   54   61   48    1    0    0    0    0    0    0 437 434
                 55  11 4   184  15 2015   1 208   10    4    9    3   10   14   12   24   28   40   45   60 364 367
                 56   . 4   191  68 2015   1  96   28   20   28   25   24   18   15   10    0    0    0    0 223 234
                 57  11 4   196  15 2015  39 181   48   57   44   57   61   49    0    0    0    0    0    0 509 493
                 58  11 4   200  15 2015   1   1    0    0    0    0    0    0  324  376    0    0    0    0 816 817
                 59  11 4   202  15 2015   2 211   67   59   55   70   60   48    0    0    0    0    0    0 583 584
                 60   . 4   203  55 2015   1   1    0    0    0    0    0    0  299  301    0    0    0    0 740 741
                 61  11 4   209  15 2015   1   1    0    0    0    0    0    0  354  385    0    0    0    0 840 843
                 62  11 4   214  15 2015   1   1    0    0    0    0    0   19   26   28   35   35   24   27 236 250
                 63   . 4   222  55 2015   2 145   33   48   44   53   49   33    0    0    0    0    0    0 399 401
                 64  11 4   234  15 2015  55 160   54   47   47   68   55   68    0    0    0    0    0    0 500 501
                 65  11 4   242  15 2015   1 154   35   42   27   29   25   36    0    0    0    0    0    0 319 326
                 66  11 4   269  15 2015  88 177   68   51   57   55   62   73    0    0    0    0    0    0 595 565
                 67  11 4   281  15 2015  55 218   79   82   62   75   73   81    0    0    0    0    0    0 695 696
                 68  11 4   283  15 2015   1 180   70   59   52   48   55   52    0    0    0    0    0    0 512 518
                 69  11 4   301  15 2015  44 161   52   57   56   47   60   44    0    0    0    0    0    0 494 477
                 70  11 4   302  15 2015  39 194   59   67   57   61   51   64    0    0    0    0    0    0 586 569
                 71  11 4   322  15 2015   1   1    0    0    0    0    0    0    3    2    8   18   26   39 947 949
                 72  11 4   327  15 2015   1   1    0    0    0    0    0    0  394  384    0    0    0    0 865 869
                 73   . 4   338  39 2015   1   1    0    0    0    0    0  182  201  236    0    0    0    0 754 754
                 74  11 4   342  15 2015  97 173   67   57   53   57   54    0    0    0    0    0    0    0 487 456
                 75   . 4   346   . 2015  92 171   48   47   36   43   37   26    0    0    0    0    0    0 395 395
                 76  11 4   348  15 2015   2 210   71   73   64   64   66    1    0    0    0    0    0    0 547 558
                 77  11 4   359  15 2015   1   1    0    0    0    0    0  336  365  341    0    0    0    0  20  19
                 78  11 4   373  15 2015  56 175   78   49   61   54   58   62    0    0    0    0    0    0 581 558
                 79  11 4   374  15 2015   1 187   65   71   75   74   75   70   62   52    0    0    0    0 748 748
                 80  11 4   375  15 2015 120 159   49   42   44   41   44   44    0    0    0    0    0    0 460 416
                 81  11 4   377  15 2015 115 163   40   46   35   32   35    0    0    0    0    0    0    0 374 330
                 82  11 4   382  15 2015  72 186   70   62   55   43   59   56    0    0    0    0    0    0 566 536
                 83  11 4   381  15 2015   2 157   48   51   49   46   49   54    0    0    0    0    0    0 449 452
                 84   . 4   384  88 2015   2 188   66   59   42   42   36   32    0    0    0    0    0    0 453 455
                 85  11 4   403  15 2015   1 140   29   40   34   38   39   42   40   43   37   31   33   33 624 633
                 86  11 4   409  15 2015  92 202   55   56   53   76   63    0    0    0    0    0    0    0 494 493
                 87  11 4   412  15 2015 118 175   62   74   65   52   71   51    0    0    0    0    0    0 574 575
                 88   . 4   418  55 2015 103 171   43   45   68   57   73   59    0    0    0    0    0    0 569 521
                 89  11 4   423  15 2015   1 188   87   85   60   57   46   14   16   12    0    0    0    0 584 586
                 90  11 4   427  15 2015   1 186   94   75   92   86   73   81    0    0    0    0    0    0 711 711
                 91  11 4   428  15 2015   1   1    0    0    0    0    0    0  377  423    0    0    0    0 882 883
                 92  11 4   429  15 2015  76 180   65   66   50   74   45    0    0    0    0    0    0    0 498 475
                 93  11 4   432  15 2015   1   1    0    0    0    0    0    0    0    0    0    2   27  156 215 223
                 94  11 4   435  15 2015   2  13  101   93  100   84  102   97    0    0    0    0    0    0 800 800
                 95  11 4   440  15 2015   2 194   59   72   56   74   63    0    0    0    0    0    0    0 508 514
                 96  11 4   443  15 2015   1   1    0    0    0    0    0    0    0    0  449  463  427  433 196 205
                 97  11 4   464  15 2015   1   1    0    0    0    0    0    0    0    0  352  377  357  362 125 123
                 98  11 4   465  15 2015   1 180   58   49   64   52   45   48    0    0    0    0    0    0 489 492
                 99  11 4   469  15 2015   1   1    0    0    0    0    0    0   52   53   51   45   39   42 381 384
                100  11 4   473  15 2015   1 191   83   75   73   79   70    0    0    0    0    0    0    0 588 591
                end

                Comment


                • #9
                  Re #7, the number 19 is the actual numeric value of the variable, and "Alaska Reach Academy" is its value label. In what way is this causing you a problem? This is a very good use of value-labeled numeric variables and in most situations makes your life simpler. The only thing you have to remember is that the numeric values are just arbitrary numbers assigned to the labeled categories. So it makes no sense to perform arithmetic with variables like that. And, at least in this case, it wouldn't make sense to do comparisons like < or > (but == and != are fine). (There are some categorical variables where order comparisons are fine, for example to represent responses to a survey question where the response options were Excellent Good Fair Poor.) But, in general, value labeled numeric variables are a great way to represent categorical variables for use in analyses.

                  With the example data shown in #8, I would do the reshape as:
                  Code:
                  reshape long enroll, i(id) j(grade)
                  replace grade = 0.5 if grade == 15
                  sort id grade
                  Last edited by Clyde Schechter; 22 Feb 2022, 19:45.

                  Comment


                  • #10
                    The following code worked perfectly after I cleaned up some entries that were causing a couple variables to not destring. Thank you! And the problem with the value labels is that the actual numeric value was being displayed in my dataset, not the value label which is the actual enrollment level.

                    Code:
                    reshape long enroll, i(id) j(grade)
                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input float id byte grade long(_fips district_fill _school _city) int year long enroll
                    1  0   . 1 99999   . 2015   5
                    1  1   . 1 99999   . 2015  43
                    1  2   . 1 99999   . 2015  31
                    1  3   . 1 99999   . 2015  33
                    1  4   . 1 99999   . 2015  31
                    1  5   . 1 99999   . 2015  26
                    1  6   . 1 99999   . 2015  32
                    1  7   . 1 99999   . 2015  36
                    1  8   . 1 99999   . 2015  36
                    1  9   . 1 99999   . 2015  37
                    1 10   . 1 99999   . 2015  20
                    1 11   . 1 99999   . 2015  20
                    1 12   . 1 99999   . 2015  15
                    1 13   . 1 99999   . 2015 404
                    1 14   . 1 99999   . 2015 399
                    1 15   . 1 99999   . 2015  39
                    2  0 188 1    19 202 2015   0
                    2  1 188 1    19 202 2015   7
                    2  2 188 1    19 202 2015   7
                    2  3 188 1    19 202 2015   8
                    2  4 188 1    19 202 2015   2
                    2  5 188 1    19 202 2015   5
                    2  6 188 1    19 202 2015   4
                    2  7 188 1    19 202 2015   8
                    2  8 188 1    19 202 2015   6
                    2  9 188 1    19 202 2015   7
                    2 10 188 1    19 202 2015   7
                    2 11 188 1    19 202 2015  10
                    2 12 188 1    19 202 2015   7
                    2 13 188 1    19 202 2015  87
                    2 14 188 1    19 202 2015  87
                    2 15 188 1    19 202 2015   9

                    Comment


                    • #11
                      And the problem with the value labels is that the actual numeric value was being displayed in my dataset, not the value label which is the actual enrollment level.
                      Attaching value labels to a numeric variable is a two-step process. The problem you describe arises when only the first step is completed. The two steps are: defining the value labels, and attaching the value labels.

                      The definition of value labels is accomplished with the -label define- command. It tells Stata the details of an association between numeric values and text labels. After the -label define- command Stata remembers that association, and unless you remove or modify it, it remains a part of the data set. But it does not apply the labels to any variables. To do that you must go on to the second step using the -label values- command. The -label values- command then tells Stata "remember that value label I defined a little while ago, now I want it applied to the following variable(s)." Once that is done, Stata will always display those labels in its output, and in the data browser (unless you specifically ask it not to with the -nolabel- option).

                      So, it is a common misconception that if you have, say, a variable called sex, coded 1 for males and 2 for females, if you run:
                      Code:
                      label define sex 1 "Male" 2 "Female"
                      you have now assigned those labels to the variable sex. But you have not. You have just given Stata the association between numbers 1, 2 and strings "Male", and "Female", respectively. But that is not the case. That doesn't happen until you follow up with
                      Code:
                      label values sex sex
                      You might wonder why Stata requires two steps to do this. The reason is that often one wants to apply the same labeling to multiple variables. For example, one might do
                      Code:
                      label values oldest_child_sex youngest_child_sex index_child_sex sex
                      Which leads to another point: the name of the value label does not have to match, or even bear any relationship whatsoever, to the name(s) of the variable(s) to which it gets attached. This kind of flexibility would be difficult to match with a one-step process. This two-step process also makes it possible to modify an existing value label either before or after it is attached to a variable.

                      Added: By the way, I have presented this two-step process above in the order: define the label, then attach the label to one or more variables. In fact, Stata allows you to do it in reverse order. You can issue a -label values- command that uses an as yet undefined label. Stata will duly note that fact and, if and when you later actually define that label, the value labels will be applied at that time. I think it is unusual to do it this way, but it is possible.
                      Last edited by Clyde Schechter; 23 Feb 2022, 10:19.

                      Comment

                      Working...
                      X