Tom already gave the correct answer: you need to %unquote() it.
Just to add, I believe this is a bug (though I'm not sure SAS views it as such). The value should be %unquoted automatically before it causes problems, but it doesn't always happen. The rule I learned is that if you turn on MPRINT, and the code in the log looks valid while generating an error, try %UNQUOTE. The SAS docs mention this:
https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p1f5qisx8mv9ygn1dikmgba1lmmu.htm&docsetVersion=9.4&locale=en
What to Do When Automatic Unquoting Does Not Work
... In rare cases, masking text with a macro quoting function changes how the word scanner tokenizes the text. (The word scanner and tokenization are discussed in SAS Programs and Macro Processing and and Macro Processing.) For example, a single or double quotation mark produced by resolution within the %BQUOTE function becomes a separate token. The word scanner does not use it as the boundary of a literal token in the input stack. If generated text that was once masked by the %BQUOTE function looks correct but SAS does not accept it, you might need to use the %UNQUOTE function to restore normal tokenization. (emphasis added)
When you use %TRIM the code works because %TRIM unquotes it. As would %UPCASE, or other macro functions that don't explicitly quote the value they return.
... View more