The EXIST() function will check if a dataset exists or not. In your case where you are generating multiple librefs you will need to define the libref first before you can use it to test for the existence of the dataset. You can use the LIBNAME() function to assign the libref and then you can use the EXIST() function to test if the particular member is in that library.
I am not going to repeat your convoluted code generation logic but here is an example you can use to see how it works.
data example;
length libref engine $8 member $32 path $200;
path='some folder name';
engine='spde';
member='sometablename';
libref='test';
rc=libname(libref,path,engine);
found = exist(catx('.',libref,member));
if not found the put member= 'NOT FOUND IN ' path=;
run;
You don't seem to understand how CALL EXECUTE() works and when the generated code works. That is another reason to use the write a file method. You are much less likely to get confused about when the generated code will run.
It would probably be easier to create a macro that confirms the list of files before trying to copy them.
Something like:
%macro copy_members(inlib,outlib,memlist);
proc sql noprint;
select nliteral(memname) into :memlist separated by ' '
from dictionary.members
where libname="%upcase(&inlib)"
and memtype='DATA'
and findw("&memlist",memname,' ','sit')
;
quit;
%if &sqlobs %then %do;
proc copy inlib=&inlib outlib=&outlib memtype=data;
select &memlist ;
run;
%end;
%mend copy_members;
Example usage:
332 options mprint; 333 %copy_members(inlib=sashelp,outlib=work,memlist=cars class xyz); MPRINT(COPY_MEMBERS): proc sql noprint; MPRINT(COPY_MEMBERS): select nliteral(memname) into :memlist separated by ' ' from dictionary.members where libname="SASHELP" and memtype='DATA' and findw("cars class xyz",memname,' ','sit') ; MPRINT(COPY_MEMBERS): quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.03 seconds cpu time 0.04 seconds MPRINT(COPY_MEMBERS): proc copy inlib=sashelp outlib=work memtype=data; MPRINT(COPY_MEMBERS): select CARS CLASS ; MPRINT(COPY_MEMBERS): run; NOTE: Copying SASHELP.CARS to WORK.CARS (memtype=DATA). NOTE: There were 428 observations read from the data set SASHELP.CARS. NOTE: The data set WORK.CARS has 428 observations and 15 variables. NOTE: Copying SASHELP.CLASS to WORK.CLASS (memtype=DATA). NOTE: There were 19 observations read from the data set SASHELP.CLASS. NOTE: The data set WORK.CLASS has 19 observations and 5 variables. NOTE: PROCEDURE COPY used (Total process time): real time 0.01 seconds cpu time 0.01 seconds
Just write the code you want to generate to a file. Then once you get it working you just use %INCLUDE to run the code.
Use CATX() or CAT() to simplify constructing the names.
%let path1=FOLDER1;
%let path2=FOLDER2;
filename code temp;
data _null_;
length path file $200 ;
array ld {9} $8 ('20210729' '20210730' '20210731' '20210801' '20210802' '20210803' '20210804' '20210805' '20210806');
array lcp {5} $2 ('A' 'B' 'C' 'D' 'E');
array bc {2} $4 ('auto' 'habi');
do i=1 to dim(lcp);
if lcp(i) in ('A','B') then L=1;
else L=2;
do j=1 to L;
put ;
path = catx('/',"&path1.",lcp(i),bc(j));
put "libname src1 spde " path :$quote. ';' ;
path = catx('/',"&path2.",lcp(i),bc(j));
put "libname dest1 spde " path :$quote. ';' ;
put 'proc datasets lib=scr1;';
put ' copy out=dest1 memtype=data;' ;
put ' select ' ;
do k=1 to dim(ld);
fname= cats(lcp(i),bc(j),'_prm_d',ld(k));
put @5 fname ;
end;
put ' ;';
put ' run;';
put 'quit;';
end;
end;
run;
%include code / source2;
Also instead of the arrays you can use the power of the DO loop.
Consider things like:
do lcp = 'A', 'B' ,'C' ,'D' ,'E';
do date='29JUL2021'd to '06AUG2021'd;
So you basically want to loop over dates and copy datasets from one location to the other? You can do that with a few nested loops and the FCOPY function.
@Kurt_Bremser wrote:
So you basically want to loop over dates and copy datasets from one location to the other? You can do that with a few nested loops and the FCOPY function.
Good idea for normal files, but probably not the right way to copy SPDE datasets.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.