Hi,
I have this macro:
%macro list;
"AAA",
"BBB",
"CCC"
%mend;
and I want to change multiple quote (") with single (').
Can you help me?
Thanks
Luca
Generally I don't put quotes around items in my lists, and I will use a macro like Richard DeVenezia's excellent %SepList https://www.devenezia.com/downloads/sas/macros/index.php?m=seplist to add quotes or change delimiters.
But for a quick replacement of quotes, you can use TRANSTRN or similar, you just have to dive down the macro quoting rabbit hole...
%macro list_parameters();
"AAA",
"BBB",
"CCC"
%mend;
%put my list is: %unquote(%sysfunc(transtrn(%bquote(%list_parameters()),%str(%"),%str(%')))) ;
Returns:
21 %macro list_parameters(); 22 "AAA", 23 "BBB", 24 "CCC" 25 %mend; 26 27 %put my list is: %unquote(%sysfunc(transtrn(%bquote(%list_parameters()),%str(%"),%str(%')))) ; my list is: 'AAA', 'BBB', 'CCC'
The %unquote() may not be needed, but sometimes SAS doesn't %unquote automatically and things break.
Hmm, LIST is a reserved name, so you can't have such a macro : )
1 %macro list; ERROR: Macro LIST has been given a reserved name. ERROR: A dummy macro will be compiled. 2 "AAA", 3 "BBB", 4 "CCC" 5 %mend; 6 7 %put %list() ; ERROR: Statement is followed by extraneous characters (. %LIST should be followed by a line number, line numbers separated with : or -, or a semicolon.
It would be unusual to use a macro to store only a list. It's more common to use a macro variable:
1 %let list= 2 "AAA", 3 "BBB", 4 "CCC" 5 ; 6 7 %put my list is: &list ; my list is: "AAA", "BBB", "CCC"
Do you really have macro that generates a list, or do you have a macro variable?
Sorry I change the name in the post but the real name of the macro is list_parameters.
I know that It is unusual but I start from another process that produce this list that I can't chage.
Thanks,
Luca
Where are you planning to use this macro? Data step? PROC? Somewhere else? Can you provide a wee bit of code where this macro would be used?
What situations arise where "AAA" can't be used but 'AAA' is okay?
I need to use the information of the macro in a format native of teradata which not accept multiple quotation marks but single ones
Generally I don't put quotes around items in my lists, and I will use a macro like Richard DeVenezia's excellent %SepList https://www.devenezia.com/downloads/sas/macros/index.php?m=seplist to add quotes or change delimiters.
But for a quick replacement of quotes, you can use TRANSTRN or similar, you just have to dive down the macro quoting rabbit hole...
%macro list_parameters();
"AAA",
"BBB",
"CCC"
%mend;
%put my list is: %unquote(%sysfunc(transtrn(%bquote(%list_parameters()),%str(%"),%str(%')))) ;
Returns:
21 %macro list_parameters(); 22 "AAA", 23 "BBB", 24 "CCC" 25 %mend; 26 27 %put my list is: %unquote(%sysfunc(transtrn(%bquote(%list_parameters()),%str(%"),%str(%')))) ; my list is: 'AAA', 'BBB', 'CCC'
The %unquote() may not be needed, but sometimes SAS doesn't %unquote automatically and things break.
@Quentin , @PaigeMiller , @Tom
There are plenty nice solutions to the question, but I can't help myself and share one which uses SAS Packages and SAS Packages Framework.
Enable the framework, install, and load MacroArray package:
filename packages "%sysfunc(pathname(work))"; /* setup WORK as temporary directory for packages */
filename SPFinit url "https://raw.githubusercontent.com/yabwon/SAS_PACKAGES/main/SPF/SPFinit.sas";
%include SPFinit; /* enable the framework */
%installPackage(macroArray) /* install macroArray package */
%loadPackage(macroArray) /* load the package content into the SAS session */
/*
%helpPackage(macroArray,'%array()')
%helpPackage(macroArray,'%do_over()')
*/
Use %array() and %do_over() macros:
data _null_;
run;
%macro list_parameters();
"AAA",
"BBB",
"CCC"
%mend;
%array(myQlist[3] $ 12 (%list_parameters()), function=quote(strip(myQlist(_I_)), "'"), macarray=Y);
%let singleQuoted_list = %do_over(myQlist,between=%str(,));
%put &=singleQuoted_list.;
data _null_;
run;
(data steps are just "markers")
Bart
%macro listas;
"AAA",
"BBB",
"CCC"
%mend;
%put %sysfunc(quote(%sysfunc(dequote(%quote(%listas))),%str(%')));
If the macro generates a string that is longer than 64K bytes you will have trouble using it with macro functions.
But you could easily use it with a data step in this way.
filename code temp;
data _null_;
length value $200 ;
file code;
put '%macro list_participants_sq;';
any=0;
do value=%list_participants;
value=quote(trim(value),"'");
if any then put ',';
put value +(-1) @;
any=1;
end;
put '%mend;';
run;
%include code ;
Example:
1 %macro list_participants; 2 "Abc", 3 " xyz", 4 "Don't know" 5 %mend; 6 %put %list_participants; "Abc", " xyz", "Don't know" 7 8 filename code temp; 9 data _null_; 10 length value $200 ; 11 file code; 12 put '%macro list_participants_sq;'; 13 any=0; 14 do value=%list_participants; 15 value=quote(trim(value),"'"); 16 if any then put ','; 17 put value +(-1) @; 18 any=1; 19 end; 20 put '%mend;'; 21 run; NOTE: The file CODE is: ..... NOTE: 4 records were written to the file CODE. The minimum record length was 6. The maximum record length was 28. NOTE: DATA statement used (Total process time): real time 0.02 seconds cpu time 0.03 seconds 22 %include code ; 27 28 %put %list_participants; "Abc", " xyz", "Don't know" 29 %put %list_participants_sq; 'Abc', ' xyz', 'Don''t know'
I am struck by the thought that the problem is really in the creation of this string with double-quotes. However this string is being created, that's where you need to change so that it has single quotes. This seems much easier and much more logical than creating strings with double quotes and then changing the quotes to single quotes.
I think you don't need data step inside the macro:
%macro foo(a);
%qsysfunc(translate(%superq(a),%str(%'),%str(%")))
%mend;
%put %foo(%str("A", "B", "C"));
Bart
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.