Help using Base SAS procedures

Copy and Rename file on PCFILES server

Reply
Occasional Contributor
Posts: 8

Copy and Rename file on PCFILES server

I have some SAS code that writes out to an excel workbook located on a windows server. SAS is running on a linux server.

Is it possible to have SAS copy and paste that workbook with a different name on the windows server? Basically the workbook is a template that gets populated and I want to be able to run the code multiple times and end up with multiple populated templates. The way it's structured now after each run I have to copy out the populated template and put another blank one in the location.

Super User
Posts: 19,822

Re: Copy and Rename file on PCFILES server

Posted in reply to JustSomeGuy
Occasional Contributor
Posts: 8

Re: Copy and Rename file on PCFILES server

So I could just set my two filenames using PCFILES and then run this macro? Will there be any issues because these are macro enabled workbooks with macros and lots of formulas, not just simple excel worksheets?

Super Contributor
Posts: 358

Re: Copy and Rename file on PCFILES server

Posted in reply to JustSomeGuy

I think your best bet is to have SAS submit a DOS command to copy / rename the file before you populate data.  I don't think there should be any difficulty accessing the file as long ans the directories are available to the system running the command.

Occasional Contributor
Posts: 8

Re: Copy and Rename file on PCFILES server

The only way I've seen to have SAS submit DOS commands is with x. How do I use x while also telling it to run the command on a different server than where SAS is installed?

Respected Advisor
Posts: 3,799

Re: Copy and Rename file on PCFILES server

Posted in reply to JustSomeGuy

I like unnamed pipe for executing OS commands.  You have good control of the output and you can use FILEVAR= option to execute a number of different commands in the same data step.

Occasional Contributor
Posts: 8

Re: Copy and Rename file on PCFILES server

Posted in reply to data_null__

So you're recommending that I use unnamed pipe to copy and rename the file with windows command line? Are there any limitations to this being that I need to use the pipe on a different server than where SAS is running? I've started googling but had never heard of this before so more detail would be helpful.

Respected Advisor
Posts: 3,799

Re: Copy and Rename file on PCFILES server

Posted in reply to JustSomeGuy

You said you only knew of X command to execute DOS commands.  I was suggesting PIPE because you have greater control over the output from each command this is executed.

http://support.sas.com/documentation/cdl/en/hostwin/63047/HTML/default/viewer.htm#n16puwsro9pakqn1ja...

You don't have to use the FILENAME statement you can also use pipes with the FILEVAR option which allows a single data step to "do it all".

What is the command that you need to execute?

Occasional Contributor
Posts: 8

Re: Copy and Rename file on PCFILES server

Posted in reply to data_null__

The command I want to execute would copy a file (leaving it in the same directory) and then rename it. So with the pipe method I can use a filename statement (which I can define as on a different server, correct?) and have the command in there? Am I understanding this right? Thanks for your help!

I'm really not sure how to go about getting the result I desire. My SAS code already writes out to the workbook correctly, but I want to add something so after it writes out that work book gets copied and has a different name.

Respected Advisor
Posts: 3,799

Re: Copy and Rename file on PCFILES server

Posted in reply to JustSomeGuy

I don't understand your parentheticals.

I don't know if this example will be helpful or not but it is the kind of thing I am talking about.  It is submitted from EG on a unix server.

16         data _null_;
17            length command $128 source target $64;
18            source = '~/shoes.xlsx';
19            target = '~/newshoes.xlsx';
20            command = catx(' ','/usr/bin/cp',source,target);
21            link runit;
22            command = catx(' ','/usr/bin/ls -l', target);
23            link runit;
24            stop;
25          runit:
26            infile dummy pipe filevar=command end=eof;
27            do while(not eof);
28               input;
29               put 'NOTE: ' _infile_;
30               end;
31            return;
32            run;

NOTE:
The infile DUMMY is:
      Pipe command=
"/usr/bin/cp ~/shoes.xlsx ~/newshoes.xlsx"

NOTE: The infile DUMMY is:
      Pipe command=
"/usr/bin/ls -l ~/newshoes.xlsx"

NOTE: -rw-rw-r--  
1 .......  users      19200 Jan 24 04:50 /home/......./newshoes.xlsx
NOTE:
0 records were read from the infile DUMMY.
NOTE:
1 record was read from the infile DUMMY.
      The minimum record length was
81.
      The maximum record length was
81.
NOTE: DATA statement used (Total process time):
      real time          
0.16 seconds
      cpu time           
0.00 seconds
Ask a Question
Discussion stats
  • 9 replies
  • 811 views
  • 0 likes
  • 4 in conversation