Hello everyone! I am trying to solve how to write a text.file using loops. I have hit an obstacle and I was hoping that you all could give me direction.

Here is the scenario. I have a dataset with 20 observations. There is one variable in this dataset and it is a string variable (str35) that stores the name of the observation. I want to generate a text file that will create a list of the 20 observation names which are stored as a string variable in the dataset.


The Stata Code below runs a loop across all 20 observations in the dataset and for each one it types "Observation Name." Which results in the a text file with Observation Name written out 20 times. For each observation (i), I want to write out the observation name that is stored in the string variable for that observation in the dataset. How can I do this?

So the final result would be a list of 20 different names for each one of the 20 observations.


Stata Code:

file open textfile using textfile.txt, text write replace
describe

local N = _N
forvalues i = 1 / `N' {

file write textfile "Observation Name" _n
}

file close textfile