Announcement

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

  • How to write a text.file using loops?

    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












  • #2
    The problem here: Given a single string variable, export it to a text file. You don't need a loop for this. Here is an example. Most of the example is setting up such a dataset. See help export for more detail and variations on how to do it.

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . keep if foreign
    (52 observations deleted)
    
    . keep make
    
    . list
    
         +----------------+
         | make           |
         |----------------|
      1. | Audi 5000      |
      2. | Audi Fox       |
      3. | BMW 320i       |
      4. | Datsun 200     |
      5. | Datsun 210     |
         |----------------|
      6. | Datsun 510     |
      7. | Datsun 810     |
      8. | Fiat Strada    |
      9. | Honda Accord   |
     10. | Honda Civic    |
         |----------------|
     11. | Mazda GLC      |
     12. | Peugeot 604    |
     13. | Renault Le Car |
     14. | Subaru         |
     15. | Toyota Celica  |
         |----------------|
     16. | Toyota Corolla |
     17. | Toyota Corona  |
     18. | VW Dasher      |
     19. | VW Diesel      |
     20. | VW Rabbit      |
         |----------------|
     21. | VW Scirocco    |
     22. | Volvo 260      |
         +----------------+
    
    .  export delimited using make.txt
    file make.txt saved
    
    . type make.txt
    make
    Audi 5000
    Audi Fox
    BMW 320i
    Datsun 200
    Datsun 210
    Datsun 510
    Datsun 810
    Fiat Strada
    Honda Accord
    Honda Civic
    Mazda GLC
    Peugeot 604
    Renault Le Car
    Subaru
    Toyota Celica
    Toyota Corolla
    Toyota Corona
    VW Dasher
    VW Diesel
    VW Rabbit
    VW Scirocco
    Volvo 260
    Backing up, your question is framed in terms of the name of the observation. In Stata terms, an observation is an entire case, row or record in the dataset and doesn't have a name so far as the software is concerned.. I imagine that you were alluding to what is true in the example just given, that the variable concerned is a kind of identifier.

    So the approach in #1 is not needed at all. If you needed something like that for another problem, this is one version with a reproducible example.
    Code:
    sysuse auto, clear
    
    keep if foreign 
    keep make 
    
    
    file open textfile using textfile.txt, text write replace
    
    local N = _N
    forvalues i = 1 / `N' {
    
    file write textfile "`= make[`i']'" _n 
    }
    
    file close textfile
    
    type textfile.txt

    Comment


    • #3
      To add a small note to the export delimited solution from Nick Cox in post #2, a review of the output of help export delimited tells us that if you do not want the first line of the output file to contain the variable names, adding the novarnames option to the export delimited command will suppress them.
      Last edited by William Lisowski; 05 Feb 2023, 09:22.

      Comment


      • #4
        Nick Cox and William Lisowski, thank you this helps a lot.

        Comment

        Working...
        X