Announcement

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

  • Outfile formatting question

    My goal is to create a flat .txt file (a file with no delimiter where variables are defined by character positions) for another person who does not use Stata. This person has been given similar files from my agency in the past, but not from me personally. I am trying to replicate the format of those files as best I can. I am trying to minimize the amount of code that the person to whom I am sending this file will have to change when they read the file I intend to send.

    My problem is that in the past they have received flat files without blank space between variables, and I am unable to replicate this situation in Stata.

    I am using the -outfile- command (with the wide and noquote options) to create the flat file.

    Below is an example of my code (using auto data), and infix code that could be used to import the output file. As you can see in the -infix- code there are two blank spaces between each variable.
    My actual data contain both numeric and string variables. I am using Stata version 13.

    Code:
    sysuse auto,clear
    
    outfile * using "auto.txt", wide noquote
    Code:
    infix str make 1 - 18  ///  
    price 21 - 28  ///  
    mpg 31 - 38  ///  
    rep78 41 - 48  ///  
    headroom 51 - 56  ///  
    trunk 59 - 66  ///  
    weight 69 - 76  ///  
    length 79 - 86  ///  
    turn 89 - 96  ///  
    displacement 99 - 106  ///  
    gear_ratio 109 - 114  ///  
    foreign 117 - 124  ///  
    using "auto.txt", clear
    Is there a way to create a flat file that does not contain these two blank spaces between each variable?

    Thank you,
    Jack

  • #2
    Welcome to Statalist, Jack.

    The answer to your question is yes, but the way I know how to do it is more complicated than using outfile. The key is the file command, see help file for documentation. Here's sample code, with the first three variables from auto.data.
    Code:
     sysuse auto, clear
    file open fi using foo.txt, write text replace
    // make a first line of column numbers for this demo
    file write fi "123456789-123456789-123456789-1234567890" _newline
    forvalues i=1/`=_N' {
    file write fi %-18s (make[`i']) %8.0f (price[`i']) %8.0f (mpg[`i']) _newline
    }
    file close fi
    type foo.txt, lines(10)
    Code:
    . type foo.txt, lines(10)
    123456789-123456789-123456789-1234567890
    AMC Concord           4099      22
    AMC Pacer             4749      17
    AMC Spirit            3799      22
    Buick Century         4816      20
    Buick Electra         7827      15
    Buick LeSabre         5788      18
    Buick Opel            4453      26
    Buick Regal           5189      20
    Buick Riviera        10372      16
    .
    And note that indeed price ends in column 26 and make ends in column 34.

    Comment


    • #3
      Thank you William! I was able to you use your method and produce a file of the exact format I had desired.

      Comment

      Working...
      X