Statalist


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

Re: st: Field Widths and Delimiters


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: Field Widths and Delimiters
Date   Fri, 9 Jan 2009 17:46:33 -0500

Prashant Shukla <[email protected]>:
Note that the first step in the second method was to turn every
variable into a string variable of the right length.  Suppose you have
used -tostring- already. If you now -recast- so that the type is the
desired length, you can then "pad" the variable with spaces to ensure
every observation is the desired length, like so:

qui ds, has(type string)
qui foreach v of varlist `r(varlist)' {
 local spaces : display _dup(245) " "
 local length = substr("`: type `v''",4,.)
 replace `v'=substr("`spaces'",1,`length' - length(`v'))+`v'
*next line also appends your delimiter "|"
 replace `v' = `v' + "|"
}

But you could also have used the -format- option on -tostring- to pad
with zeros upstream:

g t2=2
tostring t2, format(%08.0f) replace
ta t2

On Fri, Jan 9, 2009 at 5:13 PM, Prashant Shukla
<[email protected]> wrote:
> Hi Austin,
>
> So -recast- does help the fields have the desired lengths on the txt
> file. But it does not seem to be consistent, i.e, when I open up the txt
> file, the fields aren't as long as I had recasted them as in Stata.
> Also, when using -outfile- with -runtogether- option, the effects of
> -recast- do not get implemented in the txt file. Blank fields are only
> one character in length. However, -outsheet- with options -nonames- and
> -noquotes- implements the -recast- if the delimiter "|" was appended to
> the variables before hand, but not consistently as I mentioned above.
>
> I think this is because of the way I appended the character "|" at the
> end of each variable. I did the following to append "|" at the end of
> every variable:
>
> foreach var of varlist  a-z{
> replace `var' = `var' + "|"
> }
>
> Is that ok? What did you have in mind when you said append the variables
> with a "|" at the end?
>
> Also, I tried recasting the variables to the desired lengths and then
> simply using -outsheet- with the -delimiter- option to have the
> character "|" as the delimiter. But even in this case the -recast-
> didn't seem to work. Please advise. Thanks,
>
> -Prash.
*
*   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