Announcement

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

  • Splitting a Stata file into multiple xlsx files - based on an unique value in a single variable

    Hi there,

    I have a long stata file (1.7 million observations) of GPS coordinates with a unique identifier for each participant (variable "P_num", n= 100 participants).

    I want to write an .xlsx file for each participant with just their data (signified by the unique value in variable "P_num").

    Essentially, I want to drop all observations except for one participant, write an xlsx file, then do this for the next 99 participants without having to manually do it.

    Any suggestions would be very welcome,

    Thank you in advance,

    Tim

  • #2
    No need to drop observations, that's what the if clause is for. Here's sample code that should start you in the right direction. Be sure to read the output of help export excel for other options that may be of use.
    Code:
    sysuse auto, clear
    levelsof foreign, local(files)
    foreach f of local files {
        export excel using cars`f'.xlsx if foreign==`f', replace
        }
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . levelsof foreign, local(files)
    0 1
    
    . foreach f of local files {
      2.     export excel using cars`f'.xlsx if foreign==`f', replace
      3.         }
    file cars0.xlsx saved
    file cars1.xlsx saved
    
    .

    Comment


    • #3
      Thank you! This has solved my problem

      Comment

      Working...
      X