Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: insheet numeric variables as strings


From   Roy Wada <[email protected]>
To   <[email protected]>
Subject   RE: st: insheet numeric variables as strings
Date   Tue, 19 May 2009 23:39:28 -0700

 
This one will handle the missing observations.
 
 
*** program here
prog drop _all
*! chewfile version 0.9.2 May2009 by [email protected]
* version 0.9.2 May2009 : fixed the empty cells
* version 0.9.1 May2009 : added str import function
* version 0.9 Aug2008 : beta
program define chewfile
version 8.0
syntax using/, [output(string) first(numlist max=1) last(numlist max=1) clear]
local parse ","
if "`first'"=="" & "`last'"=="" {
local first 1
local last .
}
if "`clear'"=="clear" {
clear
qui set obs 1
}
if `"`output'"'=="" {
tempfile dump
local output `dump'
}
tempname fh outout
local linenum = 0
file open `fh' using `"`using'"', read
qui 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
if "`clear'"=="clear" {
 tokenize `"`macval(line)'"', parse(`"`parse'"')
 local num 1
 local colnum 1
 while "``num''"~="" {
  if `"``num''"'~=`"`parse'"' {
   cap gen str3 var`colnum'=""
   if _rc~=0 {
    cap gen str3 var`colnum'=""
   }
   cap replace var`colnum'="``num''" in `linenum'
   if _rc~=0 {
    qui set obs `=`=_N'+1'
    cap replace var`colnum'="``num''" in `linenum'
   }
   *local colnum=`colnum'+1
  }
  else {
   cap gen str3 var`colnum'=""
   local colnum=`colnum'+1
  }
  local num=`num'+1
 }
}
}
file read `fh' line
}
file close `fh'
file close `outout'
end
 
 
*** make data
clear
set mem 333m
set obs 10
gen id=_n
forval n=1/20 {
gen var`n'=uniform()
}
outsheet using test1.txt, replace noname comma
 
 

*** import them as strings:
chewfile using test1.txt, clear
 
 
 
 

 

 
_________________________________________________________________
Hotmail® has a new way to see what's up with your friends.
http://windowslive.com/Tutorial/Hotmail/WhatsNew?ocid=TXT_TAGLM_WL_HM_Tutorial_WhatsNew1_052009
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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