9. Let $n$ be an integer greater than 2, and let $a_{n}$ be the largest $n$-digit number that is neither the sum of two perfect squares nor the difference of two perfect squares.
(1) Find $a_{n}$ (expressed as a function of $n$);
(2) Find the smallest value of $n$ such that the sum of the squares of the digits of $n$ is a perfect square.
9. (1) $a_{n}=10^{n}-2$. First, $10^{n}-1$ can be expressed as the difference of two perfect squares: $10^{n}-1=9 \times \frac{10^{n}-1}{9}=\left(\frac{9+\frac{10^{n}-1}{9}}{2}\right)^{2}-\left(\frac{\frac{10^{n}-1}{9}-9}{2}\right)^{2}$, and since $\frac{10^{n}-1}{9}$ is odd, the conclusion holds.

Second, $10^{n}-2$ satisfies the condition. If $10^{n}-2$ can be expressed as the difference of two perfect squares, then it must be congruent to 0, 1, or 3 modulo 4. However, $10^{n}-2 \equiv 2(\bmod 4)$, which is a contradiction.

Furthermore, if $10^{n}-2$ can be expressed as the sum of two perfect squares, then it must be divisible by 4 or congruent to 2 modulo 8. However, $10^{n}-2$ is not divisible by 4, and $10^{n}-2 \equiv 6(\bmod 8)(n>2)$. This is also a contradiction!
(2) It is easy to see that $9^{2}(n-1)+64=k^{2}$, thus $9^{2}(n-1)=(k-8)(k+8)$. Since $n \geqslant 3$, and $-8 \not \equiv 8(\bmod 9)$, it must be that $81 \mid k-8$ or $81 \mid k+8$. In the former case, $k_{\min }=89, n=98$; in the latter case, $k_{\min }=73, n=66$. In summary, $n_{\min }=66$.