Statalist


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

Antwort: RE: st: Field Widths and Delimiters


From   Richard Ochmann <[email protected]>
To   [email protected]
Subject   Antwort: RE: st: Field Widths and Delimiters
Date   Tue, 13 Jan 2009 11:44:03 +0100

you need the linebreak before the *replace* in order to tell Stata to use the command -replace- for your line `v'=substr("`spaces'",1,`length' - length(`v'))+`v' and not to add it to the local *length* you define there, which is apparently invalid.

best, rich


[email protected] schrieb: -----


An: <[email protected]>
Von: "Prashant Shukla" <[email protected]>
Gesendet von: [email protected]
Datum: 12.01.2009 11:23PM
Thema: RE: st: Field Widths and Delimiters

Austin,

Thanks so much! I tried the first segment of code that you provided and
it works perfectly. I just had a quick observation/question so I can
understand the code properly and replicate/modify it going forward.

So when I had it in the Do-File Editor as:

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'
replace `v' = `v' + "|"
}

Stata does not like it and gives a message "invalid 'replace'" referring
to the replace in second line inside the curly brackets.

However, if I insert a line as follows:

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'
replace `v' = `v' + "|"
}

I get the required result. Why is that?

Thanks so much Austin, I sincerely appreciate it. I might bug you again
on this as I begin modifying the code for similar purposes but different
formats. Thanks again,

-Prash.

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Austin
Nichols
Sent: Friday, January 09, 2009 4:47 PM
To: [email protected]
Subject: Re: st: Field Widths and Delimiters

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/

*
*   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