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

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"';

1 ACCEPTED SOLUTION

Accepted Solutions
chrej5am
Quartz | Level 8

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

14 REPLIES 14
Kurt_Bremser
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Shmuel
Garnet | Level 18

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.

 

 

ambadi007
Quartz | Level 8

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?

 

Shmuel
Garnet | Level 18

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.

 

ballardw
Super User

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.

Shmuel
Garnet | Level 18

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;
Shmuel
Garnet | Level 18
If need try run the command in CMD window and
add quoutes until it works, then adapt the SAS code.
ambadi007
Quartz | Level 8

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

Shmuel
Garnet | Level 18

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.

Kurt_Bremser
Super User

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.

chrej5am
Quartz | Level 8

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.

Shmuel
Garnet | Level 18

@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

 

chrej5am
Quartz | Level 8

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

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!

How to Concatenate Values

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.

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
  • 14 replies
  • 28440 views
  • 0 likes
  • 6 in conversation