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   daniel klein <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: recoding a long-level categorical variable
Date   Fri, 13 Sep 2013 19:44:29 +0200

Something like

g x = .
loc i 0
forv j = 5760(15)7215 {
    di "replace x = `++i' if inrange(y, `j', `= `j' + 15')"
}

migh work for you. Note however, that x ranges from 1 to 98 (not 96)
and the ranges overlap (e.g. 5775 in y) is mapped to 1 and 2 in x. You
will haev to decide how you want to handel this.

Best
Daniel
-- 

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