Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: assigning codes to variables in bulk


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: assigning codes to variables in bulk
Date   Thu, 23 Feb 2006 12:27:55 -0500

Well, you could put all those commands in one long command like so:
#delimit;
forvalues i=1 to 16 {;
 g jobstatus`i'= 86 *(jobnum`i'== 003)+ 77*(jobnum`i'== 004)
    +...+
     8 *(jobnum`i'== 909);
};

but that is still an awful lot of typing.  Another way is make a
dataset of 500obs with just the two codes, and -merge- this on to your
original data, after applying -reshape- to your original data.  But
that is also a lot of work.

I like this approach (it seems more easily extensible and more
transparent, somehow):

#delimit;
la def jobstat 86 "003";
la def jobstat 77 "004", modify;
 ...
la def jobstat  8 "909", modify;
tempname t;
forvalues i=1 to 16 {;
 la val jobnum`i' jobstat;
 decode jobnum`i' , g(`t');
 g jobstatus`i'=real(`t');
 drop `t';
};


On 2/23/06, Marino, Jennifer <[email protected]> wrote:
> I am doing the following and fervently believe that there are more
> elegant, faster ways to do this. (I am working in Stata 8.2 but will be
> updating to 9 any day now.) Occupational history codes jobnum1-jobnum16
> each take one of 500 values from 003 to 909, and I am assigning a
> Nam-Powers status value (1 to 100) to each occupation held.
>
> forvalues i=1 to 16{
> gen jobstatus`i'=.
> }
>
> forvalues i=1 to 16{
>  replace jobstatus`i'= 86 if jobnum`i'== 003
> }
> forvalues i=1 to 16{
>  replace jobstatus`i'= 77 if jobnum`i'== 004
> }
> ...
> forvalues i=1 to 16{
>  replace jobstatus`i'= 08 if jobnum`i'== 909
> }
>
> It's not a problem to me to do this in this kludgy fashion, but I'd like
> to learn how Stata experts would do it. Please enlighten me. Thank you
> very much.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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