DATA Step, Macro, Functions and more

Renaming a dataset during a proc copy with datetime

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Renaming a dataset during a proc copy with datetime

[ Edited ]

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


Accepted Solutions
Solution
‎01-22-2018 04:17 AM
Respected Advisor
Posts: 4,557

Re: Renaming a dataset during a proc copy with datetime

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


All Replies
Super User
Posts: 22,874

Re: Renaming a dataset during a proc copy with datetime

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

Occasional Contributor
Posts: 17

Re: Renaming a dataset during a proc copy with datetime

[ Edited ]

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.

 

Frequent Contributor
Posts: 113

Re: Renaming a dataset during a proc copy with datetime

[ Edited ]

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.

 

 

Frequent Contributor
Posts: 113

Re: Renaming a dataset during a proc copy with datetime

%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;
Solution
‎01-22-2018 04:17 AM
Respected Advisor
Posts: 4,557

Re: Renaming a dataset during a proc copy with datetime

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;
Occasional Contributor
Posts: 17

Re: Renaming a dataset during a proc copy with datetime

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

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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