Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: recode non-numerical values


From   Joerg Luedicke <joerg.luedicke@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: recode non-numerical values
Date   Mon, 2 May 2011 22:54:08 -0400

On Mon, May 2, 2011 at 10:16 PM, Jet <lsj555@gmail.com> wrote:
> Hi, everyone,
>
>      I have a question regarding matching of nonnumerical values.
> Suppose the original data looks like this:
>
>   ID     Drug ID
>    1      d06662
>    2      d06665
>    3      d04878
>    4      d05048
>
>    Now  suppose d06662 is a drug for hypertension, which I want to
> code into "1", and all else into "0". I tried to destring the
> variable, but it does not allow me doing so since it contains
> characters. Any suggestion to handle this situation?
>

You have several options:

1) You could simply type:

gen drug= DrugID=="d06662"

(and maybe add
replace drug= . if  DrugID==""
in case it is empty)

2) Use encode (-help encode-) to create a categorical variable for
drug type with the strings as value labels

3) If the d, the first digit letter in DrugID, carries no information
(and if all drug ids have a meaningless first digit) you could remove
it and then convert it into a numerical variable with destring. For
example, you could type:

 gen drug=substr( Drug_ID, 2,5)

(see -help substr-)


J.

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index