[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st:How to input a portion of a file

From   Joseph Wagner <>
Subject   Re: st:How to input a portion of a file
Date   Tue, 19 Feb 2008 08:59:14 -0500

I don't have any control over the creation of these files. If I did I wouldn't have needed to send a request for help to the list.

Sergiy Radyakin wrote:

and as a lesson, next time you are doing an experiment, don't place
any "header" information in the datafile.
If you need additional information related to the setup of the
experiment, to the subject, etc, have it in a separate file, and carry
an experimentID in your data files. Proper data organization from the
start will keep you from frustration later on.


On Feb 18, 2008 1:25 PM, n j cox <> wrote:

No, think about it: a user-written program for input would have to be
written at a fairly low level, as how could you e.g. refer to variables
that don't yet exist? You need something that reads in a line at a time.
It could be done in Mata, it could be done with a plug-in, and it could
be done with -file-, possibly, but a user-programmer would have to
re-create a lot of tedious but essential parsing of lines, consistency
checking, and so forth. Not to mention flushing buffers and similar

I think it is much, much easier to go the obvious way -- to filter the
file so that what is left behind is then amenable to one of the input
commands. You are resisting that conclusion, but I think it is inescapable.

In Stata, this seems to call for something using -file-.

In Unix -- and by extension in Windows too, as ports aplenty can be
found -- this seems to call for something like sed, awk, or perl.

Here is a demonstration program. Unix adepts will love to point out how
this is very long-winded compared with one of the utilities mentioned

To see how it works, create a -test.txt- with lines 1 to 7 containing
those numbers.

striplines test.txt, outfile(test2.txt) keep(3/7)
type test2.txt
striplines test.txt, outfile(test2.txt) replace drop(1/3)
type test2.txt

*-------------------------------- striplines.ado
*! 1.0.0 NJC 18 Feb 2008
* strip lines from ASCII file
* must specify -out-
* must specify -keep()- or -drop()-
* may specify -replace-
program striplines
version 8.2
syntax anything(name=infile) , outfile(str) ///
[replace keep(numlist) drop(numlist) ]

if "`keep'" != "" & "`drop'" != "" {
di as err "specify keep() or drop()"
exit 198
if "`keep'" == "" & "`drop'" == "" {
di as err "specify keep() or drop()"
exit 198

// filenames and handles
tempname hi ho
file open `hi' using `"`infile'"', r
file open `ho' using `"`outfile'"', w `replace'

local i = 1

if "`keep'" != "" {
file read `hi' line
while r(eof) == 0 {
local tokeep : list i in keep
if `tokeep' file write `ho' `"`line'"' _n
file read `hi' line
local ++i
else {
file read `hi' line
while r(eof) == 0 {
local todrop : list i in drop
if !`todrop' file write `ho' `"`line'"' _n
file read `hi' line
local ++i

file close `ho'
di _n `"`outfile' created"'


Joseph Wagner

I can get the file into excel and the columns line up perfectly. If I
open the file in Crimson editor the columns appear to be tab-delimited
after all (apparently why I was able to use -insheet-). That said, is
there a user-written program that I have missed that will perform
-insheet- like action but with options limiting the data?

* For searches and help try:

* For searches and help try:
Joseph H. Wagner, M.P.H.
Lifespan Health Research Center
Wright State University Boonshoft School of Medicine
3171 Research Blvd.
Kettering, OH 45420-4014

(937) 775-1494 (LHRC office)
(937) 775-1456 (fax)

Visit the Lifespan Health Research Center Home Page at:
* For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index