If your values are simple country codes like in your example (that is no embedded commas or spaces in the individual item values) I would recommend telling the users to NOT enter any commas. But that might be difficult to enforce so you might see values like this in the user generated %LET statements.
%let Countries = US, DE, IT;
%let Countries = US DE IT;
%let Countries = us, De IT;
You can convert that to a simple space delimited list using code like:
%let cleanCountries=%sysfunc(compbl(%sysfunc(translate(%qupcase(&countries),' ',','))));
This will fix the case, remove the commas and compress multiple spaces to just one. Note there is no need to macro quote the space or comma if they are inside of quotes. Translate will just convert any quotes it sees back to quotes since it matches the lists of characters convert element by element.
You can then convert that to your underscore delimited version:
%let listCountries=%sysfunc(translate(&cleanCountries,'_',' '));
You can also use your tranwrd method to convert to a list of quoted values.
%let strCountries=%unquote(%bquote('%qsysfunc(tranwrd(&listCountries,_,' '))'));
You could use commas instead of spaces between the quoted strings if you want, but note that SAS is happy to use the space delimited list instead of comma delimited list. For example when using the IN operator.
where countryCode in ('US' 'DE' 'IT');
... View more