I’m adding this answer only to justify my assertion that the present question is “somehow related” to How can I check whether two control sequences have the same name? (see the comments).
Werner’s solution is great, but it has a slight drawback and a characteristic that, depending on what you want to do, can turn out to be a useful feature, or not.
The useful feature, that you might want to disable, is that it finds the control sequence being sought for at any level of expansion; the drawback is that the test is not purely expandable, since it is executed in the stomach.
The following approach uses two steps: in the first step, that requires digestion (since it needs to execute assignments), the name to be searched for is defined; having done that—and this is the second step—a purely expandable test can be performed to find out whether the replacement text of another macro contains the control sequence that has been declared before. The test does not expand the replacement text any further (that is, only one level of expansion is considered).
% Check for availability of names:
\ifx\@empty##2\@empty % if ##2 is empty
Ducks living in Duckburg: \UncleScrooge, \DonaldDuck,
\Huey, \Dewey, \Louie, and many others.%
Mices living in Micetown: \MickeyMouse, \MinnieMouse,~\ldots
I'm secretly in love with \DonalDuck!%
Test for Duckburg: the macro
\ the saved name.
Test for Micetown: the macro
\ the saved name.
You may uncomment various section of this code to see different behaviors. Some of the commented-out code serves diagnostic purposes.
This code requires nothing more than the original Knuth’s TeX. The test is completely independent of the meaning, and type (macro, primitive command, …), of the control sequence being searched for, which can also be undefined. Drawback: you cannot reliably test for the presence of a macro named “
\@empty”; this can be cured in the usual way with
\detokenize (without destroying the property of the test of being purely expandable).