Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: translation between binary system and the decimal system.


From   "Nichols, Austin" <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: translation between binary system and the decimal system.
Date   Tue, 12 Oct 2004 11:01:55 -0400

Whoops... a critical omission!

su Cat_id /* find out how many dummies to create */
local max=int(ln(r(max))/ln(2))
tempname id
cap gen `id'=Cat_id
forval x = `max'(-1)0 {
 gen byte Cat`x' = ((`id'-mod(`id',2^`x'))>0)
 replace `id'=mod(`id',2^`x')
 }

-----Original Message-----
From: Nichols, Austin [mailto:[email protected]]
Sent: Tuesday, October 12, 2004 11:00 AM
To: '[email protected]'
Subject: RE: st: translation between binary system and the decimal
system.


qui su Cat_id /* find out how many dummies to create */
local max=1+int(ln(r(max))/ln(2))
tempname id
cap gen `id'=Cat_id
forval x = `max'(-1)1 {
 gen byte Cat`x' = ((`id'-mod(`id',`x'))>0)
 replace `id'=`id'-mod(`id',`x')
 }

The above code may also be adapted (with some work) 
to decode an ID variable that encodes a series of 
categorical variables with arbitrary integer codes, 
in the form Cat_id=2^Cat1*3^Cat2*5^Cat3...*primeX^CatX

-----Original Message-----
> Cat_id Cat4 Cat3 Cat2 Cat1
> 1 0 0 0 1
> ...
> 10 1 0 1 0
> 11 0 0 1 1
**note the last line is inconsistent with the stated problem, and should
read
**11 1 0 1 1
-----Original Message-----
**The following, due to Neil Shephard, only works for Cat_id in [0,4]
forval x = 1/4{
  gen Cat`x' = cond(Cat_id == `x', 1,0)
}
*
*   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/
*
*   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