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

(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?

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

View solution in original post

6 REPLIES 6
Reeza
Super User

The approaches there, look correct. Which part do you need help with?

Bipasha
Obsidian | Level 7

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.

 

ShiroAmada
Lapis Lazuli | Level 10

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.

 

 

ShiroAmada
Lapis Lazuli | Level 10
%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;
Patrick
Opal | Level 21

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;
Bipasha
Obsidian | Level 7

Thanx Pratick. I have written similar code for the problem.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 3507 views
  • 2 likes
  • 4 in conversation