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

st: Use a few observations from a tab-delimited or csv file

From   Roy Wada <>
To   <>
Subject   st: Use a few observations from a tab-delimited or csv file
Date   Wed, 20 Aug 2008 10:47:46 -0700

>I have a file that is currently in csv format (or I could easily
>convert it to tab-delimited).  It is fairly large: roughly 80,000
>observations and 2,200 variables.

You can chew files using -file- command. I have another version 
that could do it by columns, but that would be slow. The program 
assumes variable names at the top.

* creating data
set mem 333m
set obs 100
gen id=_n
forval n=1/2000 {
	gen var`n'=uniform()
outsheet using test1.txt, replace
keep in 5/10
save test1, replace

prog drop _all

*! chewfile version 0.9 Aug2008 by
program define chewfile
version 8.0
syntax using/, first(integer) last(integer) output(string)
tempname fh outout
local linenum = 0
file open `fh' using `"`using'"', read
file open `outout' using `"`output'"', write replace
file read `fh' line
while r(eof)==0 {
local linenum = `linenum' + 1
	if `linenum'==1 | `linenum'>=`first' & `linenum'<=`last' {
	*display %4.0f `linenum' _asis `"`macval(line)'"'
	file write `outout' `"`macval(line)'"' _n
file read `fh' line
file close `fh'
file close `outout'

chewfile using test1.txt, first(6) last(11) output(mystuff.txt)

* checking
insheet using mystuff.txt, clear
append using test1

Get thousands of games on your PC, your mobile phone, and the web with Windows®.
*   For searches and help try:

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