I have a macro named raw and first it checks data existence, then if exist it creates new data from it using given filters. And it reads datanames and filters from a table.
%macro raw(data,where_filter1,where_filter2);
%if %sysfunc(exist(raw_&data)) %then %do;
data new_&data;
set raw_&data;
if &where_filter;
run;
/*code continues*/
%end;
%else %do;
data _null_;
file print;
put 'Data set raw_&data does not exist';
run;
%end;
%raw(Data2,name='john' and id='KN30' and num>10,sum>100 and id='xyz');
CALL EXECUTE("%raw("||Data||","||where_filter1||","||where_filter2||");");
if I run the code like this; it does not check table existence correctly. Always gives else do result. 'Data set raw_&data does not exist'
call execute(cats('%nrstr(%%raw)(',Data,',',WHERE_FILTER1,',',WHERE_FILTER2,')'));
if I run like this it checks table existence correctly,
but gives ERROR: All positional parameters must precede keyword parameters. Because of where_filter1 and where_filter2 parameters includes quotes and > < characters it doesn't work correctly. How can I fix that? Thanks in advance for your help.
call execute(cats('%nrstr(%raw(',Table_name,',%str(',filter,')))'));
The equal sign in your where filters makes SAS think you are using a keyword parameter. SAS doesn't know the equal sign is supposed to be used in a DATA step. So to fix this, try
%raw(Data2,%str(name='john' and id='KN30' and num>10), %str(sum>100 and id='xyz'))
You would need to add %str( and a right parenthesis ) in the proper places in the CALL EXECUTE statement.
Sorry, but I no longer try to decipher poorly formatted code.
Please paste your code into the window that appears when you click on the little running man icon, and format it so that its not very long unreadable lines and so that there is one SAS command on a line.
I tried to write an example code:
data sample;
input table_name $6. filter $30. ;
DATALINES;
Table1 name='DEF' and sum>=20
Table2 sum>=100
;
RUN;
data raw_table1;
input id $6. sum name $5.;
DATALINES;
a10300 40 ABC
b10300 20 DEF
c10300 10 XYZ
;
RUN;
%macro raw(data,where_filter);
%if %sysfunc(exist(raw_&data)) %then %do;
data new_&data;
set raw_&data;
if &where_filter;
run;
/*code continues*/
%end;
%else %do;
data _null_;
file print;
put 'Data set raw_&data does not exist';
run;
%end;
%MEND;
DATA _NULL_;
SET SAMPLE;
call execute(cats('%nrstr(%%raw)(',%str(Table_name),',',%str(filter),')'));
RUN;
Sorry, I replied to this by email, I did not notice that the code looked bad.
call execute(cats('%nrstr(%raw(',Table_name,',%str(',filter,')))'));
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.