Question

I am trying to insert a row into a SAS data set using PROC SQL and the values of existing macro variables but I'm getting the standard syntax error message. Here is an example of the code that fails:

%let viewname=MKTVIEWS.imei_ref;
%let xrc=0;
%let xmsg= ;

proc sql;
   create table results (viewname char(40), xrc numeric, xmsg char(200));
   insert into results (viewname, xrc, xmsg)
   values ( %str(%')&viewname%str(%') 
          , &xrc 
          , %str(%')%superq(xmsg)%str(%') );
quit;

Here is the error message:

ERROR 22-322: Syntax error, expecting one of the following: a quoted string, 
a numeric constant, a datetime constant, a missing value, +, -, MISSING, NULL,
USER.

Running the program without the two character macro variables works fine:

proc sql;
   create table results (viewname char(40), xrc numeric, xmsg char(200));
   insert into results (viewname, xrc, xmsg)
   values ( 'MKTVIEWS.imei_ref'
          , &xrc 
          , '' );
quit;

Clearly I'm missing something about macro quoting or something similar. I even tried using temporary macro variables rather than embedding those %STR calls to create a quoted string, but that didn't work either.

Was it helpful?

Solution

Maybe I'm missing something, but wouldn't "&viewname" do the job?

OTHER TIPS

I find it easiest to use datastep quoting functions, largely because I'm terrible at real macro quoting.

%let viewname=MKTVIEWS.imei_ref;
%let xrc=0;
%let xmsg= ;


options symbolgen mprint;
proc sql;
   create table results (viewname char(40), xrc numeric, xmsg char(200));
   insert into results (viewname, xrc, xmsg)
   values ( %sysfunc(quote(&viewname))
          , &xrc 
          , %sysfunc(quote(%superq(xmsg))) );
quit;

Does that accomplish what you're hoping?

Bob, it doesn't work, because it works. %STR masked quotes not just from macro compiler (as you expected it), but also from the program compiler in general. So in your SQL you provided value for character variable that didn't had quotes.

See the difference here, no macro stuff except of %str:

data text;
length text $50;
text=%str(%')bla bla text%str(%');
run;

data text;
length text $50;
text="%str(%')bla bla text%str(%')";
run;

In case you needed quoted strings in your table:

proc sql;
   create table results (viewname char(40), xrc numeric, xmsg char(200));
   insert into results (viewname, xrc, xmsg)
   values ( "%str(%')&viewname%str(%')"
          , &xrc 
          , "%str(%')%superq(xmsg)%str(%')" );
quit;

Of course, as already said, nobody's really good in macro quoting :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top