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

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

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;

 

 

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18

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

 

 

 

David_Billa
Rhodochrosite | Level 12

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.

andreas_lds
Jade | Level 19

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
Rhodochrosite | Level 12
How to wrap with %sysfunc?
SASKiwi
PROC Star

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;

 

 

SAS Innovate 2025: Register Now

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!

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
  • 8815 views
  • 7 likes
  • 6 in conversation