The problem is one of precision, you define `pat02' as a float (assuming you
haven't set a different default type) so it can only hold 8/9 significant
digits accurately. You could get it to work with more vars if it were a
double or long instead of a float, but it would make more sense to make
`pat02' a string so you you can go to 80 (or more with Stata/SE) variables.
You would just need to define it as string when generated (and you wouldn't
need the format statement on the next line) and then put quotes around
`which' when you assign values to it.
Michael Blasnik
[email protected]
----- Original Message -----
From: "Jun Xu" <[email protected]>
To: <[email protected]>
Sent: Sunday, February 16, 2003 2:13 AM
Subject: st: RE: loop [ignore previous one (from this message), sorry!!!]
> Stata-listers/Nick,
>
> This email might be kind of long, but to explain this problem clearly,
could
> you bear with me for a second :( Thanks a lot.
>
> Thanks a lot for your help, and I have managed to write an ado file that
> seems to work fine till I found out that it won't work with a varlist of
> more than 8. I have attached our previous Q&A emails at the end.
>
> Notes:
>
> `imax': here is a macro that I have grabbed using 2^`nvars'-1.
> `nvars': is a macro containing number of variables in the varlist.
>
> part of the ado file that's relevant to my question
> ************************************************************************
> ......
> ......
> ......
> *create this response pattern variabl and replace the value in future
> quietly gen `pat02'=.
> format `pat02' %0`nvars'.0f
>
> *create other tempvars
>
> forval i = 1 / `imax' {
> qui inbase 2 `i'
>
>
> local which : di %0`nvars'.0f `r(base)'
>
> *local a="`which'"
> *di "`which'"
>
> forval j = 1 / `nvars' {
> local char = substr("`which'",`j',1)
>
> *I only invoke `which' once within this loop
> .......
> .......
> .......
>
> }
>
>
> quietly replace `pat02'=`which' if _n==`i'
>
> *I don't see what's wrong here and I have used the following three *lines
> kind of check the instantaneous value of `which' and it is a *binary code
> everytime, but `pat02'[`i'] becomes not binary after the *binary code
> 100000000, so I strongly suspect either I misused the *replace statement,
or
> there is something unique to "local which : di %*0`nvars'.0f `r(base)'"
the
> command that Nick taught me in his email to *my question, which I might
not
> understand well. The puzzle here is *that no problems with 8 variables
and
> less, but not 9 variables and *above as you will see in the output, where
> the binary codes will have *other values than 0 and 1.
>
>
> *di _n(1) "`which'"
> *di `pat02'[`i']
> *di %0`nvars'.0f `which'
>
> *replacing values of other tempvar
>
> ......
> ......
> ......
>
> }
> ......
*
* 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/