I've the below data step which is working fine. However when I tried to convert it into macros, it's not working as I got the error as 'ERROR: Required operator not found in expression: filename(fref,"&dir1.") = 0'
%let dir1=/var/data/server/files; /*get file details from dir1*/ data files; fref = "DIR1"; if filename(fref,"&dir1.") = 0 then do; did = dopen(fref); if did ne 0 then do; do i = 1 to dnum(did); name = dread(did,i); if upcase(scan(name,-1,".")) = "CSV" then output; end; did = dclose(did); end; rc = filename(fref); end; keep name; run; /*macro variables for digit value from each file*/ data _null_; set files; digits = scan(name,5,'_'); call symputx(cats("id",_n_),digits); run;
When I convert it into macros,
%if <condition> %then %do; data files; fref = "DIR1"; %if filename(fref,"&dir1.") = 0 %then %do; did = dopen(fref); %if did ne 0 %then %do; %do i = 1 %to dnum(did); name = dread(did,i); %if upcase(scan(name,-1,".")) = "CSV" %then output; %end; did = dclose(did); %end; rc = filename(fref); %end; keep name; run; /*macro variables for digit value from each file*/ data _null_; set files; digits = scan(name,5,'_'); call symputx(cats("id",_n_),digits); run; %end;
Once the macro version works, I want to add this portion in the existing macro program and so I want the solution only in macro.
Log:
SYMBOLGEN: Macro variable DIR1 resolves to /var/data/server/files ERROR: Required operator not found in expression: filename(fref,"&dir1.") = 0
Any help?
From what you've posted, the only macro statements required in your code are the beginning %IF statement and the finishing %END statement. Everything else can stay exactly like your first non-macro example.
%let dir1=/var/data/server/files;
%if < condition > %then %do;
/*get file details from dir1*/
data files;
fref = "DIR1";
if filename(fref,"&dir1.") = 0
then do;
did = dopen(fref);
if did ne 0
then do;
do i = 1 to dnum(did);
name = dread(did,i);
if upcase(scan(name,-1,".")) = "CSV" then output;
end;
did = dclose(did);
end;
rc = filename(fref);
end;
keep name;
run;
/*macro variables for digit value from each file*/
data _null_;
set files;
digits = scan(name,5,'_');
call symputx(cats("id",_n_),digits);
run;
%end;
You replaced the IF into %IF, in that case sas expects to find a macro variable like &xxx following the %IF.
To convert a sas code into macro you should convert it like in next example:
suppose your code is:
data want;
set have;
if var = 1234 then do ; .... end;
run;
then while converting it to macro you need to choose what objects should be supplied
(a dataset name, a variable name, a variable value, part of a statement etc.);
You don't convert IF into %IF.
%macro doit(input, value);
data want;
set &input;
if var = &value then do ; .... end;
run;
%mend doit;
%doit(have,1234);
Can't we convert my code without parameters? My dataset code is different than in your example as I can't pass the value to the Parameters like you shown. I would like to know how to convert my code as macro and without Parameters and any hardcoding like you shown in your example.
There are so many conceptual errors in your attempt to convert the data step into macro, i don't know where to start ...
- all function calls that are part of macro-logic must be wrapped in %sysfunc
- but: why do you changed the if-blocks to %if-blocks at all? you are still in a data step
@David_Billa wrote:
How to wrap with %sysfunc?
Maxim 1: Read the Documentation. It provides the details, and lots of examples.
From what you've posted, the only macro statements required in your code are the beginning %IF statement and the finishing %END statement. Everything else can stay exactly like your first non-macro example.
%let dir1=/var/data/server/files;
%if < condition > %then %do;
/*get file details from dir1*/
data files;
fref = "DIR1";
if filename(fref,"&dir1.") = 0
then do;
did = dopen(fref);
if did ne 0
then do;
do i = 1 to dnum(did);
name = dread(did,i);
if upcase(scan(name,-1,".")) = "CSV" then output;
end;
did = dclose(did);
end;
rc = filename(fref);
end;
keep name;
run;
/*macro variables for digit value from each file*/
data _null_;
set files;
digits = scan(name,5,'_');
call symputx(cats("id",_n_),digits);
run;
%end;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.