Help using Base SAS procedures

Copying and renaming a file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Copying and renaming a file

Hello

I'm having great difficulty achieving something that I'm sure shouldn't be a problem. All I want to do is get SAS to copy a file (not a SAS dataset) to a new name in the same directory. The new name contains the current year/month yyyy-mm and  this is stored in a macro variable.

I've tried "x call copy" but dos keeps stopping to prompt me to tell it if it's a file or directory I'm copying, which is no good at 1am when the program will run. I read you can pass parameter /i to turn this off, but it doesn't seem to work. 

I've also tried x calling a batch file and passing the year/month as a parameter, but I can't get this to work either.

Does any know how I achieve this?


Accepted Solutions
Solution
‎12-20-2011 05:56 AM
Respected Advisor
Posts: 4,736

Copying and renaming a file

Posted in reply to JamesNewcombe

Hi

I believe what you're missing is "option noxwait;'

http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#exittemp.htm

Below code works for me.

options noxwait;
%let date=20111220;
data _null_;
/*  x "copy c:\temp\testdata.txt c:\temp\testdata_&date..txt";*/
  rc= system("copy c:\temp\testdata.txt c:\temp\testdata_&date..txt");
  put rc=;
run;

Using "system" instead of 'x' allows you to store a return code (zero if successful) and do some extra stuff based on the return code.

HTH

Patrick

View solution in original post


All Replies
Solution
‎12-20-2011 05:56 AM
Respected Advisor
Posts: 4,736

Copying and renaming a file

Posted in reply to JamesNewcombe

Hi

I believe what you're missing is "option noxwait;'

http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#exittemp.htm

Below code works for me.

options noxwait;
%let date=20111220;
data _null_;
/*  x "copy c:\temp\testdata.txt c:\temp\testdata_&date..txt";*/
  rc= system("copy c:\temp\testdata.txt c:\temp\testdata_&date..txt");
  put rc=;
run;

Using "system" instead of 'x' allows you to store a return code (zero if successful) and do some extra stuff based on the return code.

HTH

Patrick

Occasional Contributor
Posts: 11

Copying and renaming a file

Thanks Patrick, I knew there had to be an easy answer!

Occasional Contributor
Posts: 11

Re: Copying and renaming a file

Ah...

I still have one problem. The directory path to the file I want to copy has spaces in it. Normally in DOS you'd just put quotes around the path, but the string in the system command is already in quotes.

Is there a way around this, apart from renaming the path?

Thanks.

Super User
Super User
Posts: 8,086

Re: Copying and renaming a file

Posted in reply to JamesNewcombe

Use the QUOTE function.  This will put quotes around the string and also double any existing quotes so that they get based through to the OS properly.

data _null_;

   oldname = 'This file has spaces.txt';

   newname = 'This_file_doesnot.txt';

   rc= system(quote(catx(' ','copy',quote(trim(oldname)),quote(trim(newname)))));

  put rc=;

run;

Or if you want to do with macro statements.

%let oldname = "This file has spaces.txt";

%let newmame = This_file_doesnot.txt ;

%sysexec(%sysfunc(quote(copy &oldname &newname)));

Occasional Contributor
Posts: 13

Re: Copying and renaming a file

[ Edited ]

Hi, I know this is an old post, there are a few other possibilities for copying an external file to a seperate location. I'm not sure what the prompt is for the copy, as I don't run into that issue, and perhaps it's because I have a different version (9.4) but I noticed that the original X Copy wasn't correct. 

 

In an X Copy, the X and copy are outside the quotes, and the file you want to copy and where/what the new name will be need to be in their own seperate quotes.

 

X COPY "&PATH_DM.\Trial_logs.sas7bdat" "&PATH_DM.\Trail_logs_&TODATE..sas7bdat";

 

Another option (Which I personally like much better) is the systask command option. This means of copying has many more controls than RC or a typical X Copy. You have the option to Taskname, waitfor, status, ect. 

 

SYSTASK COMMAND "COPY &PATH_DM.\Trail_logs.sas7bdat &PATH_DM.\Trail_logs_&TODATE..sas7bdat" WAIT MNAME=GDDINCOPY TASKNAME=GDDINCOPY STATUS=TASK; WAITFOR GDDINCOPY; 

 

 

There are other options, though IMO I think these 2 do it all. 

 

Happy Coding.

Monster

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 10369 views
  • 0 likes
  • 4 in conversation