# Re: st: Re: decimal to binary conversion

 From n p To statalist@hsphsun2.harvard.edu Subject Re: st: Re: decimal to binary conversion Date Wed, 10 Mar 2004 06:13:34 -0800 (PST)

```A less elegant solution (including a ... loop through
observations). Caution v1 .. v6 are strings!

gen var2=""
local i=1
local max=_N
while(`i'<=`max')	{
local temp=var1[`i']
inbase 2 `temp'

replace var2=r(base) if `i'==_n
local i=`i'+1
}
local i=6
while(`i'>=1)	{
gen v`i'=substr(var2,7-`i',1)
replace v`i'="0" if v`i'==""
local i=`i'-1
}

order var1 var2 v1 v2 v3 v4 v5 v6

Nikos Pantazis
Biostatistician

--- Jeffrey Simons <jsimons@usd.edu> wrote:
> I want to take a variable that is in decimal format
> and convert it to a six
> digit binary number. Then take that and form six new
> indicator variables.
>
> For example,
>
> . inbase 2 29
> 11101
>
> Provides this. I would like to write a do file to
> loop through the values of
> a variable and do this. Converting the answer to six
> new indicator
> variables.
>
> So if var1 took on values of 15, 29, 35, ...
> I'd like to get
>
> var1    v1a v1b v1c v1d v1e v1f
> 15      0   0   1   1   1   1
> 29      0   1   1   1   0   1
> 35      1   0   0   0   1   1
> ....
>
> Any assistance would be most appreciated.
>
> <<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>
> Jeffrey Simons, Ph.D.
> Department of Psychology
> The University of South Dakota
> 414 East Clark St.
> Vermillion, SD 57069
> Phone:      605-677-5353
> Fax:        605-677-3195
> <<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>
>
> *
> *   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/

__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com
*
*   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/
```