BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RB1Kenobi
Quartz | Level 8

Hello

I'm using SAS9.3.

Is anyone aware of a macro statement to remove quotes from a list, so that the same list could be used for multiple purposes?  

Example below where a list requires a quoted and unquoted version

/* Using two macros - this is what I am trying to avoid. */

%let name_quoted='Mary' 'Philip';

%let name_unquoted =Mary Philip;

proc report data=sashelp.class;

title1 "Data for &name_unquoted.";   /* list without quotes */

where name in (&name_quoted);   /* list requires quotes */

run;

All ideas are welcomed, however I was thinking there was a macro function that might work for this scenario?

RB1

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Two things to try.

1) Using the QUOTE() function to add quotes when the strings include quotes.

TITLE %sysfunc(quote(Data for &name_quoted)) ;

2) Use the CATQ() function to add quotes when strings do not include quotes.

%let list=a b ;

%put %sysfunc(catq('1a',%sysfunc(translate(&list,%str(,),%str( )))));

View solution in original post

12 REPLIES 12
Tom
Super User Tom
Super User

Personally I have been using a utility macro to add the quotes when needed.

%let list =Mary Philip;

proc report data=sashelp.class;

title1 "Data for &list.";   /* list without quotes */

where name in %qlist(&list);   /* list requires quotes */

run;

RB1Kenobi
Quartz | Level 8

Hi Tom

I had to google "utility macro" and I've just glanced at this paper (http://www2.sas.com/proceedings/sugi30/029-30.pdf). 

I ran your example above but %Qlist was not recognized. So, is your macro %qlist something you've created as an autocall macro (could I see that code please?), or is it something that is there in the installation but not mapped on my machine?

I tend to use enterprise guide by the way, version 5.1.

Tom
Super User Tom
Super User

Here is a simplified version without parameter checking.

%macro qlist(list);

(%unquote(%str(%')%sysfunc(tranwrd(&list,%str( ),','))%str(%')))

%mend qlist;

Tom
Super User Tom
Super User

Two things to try.

1) Using the QUOTE() function to add quotes when the strings include quotes.

TITLE %sysfunc(quote(Data for &name_quoted)) ;

2) Use the CATQ() function to add quotes when strings do not include quotes.

%let list=a b ;

%put %sysfunc(catq('1a',%sysfunc(translate(&list,%str(,),%str( )))));

RB1Kenobi
Quartz | Level 8

Thanks Tom, That's exactly what I was looking for!

Rb1

brucehughw
Obsidian | Level 7

I have a similar problem. I have several dates (see longer proc sql statements) that I'd like to put in a string so that I don't need to enter them twice. The hardcoded dates work (longer code, at bottom), but the code at the top does not. I can't figure out how to quote them in SAS. Thanks for any/all help.

%let site = KBOS;
%let dateStr = '18Dec2015'd ;
options symbolgen;
proc sql;
	select * from mylib.analysisStatus
		where		site = "&site" AND date in quote(&dateStr) 
			order by date;
quit;


proc sql;
	update mylib.analysisStatus
		set analysisStatus = 6
			where		site = "&site" and date in ('18Dec2015'd, '24Jan2016'd, '23Jan2016'd );
	select * from mylib.analysisStatus
		where		site = "&site" AND date in ('18Dec2015'd, '24Jan2016'd, '23Jan2016'd ) 
			order by date;
quit;
invisibleman
Fluorite | Level 6

This worked to me!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, my first question would be why.  Your example doesn't really give any information about what this method would add to a process. For more complicated use of lists I always recommend storing in a dataset, which can be manipulated using base SAS techniques.  With this type of macro variables code gets complicated and unreadble very quickly.

RB1Kenobi
Quartz | Level 8

Fair question RW9!  I had thought of putting lists into a dataset but I had a nagging feeling that there might be a function to do the task that I wasn't aware of.

Ksharp
Super User

%let name_unquoted =Mary Philip;

%let name_quoted= %sysfunc(prxchange(s/(\w+)\s+(\w+)/"\1" "\2"/,-1,&name_unquoted));

%put  &name_quoted;

"Mary" "Philip"

And you don't need to transform it . use symget() instead .

proc report data=sashelp.class;

title1 "Data for &name_unquoted.";   /* list without quotes */

where symget ("name_unquoted")  contains strip(name)  ;   /* list requires quotes */

run;

Message was edited by: xia keshan

RB1Kenobi
Quartz | Level 8

Thanks Xia, that's two very interesting solutions. 

RB1Kenobi
Quartz | Level 8

Be aware that using a list in a macro variable (e.g. %let mvar= A B C D) has a character limit which will cause an error message to occur.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 12 replies
  • 9152 views
  • 8 likes
  • 6 in conversation