BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
luca87
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

View solution in original post

11 REPLIES 11
Quentin
Super User

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?

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
luca87
Obsidian | Level 7

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

PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
luca87
Obsidian | Level 7

I need to use the information of the macro in a format native of teradata which not accept multiple quotation marks but single ones

Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
yabwon
Onyx | Level 15

@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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26
%macro listas;
"AAA",
"BBB",
"CCC"
%mend;

%put %sysfunc(quote(%sysfunc(dequote(%quote(%listas))),%str(%')));
--
Paige Miller
Tom
Super User Tom
Super User

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'
pink_poodle
Barite | Level 11
Something like
%macro foo(a);
data _null_;
b = %sysfunc (translate (&a,%str(%'),%str(%")));
set;
%mend;

%foo(a = %str(("A", "B", "C"));
%put b;
'A', 'B', 'C'
(this is "under-baked", still some errors)
see also here:
https://marc.info/?l=sas-l&m=116624823380265&w=2
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 1676 views
  • 5 likes
  • 6 in conversation