Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: spaces between variables


From   baum <baum@bc.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: spaces between variables
Date   Thu, 15 Aug 2002 08:39:33 -0400

--On Thursday, August 15, 2002 2:33 -0400 Nick Winter wrote:


With the file command, you can roll your own version of this fairly
easily:

   version 6
   file open myfile using <filename>, text write replace
   local N=_N
   forval i=1/`N' {
      local line : di _col(1) varname1[`i'] _col(25) varname2[`i'] ...
      file write myfile `"`line'"' _n
   }
   file close myfile
   version 7

I should note that the original request was for a single space between
variables -- then the fifth line would look like this:

   local line : di varname1[`i'] " " varname2[`i'] " " varname3[`i'] ...
And in that case it would be quite straightforward to build up the macro to be written with a forvalues loop over the variables to be written, allowing such a routine to accept a varlist of indeterminate length and generate the desired single-space-delimited output. However, when using space delimiting, one must always worry about string variables with embedded spaces. If there is no risk of that, this would work quite nicely. Here is a quick and dirty hack that nevertheless seems to work (well, watch out for string variables):


. adotype ssconcat
~:ado:personal:ssconcat.ado:

* ssconcat: write contents of varlist to an external file with space delimiters
* cfb 2815
program define ssconcat,rclass
version 7.0
syntax varlist using/, [replace]
file open myfile using `using', text write `replace'
local N=_N
forval i=1/`N' {
local out
foreach v of local varlist {
local elt = `v'[`i']
local out "`out'`elt' "
}
file write myfile `"`out'"' _n
}
file close myfile
end

. ssconcat price mpg headroom using testss,replace

. type testss
4099 22 2.5
4749 17 3
3799 22 3
4816 20 4.5
7827 15 4

.


Kit



*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




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