Announcement

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

  • Reshape Long Help

    I'm normally pretty competent when it comes to reshaping data. However, I am running into trouble here reshaping long.


    Code:
    reshape long value, i(state) j(yearID)
    I'm running a pretty simple reshape command. In my original data below I have 14 variables. I want to instead have 3. State, yearID, and value. However, my reshape command is giving my 16 variables. It is preserving the original 14 and then adding a yeartest variable which looks correct, and then the value variable is all missing values.

    I'm not sure what I am doing incorrect.






    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str2 stateab int(value20060061 value20100061 value20120061 value20130061 value20140061 value20145161 value20155161 value20165161 value20175161 value20185161 value20195161 value20205161 value20215161)
    "AK" 146 146   .   .   . 160 160 160 160 160 160 160 160
    "CT" 137 137 137 137 137   . 160 160 160 160 160 160 160
    "DC" 141 141 141 141   . 160 160 160 160 160 160 160 160
    "DE" 141 141 141 141   . 160 160 160 160 160 160 160 160
    "IA"   .   .   . 125   . 160 134 132 134 135 135 135 135
    "ID" 119 129 129 129   . 160 160 160 160 160 160 160 160
    "KS" 137 137 137 137   . 152 152 152 152 152 152 152 152
    "LA" 125 135 135 135 135   . 160 160 160 160 160 160 160
    "MD" 141 141 141 141 141 160 160 160 160 160 160 160 160
    "ME" 126 126 126 126   . 160 160 160 160 160 160 160 160
    "MS" 123 123 123 128   . 160 160 160 160 160 152 152 152
    "MT"   . 128 128 128 128 160 160 160 160 160 160 160 160
    "NC"   .   .   .   .   . 160 160 160 160 160 160 160 160
    "ND" 139 139 139 139   . 160 160 160 160 160 160 160 160
    "NH" 127 127 127 127   . 160 160 160 160 160 160 160 160
    "NV"   . 133 133 133   . 160 160 160 160 160 160 160 160
    "RI"   .   .   .   . 142   . 160 160 160 160 160 160 160
    "SC" 131 131 131 131   . 160 160 160 150 150 150 150 150
    "SD" 124 124 124 124   . 160 160 160 160 160 160 160 160
    "TN" 136 136 136 136   . 160 160 160 160 160 160 160 160
    "UT" 138 138 138 138   . 160 160 160 160 160 160 160 160
    "VA" 147 147 147 147 147   . 160 160 160 160 160 160 160
    "VT" 141 141 141 141   . 160 160 160 160 160 160 160 160
    "WI" 133 135   .   .   .   . 160 160 160 160 160 160 160
    "WV" 133 133 133 133   . 160 160 160 160 160 160 160 160
    end

  • #2
    There is nothing obviously wrong with your code or data. And I am unable to replicate your problem:
    Code:
    . * Example generated by -dataex-. To install: ssc install dataex
    . clear
    
    . input str2 stateab int(value20060061 value20100061 value20120061 value20130061 value20140061 value20145161 value20155161 value20165161 v
    > alue20175161 value20185161 value20195161 value20205161 value20215161)
    
           stateab  va~60061  va~00061  va~20061  va~30061  va~40061  va~45161  va~55161  va~65161  va~75161  va~85161  va~95161  va~05161  va
    > ~15161
      1. "AK" 146 146   .   .   . 160 160 160 160 160 160 160 160
      2. "CT" 137 137 137 137 137   . 160 160 160 160 160 160 160
      3. "DC" 141 141 141 141   . 160 160 160 160 160 160 160 160
      4. "DE" 141 141 141 141   . 160 160 160 160 160 160 160 160
      5. "IA"   .   .   . 125   . 160 134 132 134 135 135 135 135
      6. "ID" 119 129 129 129   . 160 160 160 160 160 160 160 160
      7. "KS" 137 137 137 137   . 152 152 152 152 152 152 152 152
      8. "LA" 125 135 135 135 135   . 160 160 160 160 160 160 160
      9. "MD" 141 141 141 141 141 160 160 160 160 160 160 160 160
     10. "ME" 126 126 126 126   . 160 160 160 160 160 160 160 160
     11. "MS" 123 123 123 128   . 160 160 160 160 160 152 152 152
     12. "MT"   . 128 128 128 128 160 160 160 160 160 160 160 160
     13. "NC"   .   .   .   .   . 160 160 160 160 160 160 160 160
     14. "ND" 139 139 139 139   . 160 160 160 160 160 160 160 160
     15. "NH" 127 127 127 127   . 160 160 160 160 160 160 160 160
     16. "NV"   . 133 133 133   . 160 160 160 160 160 160 160 160
     17. "RI"   .   .   .   . 142   . 160 160 160 160 160 160 160
     18. "SC" 131 131 131 131   . 160 160 160 150 150 150 150 150
     19. "SD" 124 124 124 124   . 160 160 160 160 160 160 160 160
     20. "TN" 136 136 136 136   . 160 160 160 160 160 160 160 160
     21. "UT" 138 138 138 138   . 160 160 160 160 160 160 160 160
     22. "VA" 147 147 147 147 147   . 160 160 160 160 160 160 160
     23. "VT" 141 141 141 141   . 160 160 160 160 160 160 160 160
     24. "WI" 133 135   .   .   .   . 160 160 160 160 160 160 160
     25. "WV" 133 133 133 133   . 160 160 160 160 160 160 160 160
     26. end
    
    .
    . reshape long value, i(state) j(yearID)
    (j = 20060061 20100061 20120061 20130061 20140061 20145161 20155161 20165161 20175161 20185161 20195161 20205161 20215161)
    
    Data                               Wide   ->   Long
    -----------------------------------------------------------------------------
    Number of observations               25   ->   325         
    Number of variables                  14   ->   3           
    j variable (13 values)                    ->   yearID
    xij variables:
    value20060061 value20100061 ... value20215161->value
    -----------------------------------------------------------------------------
    
    .
    . list in 1/10, noobs clean
    
        stateab     yearID   value  
             AK   20060061     146  
             AK   20100061     146  
             AK   20120061       .  
             AK   20130061       .  
             AK   20140061       .  
             AK   20145161     160  
             AK   20155161     160  
             AK   20165161     160  
             AK   20175161     160  
             AK   20185161     160  
    
    .
    end of do-file
    So, it works for me. (Version 17MP4, Windows 10). In situations like this, my suspicion goes to the Stata installation being corrupted or out of date, or perhaps the operating system malfunctioning due to data leaks or something like that. First, I'd do a cold reboot of the computer and try again. If that doesn't work, I would do an -update all, force- and try again.

    Comment


    • #3
      I have tried switching from my Mac to my PC and did the update all, force thing. I am still getting the same strange result.


      Code:
      clear
      set more off
      clear
      input str2 stateab int(value20060061 value20100061 value20120061 value20130061 value20140061 value20145161 value20155161 value20165161 value20175161 value20185161 value20195161 value20205161 value20215161)
      "AK" 146 146   .   .   . 160 160 160 160 160 160 160 160
      "CT" 137 137 137 137 137   . 160 160 160 160 160 160 160
      "DC" 141 141 141 141   . 160 160 160 160 160 160 160 160
      "DE" 141 141 141 141   . 160 160 160 160 160 160 160 160
      "IA"   .   .   . 125   . 160 134 132 134 135 135 135 135
      "ID" 119 129 129 129   . 160 160 160 160 160 160 160 160
      "KS" 137 137 137 137   . 152 152 152 152 152 152 152 152
      "LA" 125 135 135 135 135   . 160 160 160 160 160 160 160
      "MD" 141 141 141 141 141 160 160 160 160 160 160 160 160
      "ME" 126 126 126 126   . 160 160 160 160 160 160 160 160
      "MS" 123 123 123 128   . 160 160 160 160 160 152 152 152
      "MT"   . 128 128 128 128 160 160 160 160 160 160 160 160
      "NC"   .   .   .   .   . 160 160 160 160 160 160 160 160
      "ND" 139 139 139 139   . 160 160 160 160 160 160 160 160
      "NH" 127 127 127 127   . 160 160 160 160 160 160 160 160
      "NV"   . 133 133 133   . 160 160 160 160 160 160 160 160
      "RI"   .   .   .   . 142   . 160 160 160 160 160 160 160
      "SC" 131 131 131 131   . 160 160 160 150 150 150 150 150
      "SD" 124 124 124 124   . 160 160 160 160 160 160 160 160
      "TN" 136 136 136 136   . 160 160 160 160 160 160 160 160
      "UT" 138 138 138 138   . 160 160 160 160 160 160 160 160
      "VA" 147 147 147 147 147   . 160 160 160 160 160 160 160
      "VT" 141 141 141 141   . 160 160 160 160 160 160 160 160
      "WI" 133 135   .   .   .   . 160 160 160 160 160 160 160
      "WV" 133 133 133 133   . 160 160 160 160 160 160 160 160
      end
      reshape long value, i(state) j(ID)

      Code:
      reshape long value, i(state) j(ID)
      (note: j = 20060060 20100060 20120060 20130060 20140060 20145160 20155
      > 160 20165160 20175160 20185160 20195160 20205160 20215160)
      (note: value20060060 not found)
      (note: value20100060 not found)
      (note: value20120060 not found)
      (note: value20130060 not found)
      (note: value20140060 not found)
      (note: value20145160 not found)
      (note: value20155160 not found)
      (note: value20165160 not found)
      (note: value20175160 not found)
      (note: value20185160 not found)
      (note: value20195160 not found)
      (note: value20205160 not found)
      (note: value20215160 not found)
      
      Data wide -> long
      ----------------------------------------------------------------------
      > -------
      Number of obs. 25 -> 325
      Number of variables 14 -> 16
      j variable (13 values) -> ID
      xij variables:
      value20060060 value20100060 ... value20215160->value
      ----------------------------------------------------------------------
      > -------

      Comment


      • #4
        Note you entered value20060061, your output says note: j = 20060060...

        What's the Stata version and reshape version? What's the output of:

        Code:
        about 
        which reshape

        Comment


        • #5
          My version is Stata 14.2 MP.

          Code:
          . which reshape
          /Applications/Stata/ado/base/r/reshape.ado
          *! version 4.5.0  05may2016

          Comment


          • #6
            I just tried running it on another computer that has Stata SE 15.1 and it worked just as it did for Clyde

            Comment


            • #7
              Documentation for the August 23 2019 update says:

              Code:
                 9. reshape with large stub numbers (those rounded because of floating-point precision) produced a faulty dataset and displayed the note "(note: variable not
                      found)".  This has been fixed.
              That may be the problem you're encountering. Treating the j variable as a string may fix it. Try:

              Code:
              reshape long value, i(state) j(ID,string)
              Then you can destring the new ID variable.

              Comment


              • #8
                Originally posted by Ali Atia View Post
                Documentation for the August 23 2019 update says:


                Code:
                reshape long value, i(state) j(ID,string)
                Then you can destring the new ID variable.
                Woohoo! This fixed it, thank you so so much!!!

                Comment

                Working...
                X