BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
orangejuss
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
 call execute(cats('%nrstr(%raw(',Table_name,',%str(',filter,')))'));
--
Paige Miller

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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'))

 

--
Paige Miller
orangejuss
Fluorite | Level 6
Thankk you. It works but how can I apply it in
call execute(cats('%nrstr(%%raw)(',Data,',',WHERE_FILTER1,',',WHERE_FILTER2,')'));
this code?
PaigeMiller
Diamond | Level 26

You would need to add %str( and a right parenthesis ) in the proper places in the CALL EXECUTE statement.

--
Paige Miller
orangejuss
Fluorite | Level 6
I tried to write an example code:
data sample;input table_name $6. filter $30. ;DATALINES;Table1 name='DEF' and sum>=20Table2 sum>=100;RUN;
data raw_table1;input id $6. sum name $5.;DATALINES;a10300 40 ABCb10300 20 DEFc10300 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;
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
orangejuss
Fluorite | Level 6
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.

PaigeMiller
Diamond | Level 26
 call execute(cats('%nrstr(%raw(',Table_name,',%str(',filter,')))'));
--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 5201 views
  • 1 like
  • 2 in conversation