Hello,

I am using Stata version 15.1 and would like to create a file on disk and append the contents of the current dataset in memory to the file on each iteration of a loop:

The scenario is as follows:

Code:
levelsof catVar, local(levels)
foreach l of local levels {
    preserve
    drop if catVar != `l'

    // here I wish to create some new variables
    // based on the values of the current variables 
    // in memory, and append to a file on disk.
    
    restore
}
The Stata append command appears to be only for appending from disk to the current dataset while I wish to append in the other direction.

If there is no way of doing this I would appreciate any alternative suggestions, such as iterating overall all current rows in the dataset and writing them out one-by-one.