Announcement

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

  • reshape data wide to long


    I am working on wide formatted data that have 11574 observations &11547 row. the repeated observations is for prescription issuedate as every patid has multiple issue date. I need to work on the data to identify gaps of 90 days between issue dates, therefore I want to transform the data to long format. this is an example of my data

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(patid issuedate_n1 issuedate_n2 issuedate_n3 issuedate_n4 evdate_n)
     20018 19842 19792 19766     . 19751
     820018 18596 18666 18596 18714 18650
     220018 21090 20681 20893     . 20879
     720018 21010 21158 21183 21273 20944
     620018 20216 20010 20451 20023 20149
     720018 19715 20023 20072 19876 19771
     420018 19451 19201 18756 19430 19102
     820018 19493 19481     .     . 19452
     320018 19250 18919 19250 19023 19126
    end
    format %tdCCYY/NN/DD issuedate_n1
    format %tdCCYY/NN/DD issuedate_n2
    format %tdCCYY/NN/DD issuedate_n3
    format %tdCCYY/NN/DD issuedate_n4
    format %tdCCYY/NN/DD evdate_n
    I tried this code [reshape long issuedate_n, i(patid) j(littlen)]

    and got thie error � invalid name
    r(198); t=1138.64 0:08:20

    can you please advice how to transform this data and identify the gaps for each pat ID ?
    This my first post I hope I made it clear


  • #2
    You have repeated patient identifiers, e.g., 720018. You need to create a variable that accounts for this.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(patid issuedate_n1 issuedate_n2 issuedate_n3 issuedate_n4 evdate_n)
     20018 19842 19792 19766     . 19751
     820018 18596 18666 18596 18714 18650
     220018 21090 20681 20893     . 20879
     720018 21010 21158 21183 21273 20944
     620018 20216 20010 20451 20023 20149
     720018 19715 20023 20072 19876 19771
     420018 19451 19201 18756 19430 19102
     820018 19493 19481     .     . 19452
     320018 19250 18919 19250 19023 19126
    end
    format %tdCCYY/NN/DD issuedate_n1
    format %tdCCYY/NN/DD issuedate_n2
    format %tdCCYY/NN/DD issuedate_n3
    format %tdCCYY/NN/DD issuedate_n4
    format %tdCCYY/NN/DD evdate_n
    
    bys patid (issuedate_n1): gen which=_n
    reshape long issuedate_n, i(patid which) j(littlen)
    l, sepby(patid which)
    Res.:

    Code:
    . l, sepby(patid which)
    
         +----------------------------------------------------+
         |  patid   which   littlen   issuedat~n     evdate_n |
         |----------------------------------------------------|
      1. |  20018       1         1   2014/04/29   2014/01/28 |
      2. |  20018       1         2   2014/03/10   2014/01/28 |
      3. |  20018       1         3   2014/02/12   2014/01/28 |
      4. |  20018       1         4            .   2014/01/28 |
         |----------------------------------------------------|
      5. | 220018       1         1   2017/09/28   2017/03/01 |
      6. | 220018       1         2   2016/08/15   2017/03/01 |
      7. | 220018       1         3   2017/03/15   2017/03/01 |
      8. | 220018       1         4            .   2017/03/01 |
         |----------------------------------------------------|
      9. | 320018       1         1   2012/09/14   2012/05/13 |
     10. | 320018       1         2   2011/10/19   2012/05/13 |
     11. | 320018       1         3   2012/09/14   2012/05/13 |
     12. | 320018       1         4   2012/01/31   2012/05/13 |
         |----------------------------------------------------|
     13. | 420018       1         1   2013/04/03   2012/04/19 |
     14. | 420018       1         2   2012/07/27   2012/04/19 |
     15. | 420018       1         3   2011/05/09   2012/04/19 |
     16. | 420018       1         4   2013/03/13   2012/04/19 |
         |----------------------------------------------------|
     17. | 620018       1         1   2015/05/08   2015/03/02 |
     18. | 620018       1         2   2014/10/14   2015/03/02 |
     19. | 620018       1         3   2015/12/29   2015/03/02 |
     20. | 620018       1         4   2014/10/27   2015/03/02 |
         |----------------------------------------------------|
     21. | 720018       1         1   2013/12/23   2014/02/17 |
     22. | 720018       1         2   2014/10/27   2014/02/17 |
     23. | 720018       1         3   2014/12/15   2014/02/17 |
     24. | 720018       1         4   2014/06/02   2014/02/17 |
         |----------------------------------------------------|
     25. | 720018       2         1   2017/07/10   2017/05/05 |
     26. | 720018       2         2   2017/12/05   2017/05/05 |
     27. | 720018       2         3   2017/12/30   2017/05/05 |
     28. | 720018       2         4   2018/03/30   2017/05/05 |
         |----------------------------------------------------|
     29. | 820018       1         1   2010/11/30   2011/01/23 |
     30. | 820018       1         2   2011/02/08   2011/01/23 |
     31. | 820018       1         3   2010/11/30   2011/01/23 |
     32. | 820018       1         4   2011/03/28   2011/01/23 |
         |----------------------------------------------------|
     33. | 820018       2         1   2013/05/15   2013/04/04 |
     34. | 820018       2         2   2013/05/03   2013/04/04 |
     35. | 820018       2         3            .   2013/04/04 |
     36. | 820018       2         4            .   2013/04/04 |
         +----------------------------------------------------+

    Comment


    • #3
      Thank you for your response I tried this but when I browse the data after applying the code it is still wide.

      here is my data after code
      [CODE]
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input double(patid issuedate_n1 issuedate_n2 issuedate_n3)
      708267120136 17930 18059 18081
      1422185020721 17941 18248 18317
      2733868021032 17959 18239 18071
      5405482221244 17960 18064 18087
      2419429820785 17969 18394 18487
      2855796220906 17972 18304 18287
      2172565120180 17980 18053 18365
      212431120407 17987 18365 18358
      2182352220591 17990 18409 18665
      2055830920879 17996 18322 18625
      1199575320655 18004 18176 18379

      it did not work well

      Comment


      • #4
        You need to show the full sequence of commands that you ran, including the output of the list command. Show the exact Stata output, where you can change the last line to

        Code:
        l in 1/20, sepby(patid which)

        Comment


        • #5
          when I used
          code :
          l, sepby(patid which)

          I got this result
          Click image for larger version

Name:	data.png
Views:	2
Size:	877.8 KB
ID:	1645206


          bit when I browse the data its is still showing issue date repeated in column rather that rows.

          I think now i have to delete the issue dates with missing values as it is taking too much space, what command can I use to do this? how can I change the data to wide format again?
          Attached Files
          I am working on wide formatted data that have 11574 observations &11547 row. the repeated observations is for prescription issuedate as every patid has

          Comment


          • #6
            You are still not showing all previous commands that you ran. Copy the commands from when you open the dataset to up to but not including the list command. Click on the # tab in the forum's advanced editor and paste this within the generated code delimiters.

            how can I change the data to wide format again?
            What you show implies that the data is still in wide layout. To delete variables with all missing values, loop.

            Code:
            forval i= 1/11547{
                sort issuedate_n`i'
                if missing(issuedate_n`i'[1]) & missing(issuedate_n`i'[_N]){
                    drop issuedate_n`i'
               }
            }

            Comment


            • #7
              this is the code I have entered

              Code:
              preserve
              
              . bys patid (issuedate_n1): gen which=_n
              
              . 
              . reshape long issuedate_n, i(patid which) j(littlen)
              (j = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 
              > 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 
              > 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11
              > 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 1
              > 50 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 
              > 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
              >  212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 24
              > 2 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 2
              > 73 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 
              > 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
              >  335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 36
              > 5 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 3
              > 96 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 
              > 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
              >  458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 48
              > 8 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515)

              Comment


              • #8
                Thanks for presenting the output within CODE delimiters.


                I tried this but when I browse the data after applying the code it is still wide.

                The reshape command executes fine. Your problem is that you preserve the dataset prior to reshaping and restore it subsequently. So this undoes the changes that reshape makes. Just delete the preserve and restore commands.

                Comment


                • #9
                  I tried the same codes without preserve and restore and still wide.
                  please see below

                  you have mentioned above that [QUOTE][/you have repeated patient identifier ]. I am have one patient id for each row no repetition, I deleted the initial 3 numbers when I copied the data for confidentiality. not sure if this is the reason
                  Attached Files
                  I am working on wide formatted data that have 11574 observations &11547 row. the repeated observations is for prescription issuedate as every patid has

                  Comment


                  • #10
                    You should get an error message if the reshape fails. If the identifiers are not repeated, the variable "which" does no harm as it will equal one for all entries. If you are working with confidential data and cannot provide a reproducible example that replicates the issue, find a colleague who has some experience with Stata and is authorized to view the dataset to help. Otherwise, I am afraid that we will go round in circles with no reproducible example.

                    Comment


                    • #11
                      I got an error message at the end.

                      . sort patid

                      . bys patid (issuedate_n1): gen which=_n

                      . reshape long issuedate_n, i(patid which) j(littlen)
                      (j = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
                      > 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                      > 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11
                      > 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 1
                      > 50 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
                      > 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
                      > 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 24
                      > 2 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 2
                      > 73 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
                      > 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
                      > 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 36
                      > 5 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 3
                      > 96 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
                      > 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
                      > 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 48
                      > 8 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515)

                      Variable name Display format
                      -----------------------------
                      __000001 %10.0g
                      -----------------------------

                      Variable name Display format
                      -----------------------------
                      __000001 %10.0g
                      -----------------------------

                      Variable name Display format
                      -----------------------------
                      __000001 %10.0g
                      -----------------------------

                      Variable name Display format
                      -----------------------------
                      __000001 %10.0g
                      -----------------------------

                      Variable name Display format
                      -----------------------------
                      __000001 %10.0g
                      -----------------------------

                      Variable name Display format
                      -----------------------------
                      __000001 %10.0g
                      -----------------------------
                      � invalid name
                      r(198);

                      The example I provided above is the exact data I have I just deleted the first number form patid.

                      Many thanks for your consistent response to solve my issuse

                      Comment

                      Working...
                      X