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
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( )))));
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;
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.
Here is a simplified version without parameter checking.
%macro qlist(list);
(%unquote(%str(%')%sysfunc(tranwrd(&list,%str( ),','))%str(%')))
%mend qlist;
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( )))));
Thanks Tom, That's exactly what I was looking for!
Rb1
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;
This worked to me!
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.
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.
%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
Thanks Xia, that's two very interesting solutions.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.