DATA Step, Macro, Functions and more

Copy file from one directory to another using X command

Accepted Solution Solved
Reply
Contributor
Posts: 64
Accepted Solution

Copy file from one directory to another using X command

Dear Experts,

 

I am trying to copy the file which created in run getting an error saying the directory specified is incorrect.I am trying to achieve it in macros and while copying i am removing the date string as well.Please have a look the code

 

%let rptpath = 'R:\CDW Reporting\Ad-Hoc\WR#_JAS0031 -Walls Regional Hospital Hainey\Data Out\';
%let rptname = 'Walls Regional Hospital Hainey Report';
%let rptdate = today();
%let rptdatefmt = YYMMDDd10.;
%let path=&rptpath;

%put &report;

%let path=&rptpath;
%let name=&rptname ;
%let date=&rptdate;
%let format=&rptdatefmt;


X'COPY "&path\&name || ' ' || put(&date,&format).xlsx" "C:\Users\UCS1MKP\Desktop\y\&rptname.xlsx"';


Accepted Solutions
Solution
‎01-03-2017 04:31 AM
Contributor
Posts: 42

Re: Copy file from one directory to another using X command

Posted in reply to KurtBremser

Hi this code will handle files with spaces in name of the file, if you are still looking for a solution

 

X %unquote(%str(%'copy "C:\file with spaces.txt" "C:\file with spaces2.txt" /y%'));

 

Anyway, take the advice given and try to avoid such file names.

View solution in original post


All Replies
Super User
Posts: 7,854

Re: Copy file from one directory to another using X command

Posted in reply to ambadi007

Do not use quotes in

%let rptpath = 'R:\CDW Reporting\Ad-Hoc\WR#_JAS0031 -Walls Regional Hospital Hainey\Data Out\';
%let rptname = 'Walls Regional Hospital Hainey Report';

Instead use

%let rptpath = R:\CDW Reporting\Ad-Hoc\WR#_JAS0031 -Walls Regional Hospital Hainey\Data Out\;
%let rptname = Walls Regional Hospital Hainey Report;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,988

Re: Copy file from one directory to another using X command

Posted in reply to ambadi007

Why not write the file directly to the location you want?

Trusted Advisor
Posts: 1,584

Re: Copy file from one directory to another using X command

Posted in reply to ambadi007

Take out the single quotes:

       %let rptpath   = R:\CDW Reporting\Ad-Hoc\WR#_JAS0031 -Walls Regional Hospital Hainey\Data Out\;

       %let rptname = Walls Regional Hospital Hainey Report;

otherwise they become part of the macro variable.

 

 

Your line: 

      %put &report;

you havn't defined a value to the macro variable &report.

 

The X statement should be enclosed in a data step:

You probably meant to write:

 

%let rptpath = R:\CDW Reporting\Ad-Hoc\WR#_JAS0031 -Walls Regional Hospital Hainey\Data Out\;
%let rptname = Walls Regional Hospital Hainey Report;
%let rptdate = today();
%let rptdatefmt = YYMMDDd10.;


data _NULL_;
cmd = "COPY &rptpath.&name" || put(&rptdate,&rptdatefmt) || ".xlsx C:\Users\UCS1MKP\Desktop\y\&rptname..xlsx";
call system(cmd);
run;

 

Pay attention: at &rptname..xlsx

      I used two dots, the 1st to assign the end of the macro varaible and the 2nd as part of the name.

 

 

Contributor
Posts: 64

Re: Copy file from one directory to another using X command

Hi Shmuel,

 

I used the code but still I am getting the error while copying the error "The system cannnot find the file specified"

%let rptpath = R:\CDW Reporting\Ad-Hoc\WR#_JAS0031 -Walls Regional Hospital Hainey\Data Out\;
%let rptname = Walls Regional Hospital Hainey Report;
%let rptdate = today();
%let rptdatefmt = YYMMDDd10.;


data _NULL_;
 cmd = "COPY &rptpath.&rptname" ||put(&rptdate,&rptdatefmt) || ".xlsx C:\Users\UCS1MKP\Desktop\y\&rptname..xlsx";
 call system(cmd);
run;

Is there any problem with this code I used?

 

Trusted Advisor
Posts: 1,584

Re: Copy file from one directory to another using X command

[ Edited ]
Posted in reply to ambadi007

I suggest you put the command CMD to the log and copy it in a DOS / command window.

Does it work ? Is the path, name, etc - realy combine the full path and report name you want to copy ?

Using X or call system should run same code as if it was run manually.

 

Run:

data _NULL_;
 cmd = "COPY &rptpath.&rptname" ||put(&rptdate,&rptdatefmt) || ".xlsx C:\Users\UCS1MKP\Desktop\y\&rptname..xlsx";
put cmd=;
/* call system(cmd); */ run;

 maybe there are imbeded blanks to remove.

 

Super User
Posts: 11,343

Re: Copy file from one directory to another using X command

Posted in reply to ambadi007

Your command resolves to

COPY R:\CDW Reporting\Ad-Hoc\WR#_JAS0031 -Walls Regional Hospital Hainey\Data Out\Walls Regional Hospital Hainey Report2016-12-05.xlsx C:\Users\UCS1MKP\Desktop\y\Walls Regional Hospital Hainey Report.xlsx

 

The copy command is likely not going to like SPACES inside the file names unless the whole file is enclosed in quotes.

Trusted Advisor
Posts: 1,584

Re: Copy file from one directory to another using X command

I have checked in windows command window, you need doble quotes to enclose path or file name with embeded spaces.

Then change the code into:

data _NULL_;
 cmd = "COPY ""&rptpath.&rptname" ||put(&rptdate,&rptdatefmt) || ".xlsx"" ""C:\Users\UCS1MKP\Desktop\y\&rptname..xlsx"" ";
put cmd=; /* call system(cmd); */
run;
Trusted Advisor
Posts: 1,584

Re: Copy file from one directory to another using X command

If need try run the command in CMD window and
add quoutes until it works, then adapt the SAS code.
Contributor
Posts: 64

Re: Copy file from one directory to another using X command

Hi Shmuel,

 

Iam still stucked in this. Is there any other way to rename the file . Instead to copy to different folder? Just to rename the file

 

Please advice

Trusted Advisor
Posts: 1,584

Re: Copy file from one directory to another using X command

Posted in reply to ambadi007

You probably can force using file name by replacing spaces into underscores

but, can you force the system to do the same upon the path name ?
That is creating a new path without embeded spaces.

 

If you have the syntax to rename it by windows command line then it is possible 

to submit such command by SAS.

 

I am running sas UE (university edition) on linux and cannot test it on windows,

even not use X or call system.

Super User
Posts: 7,854

Re: Copy file from one directory to another using X command

Posted in reply to ambadi007

Bottom line:

 

DO.NOT.USE.BLANKS.IN.FILENAMES.

 

Use underlines instead. Blanks serve no purpose that can't be handled with underlines and only cause additional work and often lots of grief.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎01-03-2017 04:31 AM
Contributor
Posts: 42

Re: Copy file from one directory to another using X command

Posted in reply to KurtBremser

Hi this code will handle files with spaces in name of the file, if you are still looking for a solution

 

X %unquote(%str(%'copy "C:\file with spaces.txt" "C:\file with spaces2.txt" /y%'));

 

Anyway, take the advice given and try to avoid such file names.

Trusted Advisor
Posts: 1,584

Re: Copy file from one directory to another using X command

@chrej5am - I'm using SAS UE on linux, so I cannot test your code (can't use X command),

but I'm qurious to know, would next code work too?

or do I need replace single quotes with double quotes and vice versa:

 

%let file1 = C:\file with spaces.txt;

%let file2 = C:\file with spaces2.txt ;

 

X %unquote(%str(%'copy "&file1" "&file2" /y%'));

 

Thanks ahead

 

Contributor
Posts: 42

Re: Copy file from one directory to another using X command

@Shmuel Yes the code you posted is also valid. No errors given, text file copied.

☑ This topic is solved.

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

Discussion stats
  • 14 replies
  • 2201 views
  • 0 likes
  • 6 in conversation