Statalist The Stata Listserver


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

Re: st: Re: Encode string in batch way?


From   "Jia Xiangping" <[email protected]>
To   [email protected]
Subject   Re: st: Re: Encode string in batch way?
Date   Thu, 21 Sep 2006 18:15:24 +0800

Michael, thank you again. It works well now. I  am now  aware of the
usefulness of "capture", which I did not expect to use.

But, like you mentioned, how to fulfill this work in another way, to
check the variable type within the loop?  I am very curious at it
because that is the way I meant to do.




On 9/20/06, Michael Blasnik <[email protected]> wrote:
You can either check the variable type within the loop or else just use the
capture command:

forvalue i=1(1)614 {
 cap encode v`i', gen(vv`i') label(yesno)
 if _rc==0 {
    drop v`i'
    rename vv`i' v`i'
 }
}

Michael Blasnik


----- Original Message -----
From: "Jia Xiangping" <[email protected]>
To: "statalist" <[email protected]>
Sent: Wednesday, September 20, 2006 6:31 AM
Subject: st: Encode string in batch way?


> Dear list friends,
>
> I am dealing with a datafile in which a batch of variables are
> mistakenly coded as Y or N in the type of string. You know, string is
> useless to me and I have to encode them. It is easy to encode
> individual variables via following way:
>
> label define yesno 0 "N" 1 "Y"
> encode v1, gen(vv1) label(yesno)
> drop vv1
> rename vv1 v1
>
> However, you know, there are numerous problems in the file like this,
> and I try to encode them a batch way.
> label define yesno 0 "N" 1 "Y"
> forvalue i=1(1)614 {
> encode v`i', gen(vv`i') label(yesno)
> drop v`i'
> rename vv`i' v`i'
> }
>
> Then I was informed
> not possible with numeric variable
> r(107);
> end of do-file
> r(107);

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


--
Xiangping JIA
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index