@ydang wrote:
I'm now using symget. I would like to run the macro with a call execute, so I will get a list of all the files in the folder. In the end some of these files would need to be deleted.
I've now created the full path in variable pathname in the dataset deletefolders, this also contains the pathname. The deletefolder dataset has multiple folders listed with haveseq as sequence number.
I'm still getting the same error when using symget.
%macro basefile3 ( haveseq, pathname ); data have_&haveseq; rc=filename('xx',symget('pathname')); did=dopen('xx'); do i=1 to dnum(did); fname=dread(did,i); output; end; rc=dclose(did); run; %mend basefile3;
data _null_; set Deletefolders; call execute(cats('%basefile3(',haveseq||','||pathname,')')); run;
First point is fix the CALL EXECUTE call so that the macro execute AFTER the data step. Wrap the macro triggers in %NRSTR() so that the call to the macro is pushed to run instead of the code the macro generates being pushed to run. Also remove the || since you are already using CATS() to do the concatenation.
call execute(cats('%nrstr(%basefile3)(',haveseq,',',pathname,')'));
But why make separate datasets for each folder? Ditch the macro and just put the DOPEN(),DREAD() logic into a data step and make ONE dataset with all of the file names.
data DeleteFiles;
set Deletefolders;
rc = filename('xx',pathname);
did=dopen('xx');
if did then do;
do filenum=1 to dnum(did);
length fname $256 ;
fname=dread(did,filenum);
output;
end;
rc=dclose(did);
end;
else put 'WARNING: Unable to open ' pathname 'as a directory.';
drop rc did;
run;
If you want to use a macro then perhaps use one like https://github.com/sasutils/macros/blob/master/dirtree.sas
data _null_;
set deletefolders end=eof;
if _n_=1 then call execute('%nrstr(%dirtree)(out=deletefiles,directory=');
else call execute('|');
call execute(pathname);
if eof then call execute(')');
run;
Add MAXDEPTH=1 to the macro call if you don't want it to recurse into subdirectories.
... View more