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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

5 REPLIES 5
Patrick
Opal | Level 21

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

JamesNewcombe
Calcite | Level 5

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

JamesNewcombe
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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)));

XMonsterX
Obsidian | Level 7

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 21037 views
  • 0 likes
  • 4 in conversation