(spun off of this original thread)
I have to copy all files from one library to another with renaming all of them. For example, XYZ file should be renamed as 'XYZ_datetime'.
Anybody have any idea how to do that?
Something like below could do.
data work.abc1;
set sashelp.class;
run;
data work.abc2;
set sashelp.class;
run;
data work.abcXXXXXXXXXXXXXXXXXXXXXXX;
set sashelp.class;
run;
%let dttm=%sysfunc(datetime());
proc sql noprint;
select
memname, cats(memname,'=',memname,'_',put(&dttm,b8601dt.))
into
:cp_list separated by ' ',
:rn_list separated by ' '
from dictionary.tables
where
libname='WORK'
and memname like 'ABC%'
/* only select DS names which don't get longer than 32 characters */
and length(cats(memname,'=',memname,'_',put(&dttm,b8601dt.)))<=32
;
quit;
%put %nrbquote(&=cp_list);
%put %nrbquote(&=rn_list);
libname outds 'c:\temp';
proc datasets lib=outds nolist;
copy in=work out=outds;
select &cp_list;
run;
change &rn_list;
run;
quit;
The approaches there, look correct. Which part do you need help with?
I have to rename only the files I am copying from the source library (automated approach) with concatenating timestamp with them without affecting existing files in the target library. Also, I don't want to rename the files in the source library.
Before adding a datetime suffix, you have to check and consider the length of your existing dataset name first. SAS is only capable of 32 characters when naming or RENAMING tables and/or columns.
Just how you want the datetime suffix to look like?
Examples:
mmddyy_hhmmss - 13 additional characters
mmddyyhhmmss- 12 additional characters
mmddyyhhmm- 10 additional characters
OLD dataset name + datetime Suffix > 32 is an automatic error.
%let srclib=SASHELP;
%let tgtlib=WORK;
proc sql noprint;
create table COPYLIST(where=(length(RENAME) <=32)) as
select MEMNAME, compress(catx("_",MEMNAME,put(datetime(),datetime16.)),":") as
rename from DICTIONARY.TABLES
where
upcase(LIBNAME)="&srclib." and upcase(MEMTYPE)='DATA';
select count(MEMNAME) into :n from COPYLIST;
%let n=&n.;
select MEMNAME, RENAME into :MEM1-:MEM&n, :REN1-:REN&n. from COPYLIST;
quit;
%macro CopyThenRename;
proc datasets lib=&srclib. nolist;
copy out=&tgtlib. MT=DATA;
select %do i=1 %to &n.; &&mem&i. %end; ;
quit;
proc datasets lib=&tgtlib. nolist;
change
%do i=1 %to &n.;
&&mem&i. = &&ren&i.
%end; / mt=data;
run;
quit;
%mend;
%CopyThnRname;
Something like below could do.
data work.abc1;
set sashelp.class;
run;
data work.abc2;
set sashelp.class;
run;
data work.abcXXXXXXXXXXXXXXXXXXXXXXX;
set sashelp.class;
run;
%let dttm=%sysfunc(datetime());
proc sql noprint;
select
memname, cats(memname,'=',memname,'_',put(&dttm,b8601dt.))
into
:cp_list separated by ' ',
:rn_list separated by ' '
from dictionary.tables
where
libname='WORK'
and memname like 'ABC%'
/* only select DS names which don't get longer than 32 characters */
and length(cats(memname,'=',memname,'_',put(&dttm,b8601dt.)))<=32
;
quit;
%put %nrbquote(&=cp_list);
%put %nrbquote(&=rn_list);
libname outds 'c:\temp';
proc datasets lib=outds nolist;
copy in=work out=outds;
select &cp_list;
run;
change &rn_list;
run;
quit;
Thanx Pratick. I have written similar code for the problem.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.