# Re: st: Recode non-integers to positive integers

 From Tirthankar Chakravarty To statalist@hsphsun2.harvard.edu Subject Re: st: Recode non-integers to positive integers Date Sat, 22 Aug 2009 17:01:02 +0100

```Well, in your case (non-negative values) it is easy. Simply subtract 1
from the -group-ed variable:
*******************
sysuse auto, clear
tab mpg
recode mpg (18=0) // change 18 to 0
egen new = group(mpg)
replace new = new - 1
tab new
*******************

T

On Sat, Aug 22, 2009 at 4:24 PM, Frank Gallo<fjgallo@mac.com> wrote:
> Hi All,
>
> So the syntax "egen newvar = group(oldvar)" achieves my goal for one
> variable. But for another variable, I have "0" values that I want to keep
> and do not include in the grouping. I tried adding the if qualifier [egen
> newvar = group(oldvar) if oldvar > 0] but this transformed the 0s to
> missing. I would greatly appreciate any suggestions. Thank you.
>
> Best,
> Frank
>
>
> On Aug 22, 2009, at 9:56 AM, Frank Gallo wrote:
>
> Hi Mike & Jon,
>
> Thank you for your time and input. I solved my problem with the following
> syntax: egen newvar = group(oldvar). This syntax produced a new variable,
> which grouped similar values and ordered them from lowest (1) to highest
> (28). I used the tabulate command to check the recoding, which was correct.
> I apologize if I were not clearer with my original post: I am a beginner
> with Stata. Thank you.
>
> Best,
> Frank
>
> On Aug 22, 2009, at 2:02 AM, Mike Wazowski wrote:
>
> here's a step-by-step code you could modify, assume x is your original
> variable....
>
> clear
> set obs 500
> set seed 123
> gen x = round(10*runiform()+10,.5)
> gen x2 = -x
> sort x2
> by x2: gen rank=1 if _n==_N
> drop x2
> sort x
> replace rank = sum(rank)
>
> mike
>
>
>
>
> --- On Sat, 8/22/09, Frank Gallo <fjgallo@mac.com> wrote:
>
>> From: Frank Gallo <fjgallo@mac.com>
>> Subject: Re: st: Recode non-integers to positive integers
>> To: statalist@hsphsun2.harvard.edu
>> Date: Saturday, August 22, 2009, 2:33 AM
>> Hi Jon,
>>
>> Thank you very much for your input.. However, the syntax
>> generates new variable values greater than 28. The 28 unique
>> values occur many times. What I would like to accomplish,
>> for example, is that each time the value 2.87 occurs the new
>> variable value would be 1, and so on for the other unique
>> values. I apologize if I was not clear in my previous post.
>> Thank you.
>>
>> Best,
>> Frank
>>
>>
>> On Aug 21, 2009, at 9:25 PM, John Ataguba wrote:
>>
>> Hi Frank,
>>
>> There are many possible options.
>>
>> Try
>>
>> egen newvar=rank(oldvar), unique
>>
>> This should work.  Note that the oldvar is the
>> variable containing 2.87 to 6.04.  The newvar is a new
>> variable you may specify differently to the oldvar.
>>
>> I hope this helps.
>>
>> Regards
>>
>> Jon
>> ________________________________
>> From: Frank Gallo <fjgallo@mac.com>
>> To: statalist@hsphsun2.harvard.edu
>> Sent: Friday, 21 August, 2009 18:15:45
>> Subject: st: Recode non-integers to positive integers
>>
>> Hi All,
>>
>> I have a variable that has 28 unique non-integer values,
>> which range from 2.87 to 6.04. I would like to recode the
>> values to positive integers from 1 to 28 respectively. Is
>> there a simpler way instead of "recode old var
>> (2.87=1)....(6.04=28), generate(new var)" to accomplish
>> this? Thank you.
>>
>> Best,
>> Frank
>>
>
> *
> *   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/
>

--
To every ω-consistent recursive class κ of formulae there correspond
recursive class signs r, such that neither v Gen r nor Neg(v Gen r)
belongs to Flg(κ) (where v is the free variable of r).

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