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"';
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.
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;
Why not write the file directly to the location you want?
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.
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?
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.
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.
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;
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
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.
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.
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.
@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
@Shmuel Yes the code you posted is also valid. No errors given, text file copied.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.