BookmarkSubscribeRSS Feed
vicky07
Quartz | Level 8

Hello,

I am using SAS EG and I'm trying to use X command to create a folder and move files using mv command. The create directory stop is working fine however, the file move step is not working.  I cant figure out what is wrong with my syntax.

I am not getting any error and the files are not moving. Any help is appreciated.

 

Below is my code:

 

%let curr = %str(/Client/Market Ops/%sysfunc(today(),yymmddp9.)/);

%macro a1;

%let tm= %sysfunc(time(),time8.0) ;
%let archive = archive_%scan(&tm.,3,:);

 

/* create directory */

x "mkdir ""/&curr/&archive.""";

 

/* move files from current directory to archive*/
x "mv ""&curr/*.*"" ""&curr/&archive.""";
%mend;
%a1;

2 REPLIES 2
Tom
Super User Tom
Super User

How can you generated a date string with punctuation that is only 9 characters long?  You should need 10,  4 for the year, 2 for the month, 2 for the day and 2 for punctuation.

%let curr = /Client/Market Ops/%sysfunc(today(),yymmddp10.)/ ;

You have extra slashes in there. The %LET is generating a string with a slash at the beginning and at the end into the macro variable &CURR.  But then later when you use the value you add an extra one in front of it and after it. 

x "mkdir ""&curr.&archive."" ";
x "mv ""&curr.*.*"" ""&curr.&archive."" ";

 

Two additional points.

1) I find it is usually easier to let the QUOTE() function figure out when you need to double embedded quotes.

x %sysfunc(quote(mkdir "&curr.&archive."));

2) I find using a PIPE to run commands works much better than just the X (or SYSTEM) command.  That way you can actually capture any messages the operating system is sending you.

data _null_;
  infile %sysfunc(quote(mv "&curr.*.*" "&curr.&archive.")) pipe ;
  input;
  put _infile_;
run;

 

Kurt_Bremser
Super User

The string inside the quotes is expanded by the UNIX shell, creating a single object (because of the quotes) which does not exist.

You need to move every file in a single mv.

Let this be an object lesson why you must not have blanks in filenames, period.

Blanks are the natural separator of items on the commandline and must therefore not be used in object names, or they cause problems like this.

See Maxim 44.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 626 views
  • 0 likes
  • 3 in conversation