Your SAS programs, embedded in web apps and elsewhere

Where In clause with Apostrophes

Accepted Solution Solved
Reply
Super Contributor
Posts: 497
Accepted Solution

Where In clause with Apostrophes

I am trying to run a where in proc sql clause where a character string is fed into the list.  When I feed the string in it errors out.  Am I using the right structure?

%let sumlev = 'AX', 'AH';

proc sql noprint;

where term ='Fall' and college in (&sumlev);

quit;


Accepted Solutions
Solution
‎05-27-2015 08:18 PM
Trusted Advisor
Posts: 1,301

Re: Where In clause with Apostrophes

Posted in reply to DavidPhillips2

This is fine, once you have valid SQL, anyway.

However, given your other post about adding the apostrophes, which used macro quoting, you need to %unquote() the macro variable for it to work correctly:

%let a=%str(%');

%let sex=&a.M&a.,'F';

proc sql;

select * from sashelp.class where sex in (%unquote(&sex.));

quit;

View solution in original post


All Replies
Solution
‎05-27-2015 08:18 PM
Trusted Advisor
Posts: 1,301

Re: Where In clause with Apostrophes

Posted in reply to DavidPhillips2

This is fine, once you have valid SQL, anyway.

However, given your other post about adding the apostrophes, which used macro quoting, you need to %unquote() the macro variable for it to work correctly:

%let a=%str(%');

%let sex=&a.M&a.,'F';

proc sql;

select * from sashelp.class where sex in (%unquote(&sex.));

quit;

Super Contributor
Posts: 497

Re: Where In clause with Apostrophes

Thanks for answering my questions about crazy quotes.  I’ve been doing a lot of work with quotes lately and it’s a learning curve.

Super User
Posts: 10,035

Re: Where In clause with Apostrophes

Posted in reply to DavidPhillips2

Not need to use quote character . For your situation , use symget() would be better;

%let sumlev = AX AH ;

proc sql noprint;

where term ='Fall' and symget(sumlev)  contains strip(college) ;

quit;

Trusted Advisor
Posts: 1,301

Re: Where In clause with Apostrophes

One minor correction

symget('sumlev')

And yes, this will work and does not require the quoting or delimiting of the list and is viable with the example data (although may not be with your actual data, depending...)

Super User
Posts: 10,035

Re: Where In clause with Apostrophes

Opps. Did you see Arthur.T before ?  He didn't appear here for a very long time.

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 546 views
  • 0 likes
  • 3 in conversation