Hi,
I created the %getValues macro below, which should return all values of a column in a data set, within single quotes, separated by a space.
[PRE]
%macro getValues(dsname,colname);
%let dsid=%sysfunc(open(&dsname));
%let vartype=%sysfunc(vartype(&dsid,%sysfunc(varnum(&dsid,&colname))));
%do %while(not(%sysfunc(fetch(&dsid))));
%if (&vartype=C) %then
%do;
%str(%')%sysfunc(compress(%sysfunc(getvarc(&dsid,%sysfunc(varnum(&dsid,&colname))))))%str(%')
%end;
%else
%do;
%sysfunc(compress(%sysfunc(getvarn(&dsid,%sysfunc(varnum(&dsid,&colname))))))
%end;
%end;
%let dsid=%sysfunc(close(&dsid));
%mend getValues;
[/PRE]
It works when I %put it:
[PRE]15 %put %getValues(SASHELP.CLASS,NAME);
'Alfred' 'Alice' 'Barbara' 'Carol' 'Henry' 'James' 'Jane' 'Janet' 'Jeffrey' 'John' 'Joyce' 'Judy' 'Louise' 'Mary' 'Philip' 'Robert'
'Ronald' 'Thomas' 'William'
[/PRE]
However, when I include the macro in a proc sql, it doesn't work:
[PRE]
15 proc sql;
16 create table x as
17 select *
18 from sashelp.class
19 where name in (%getValues(SASHELP.CLASS,NAME))
_
22
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant,
a missing value, (, -, SELECT.
_
76
ERROR 76-322: Syntax error, statement will be ignored.
MPRINT(GETVALUES): 'Alfred' 'Alice' 'Barbara' 'Carol' 'Henry' 'James' 'Jane' 'Janet' 'Jeffrey' 'John' 'Joyce' 'Judy' 'Louise'
'Mary' 'Philip' 'Robert' 'Ronald' 'Thomas' 'William
20 ;
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
21 quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
[/PRE]
In the log I see that the last quote is missing.
When I replace the %str(%') in the macro with double quotes or other characters, it works fine. But I need the single quotes to prevent macro resolution.
What am I doing wrong?
Thanks,
Bart