2

I have a macro variable I need to use within PROC SQL. The way it resolves appears to have perfect syntax, but I am getting a syntax error and I'm not sure why;

%let test = mytext;
PROC SQL;
CREATE TABLE myTalbe&test AS
SELECT DISTINCT
    a.column
FROM
    tablename a
WHERE
    a.column = %bquote('&test')
;QUIT;

The error I get throws a red line under the resolved text, 'mytext', and says

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, a missing value, (, *, +, -, ALL, ANY, BTRIM, CALCULATED, CASE, INPUT, PUT, SELECT, SOME, SUBSTRING, TRANSLATE, USER.

I don't feel like this error applies here. If I hard code in 'mytext' it works fine. Am I missing something right under my nose? Can anyone lend me a hand?

Thanks!!!

1
  • We don't encourage use of enterprise-guide tag unless you're asking about EG functionality (as opposed to SAS language functionality). Commented Jun 5, 2017 at 16:10

2 Answers 2

3

The macro quoting is confusing the SAS parser. For this program I would remove the use of %bquote() and just use double quotes instead of single quotes so that the macro variable reference will resolve.

WHERE a.column = "&test"

If your are actually generating pass thru SQL into a system that requires the use of single quotes for string literals then you will need to use %unquote() to remove the macro quoting.

... from connection to ... ( ...
WHERE a.column = %unquote(%bquote('&test'))
... ) ...
Sign up to request clarification or add additional context in comments.

1 Comment

I always use %bquote without fail when I have pass-throughs. I would have swore I tried double quotes first and I had issues, before posting this issue. I gotta give it to Allan though, since he answered first! Thanks, Tom!
2

The BQUOTE function tries to resolve the value immediately at execution time. Try removing it and using double quotes instead:

%let test = mytext;
PROC SQL;
CREATE TABLE myTalbe&test AS
SELECT DISTINCT
    a.column
FROM
    tablename a
WHERE
    a.column = "&test"
;QUIT;

4 Comments

This generates the same error, this time showing my desired output with double quotes
NOTE: Line generated by the macro function "BQUOTE". 77 "mytext" This shows up before the error
apologies yes - bquote is a funny one! Answer updated
I would have swore that I had already done this, and it didn't work. This use of just double quotes appears to be giving me the desired output. Thanks, Allan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.