Statalist


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

AW: st: RE: efficiently creating 1/0 variables from existing string y/n variables


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: st: RE: efficiently creating 1/0 variables from existing string y/n variables
Date   Mon, 28 Sep 2009 13:00:19 +0200

<> 

" Is there a simple way to then rename the resulting many new_variable  "


You want to look at -renpfix-, -rename-`s brother :-)


HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Michael
McCulloch
Gesendet: Montag, 28. September 2009 00:53
An: [email protected]
Betreff: Re: st: RE: efficiently creating 1/0 variables from existing string
y/n variables

Thank you Martin, the pre-labeled feature of -encode- is brilliant.
Dropping the old variables will be simple.
Is there a simple way to then rename the resulting many new_variable  
names back their old title?
	e.g.: combining many of these commands into a shorter step?
	rename new_etoh etoh
	rename new_smk smk?
Michael
	


On Sep 27, 2009, at 3:29 PM, Martin Weiss wrote:

>
> <>
>
> -encode- comes to mind...
>
>
>
> **************
> clear*
>
> input  str1 alc_now
> y
> y
> y
> y
> y
> y
> n
> y
> end
>
> compress
> list, noobs
>
> encode alc_now, gen(etoh_now)
> **************
>
>
> HTH
> Martin
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Michael  
> McCulloch
> Sent: Montag, 28. September 2009 00:25
> To: Statalist
> Subject: st: efficiently creating 1/0 variables from existing string  
> y/n
> variables
>
> Hello,
> I have a long list of string variables which contain only "y", "n", or
> are missing.
> I'm converting them to new numeric variables with values 1 if  the old
> variable is "y", and 0 if "n".
> For example:
> 	gen etoh_now=alc_now=="y"
>
> This gives, correctly:
> .. list alc_now etoh_now, clean
>        alc_now   etoh_now
>   1.         y          1
>   2.         y          1
>   3.         y          1
>   4.         y          1
>   5.         y          1
>   6.         y          1
>   7.         n          0
>   8.         y          1
>
> Is there a more efficient way to do this, where I can change "y" to 1
> and "n" to 0 without creating a new variable and manually dropping the
> old variable?
>
>
>
>
>
>
>
> Michael McCulloch, LAc MPH PhD
> Pine Street Foundation
> 124 Pine Street
> San Anselmo, CA 94960-2674
> tel:	415-407-1357
> fax: 	206-338-2391
> [email protected]
>
>
>
>
>
>
> *
> *   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/



Michael McCulloch, LAc MPH PhD
Pine Street Foundation
124 Pine Street
San Anselmo, CA 94960-2674
tel:	415-407-1357
fax: 	206-338-2391
[email protected]






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