oob
oob

Reputation: 1968

How to rename a text file with SAS on Windows XP?

I have a process in SAS that creates a .csv. I have another process in Python that waits until the file exists and then does something with that file. I want to make sure that the Python process doesn't start doing its thing until SAS is done writing the file.

I thought I would just have SAS create the .csv and then rename the file after it was done. Can I do the renaming in SAS? Then the python process could just wait until the renamed file existed.

EDIT: I accidentally posted this question twice and then accidentally deleted both versions. Sorry about that.

Upvotes: 3

Views: 969

Answers (5)

Tom
Tom

Reputation: 51621

How about having the watching program watch for a separate small file instead of the large CSV file? That would be written in one write operation by SAS and reduce the risk of triggering the read before the write is done.

 data _null_;
   file 'bigfile.csv' dsd ;
   set bigdata;
   put (_all_) (+0);
 run;
 data _null_;
   file 'bigfile.txt' ;
   put 'bigfile.csv written';
 run;

Upvotes: 0

RamB
RamB

Reputation: 428

There are more than one way of doing this.

Does the python script monitor for .csv files only? Or it will be triggered when any new file is created in that directory?

  1. If it will trigger only to .csv, then you could simply output to, say, a .txt file, then rename it using the x statement (OS command), or the rename function as @cmjohns suggested.

  2. Another option is, you could output the .csv file to a different directory, then just move it to the directory that python is monitoring. As long as the file is in the same disk, moving the file will be done in an instant.

  3. You could also modify the python script to only look for .csv, then do the option 1.

  4. You could even create a "flag" file for python to check when sas has finished with the .csv.

But if possible, I think I would go with @jgunnink.

Upvotes: 0

jgunnink
jgunnink

Reputation: 171

I think better than renaming would be to write a shell script that executes the SAS program, then only executes the Python script once the SAS program has exited without errors. The syntax for this varies somewhat depending on your OS, but wouldn't be too difficult.

Upvotes: 2

cmjohns
cmjohns

Reputation: 4475

With verion 9.2 and later SAS has a rename function that should work just the way you would like.

Upvotes: 1

itzy
itzy

Reputation: 11765

You could generate your output in a SAS dataset and then write to the .csv file only when you're finished with it. Not sure how you're creating the csv, but I usually just do this:

data _null_;
file './ouptut_file.csv' dlm=',';
set dataset_name;
put var1 var2;
run;

If you do this at the very end of your SAS code, no csv file will be generated until you're finished manipulating the data.

Upvotes: 0

Related Questions