My usual approach when faced with such things is to introduce a backslash character to "escape" the problem character. So, for example try swapping %% with \% or \%\% or %\% - hack rapidly and then if it works, try to understand why afterwards.
This approach comes from many years of working between the unix command line interpreter and shell scripts, which were infuriatingly similar but different. I suspect there is something about the way the function evaluator parses things (presumably interactively in A/View) and the way the macro command reader parses things non-interactively.
You might also try examining the command window immediately after successfully evaluating the function; many times I have found the differences between what's in the function builder and what needs to go into a command file (usually quotes, escape characters, and so on) in this way.