SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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