DATA Step, Macro, Functions and more

Quote and Unquote a list?

Accepted Solution Solved
Reply
Contributor
Posts: 47
Accepted Solution

Quote and Unquote a list?

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


Accepted Solutions
Solution
‎04-28-2015 10:31 AM
Super User
Super User
Posts: 6,502

Re: Quote and Unquote a list?

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


All Replies
Super User
Super User
Posts: 6,502

Re: Quote and Unquote a list?

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;

Contributor
Posts: 47

Re: Quote and Unquote a list?

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.

Super User
Super User
Posts: 6,502

Re: Quote and Unquote a list?

Here is a simplified version without parameter checking.

%macro qlist(list);

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

%mend qlist;

Solution
‎04-28-2015 10:31 AM
Super User
Super User
Posts: 6,502

Re: Quote and Unquote a list?

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( )))));

Contributor
Posts: 47

Re: Quote and Unquote a list?

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

Rb1

Contributor
Posts: 37

Re: Quote and Unquote a list?

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;
Occasional Contributor
Posts: 8

Re: Quote and Unquote a list?

This worked to me!

Super User
Super User
Posts: 7,407

Re: Quote and Unquote a list?

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.

Contributor
Posts: 47

Re: Quote and Unquote a list?

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.

Super User
Posts: 9,682

Re: Quote and Unquote a list?

%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

Contributor
Posts: 47

Re: Quote and Unquote a list?

Thanks Xia, that's two very interesting solutions. 

Contributor
Posts: 47

Re: Quote and Unquote a list?

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 2616 views
  • 8 likes
  • 6 in conversation