Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: recoding a long-level categorical variable


From   Richard Goldstein <[email protected]>
To   statalist <[email protected]>
Subject   Re: st: recoding a long-level categorical variable
Date   Fri, 13 Sep 2013 13:58:16 -0400

Note first that the FAQ asks you not to send private email

I am a consultant and generally treat private email as though it is from
someone who might hire me; in this case ...

I believe that there is still a typo as "7185" appears in both the first
and second lines of your recode command

try this: gen x=floor((y-5760)/15)

Rich

On 9/13/13 1:53 PM, Jonathan Furszyfer wrote:
> Thank you, Richard. Yes --it was a typo. 
> 
> The actual values are y=[5760-7200]. The code is:
> 
>  recode y (7185 / max = 96 "7185 to 7200" )  ///
>                        (7170/ 7185 = 95 "7170 to 7185" )  ///   
>                        (7155/ 7170 = 94 )  "7155 to 7170"///
>                        . ...
>                         (5760/5775 = 1 "5760 to 5775"), generate (x)
> 
> Any advice or suggestions to make it shorter?
> 
> Jon-
> 
> -----Original Message-----
> From: Richard Goldstein [mailto:[email protected]] 
> Sent: Friday, September 13, 2013 1:30 PM
> To: [email protected]
> Cc: Jonathan Furszyfer
> Subject: Re: st: recoding a long-level categorical variable
> 
> 
> If you have made a typo or 2, then there is; note that (7230-5760)/15=98; so, do you want 1-98?
> 
> note also, however, that in your code below, you are repeating some numbers (e.g., 7215 appears on both the first (=96) and second (=95) lines; this appears to be an error
> 
> if you want each new value of a new variable to increase by 1 for each set of 15 consecutive numbers in the old variable, then, yes, you could put this is a forval loop; however, this may not be your situation so please clarify
> 
> Rich
> 
> On 9/13/13 1:19 PM, Jonathan Furszyfer wrote:
>> Dear All,
>>
>> For methodological reasons, I have to create a 96-level categorical 
>> variable, say x,
> where x={1,2,3,...,96}. The categories of x, however, are conditioned on other variable, y.
>>
>> y is a continuous variable. More specifically, y=[5760,7230]. N=1,450,708.
>>
>> In order to generate x, then, I used recode:
>>
>> recode y (7215 / 7230 = 96 )  ///
>>                         (7200/ 7215 = 95 )  ///
>>                         (7185/ 7200 = 94 )  ///
>>                         .
>>                         (5760/5775 = 1), generate (x)
>>
>> In other words, using recode takes 96 lines of coding (or less if you 
>> add several
> "(#/#)" in a single line).
>> I was wondering if there is an easier method to simplify 96 lines of 
>> coding,
> probably using loops, foreach and/or forvalues.
>>
>> Thank you very much!
>>
>> Best,
>> Jon-
> 
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index