BookmarkSubscribeRSS Feed
Emma2021
Quartz | Level 8
I have 2 csv files in c:\ drive.
How can I change file names by swapping each one without uploading/importing into sas?
I have: test.csv and test1.csv

I want to change test.csv to test1.csv
And test1.csv to test.csv

Thank you.
13 REPLIES 13
japelin
Rhodochrosite | Level 12

For example, you could try using a different file name once.

 

options noxwait xsync;
x 'rename C:\Temp\test.csv text0.csv';
x 'rename C:\Temp\test1.csv text.csv';
x 'rename C:\Temp\test0.csv text1.csv';
ChrisNZ
Tourmaline | Level 20
Emma2021
Quartz | Level 8
Thank you, but it does not do anything. Should I add a line of code perhaps “run;”?
Reeza
Super User
I would not recommend X command statements - they will work on your computer but not necessarily any system.
Using the RENAME function will work in any OS making your code more portable. Note that if you're swapping names, you'll need a temporary name for one of them to avoid conflicts.
The usage is the same as with FCOPY/FINFO from your previous question.
Kurt_Bremser
Super User

The problem with the X statement is that it does not really tell you what happened. Use PIPE to retrieve the responses of the command:

data commands;
input command $80.; /* safe because DATALINES are always padded to at least 80 characters */
datalines;
rename C:\Temp\test.csv text0.csv
rename C:\Temp\test1.csv text.csv
rename C:\Temp\test0.csv text1.csv
;

data _null_;
set commands;
command = catx(" ",command,'2>&1'); /* reroutes stderr to stdout, so everything is caught */
infile dummy pipe filevar=command eof=done;
start:
  input;
  put _infile_;
  go to start;
done:
run;

Untested, as On Demand runs with NOXCMD.

Emma2021
Quartz | Level 8
Thanks, but it does not do anything either.
andreas_lds
Jade | Level 19

If this has to be done in a sas session, using the rename function in a data step seems to be the best choice.

 

 

Kurt_Bremser
Super User

If your SAS runs on a remote server, you (most probably) cannot do this from SAS code. You would need to share your C: drive to the network and mount it on the SAS server.

Emma2021
Quartz | Level 8
My sas is on my local not server based
Tom
Super User Tom
Super User

Try using the RENAME() function.

Example:

* Create two files ;
data _null_;
  file 'c:\downloads\file1.csv' ;
  put 'file1';
  file 'c:\downloads\file2.csv' ;
  put 'file2';
run;

* Use the RENAME() function to "swap" them ;
data _null_;
  if fileexist('c:\downloads\temp') then put 'Will not work because temp file already exists';
  else do;
    rc1=rename('c:\downloads\file1.csv','c:\downloads\temp','file');
    rc2=rename('c:\downloads\file2.csv','c:\downloads\file1.csv','file');
    rc3=rename('c:\downloads\temp','c:\downloads\file2.csv','file');
    put (rc1-rc3)(=);
  end;
run;

* check that it worked ;
data _null_;
  infile 'c:\downloads\file1.csv' ;
  input;
  put _infile_;
  infile 'c:\downloads\file2.csv' ;
  input;
  put _infile_;
run;

 

starrtiktokk
Calcite | Level 5
I would not recommend X command statements - they will work on your computer but not necessarily any system.
Kurt_Bremser
Super User

Maxim 14 says "Use the Right Tool".

When you can do a complex action with a one line system command instead of 100 lines of SAS code (and much quicker at that!), there is no question which is the better option.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 13 replies
  • 1092 views
  • 2 likes
  • 8 in conversation