<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Move external files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488529#M127342</link>
    <description>&lt;PRE&gt;data _null_;
  set files_to_move;
  call execute(cats('filename tmp pipe ',"'",'copy ",filepath,"/",filename,'" "',target_path,'"; data log;  infile tmp; put _input_; run;')); 
run;&lt;/PRE&gt;
&lt;P&gt;So this creates for each row in input dataset, a filename pipe of the copy command, then this is read in as infile and put out to log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I really would however advise against using SAS - which is for analysing data - to do operating system operations.&amp;nbsp; If you need to move files its only a matter of cntrl+a and then move them across in Windows explorer.&amp;nbsp; What benefit is there in programming this?&lt;/P&gt;</description>
    <pubDate>Tue, 21 Aug 2018 13:04:37 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-08-21T13:04:37Z</dc:date>
    <item>
      <title>Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488526#M127341</link>
      <description>&lt;P&gt;Hi experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to move external files programmatically from one folder to another and I want to capture the OS log messages in the SAS log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know how this works for a single file using code as below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename moveit pipe 'mv -f /sourcedir/myfile.txt /targetdir';
data _null_;
  infile moveit;
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I have is a SAS dataset from a directory listing with a list of files to be moved. Something like below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data files_to_move;
  file_name='something.txt          ';
  file_path='/sourcedir';
  output;
  file_name='something_else.txt';
  file_path='/sourcedir';
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm having a "brain freeze"&amp;nbsp;and just can't figure out how to&amp;nbsp;move these files with a single data step while also capturing the log messages. I feel I better ask you guys for help before I start with some macro code generating separate filenames/data steps per file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I'm after is something like below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let target_path=/targetdir;
data _null_;
  set files_to_move;
  /*  ????? move files */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I believe to remember that I've seen code from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; doing what I want but I just can't find it. Help - please!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Patrick&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 12:59:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488526#M127341</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-08-21T12:59:15Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488529#M127342</link>
      <description>&lt;PRE&gt;data _null_;
  set files_to_move;
  call execute(cats('filename tmp pipe ',"'",'copy ",filepath,"/",filename,'" "',target_path,'"; data log;  infile tmp; put _input_; run;')); 
run;&lt;/PRE&gt;
&lt;P&gt;So this creates for each row in input dataset, a filename pipe of the copy command, then this is read in as infile and put out to log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I really would however advise against using SAS - which is for analysing data - to do operating system operations.&amp;nbsp; If you need to move files its only a matter of cntrl+a and then move them across in Windows explorer.&amp;nbsp; What benefit is there in programming this?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 13:04:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488529#M127342</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-21T13:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488531#M127344</link>
      <description>&lt;P&gt;Make use of the fact that mv allows to move multiple files into one target:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data files_to_move;
  file_name='something.txt          ';
  file_path='/sourcedir';
  output;
  file_name='something_else.txt';
  file_path='/sourcedir';
  output;
run;

proc sort data=files_to_move;
by source_dir;
run;

data _null_;
set files_to_move;
by sourcedir;
length filenames $4096;
retain filenames;
if first.sourcedir
then filenames = '';
filenames = catx(' ',filenames,file_name);
if last.sourcedir
then do;
  call execute("filename oscmd pipe 'mv " !! trim(filenames) !! " " !! "&amp;amp;target_path. 2&amp;gt;&amp;amp;1';");
  call execute("data _null_; infile oscmd; input; put _infile_; run;");
end;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Aug 2018 13:08:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488531#M127344</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-21T13:08:12Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488533#M127346</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"If you need to move files its only a matter of cntrl+a and then move them across in Windows explorer"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;This will be part of an ETL process run in batch on a RHEL server where I don't know the file names in advance.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you for the proposed solution but that's not really what I'm after as it still executes a separate data step per file.&amp;nbsp;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;What I'm after is a data _null_ step for all the files at once - but allowing me to write the log messages to the SAS log (the put _infile_; bit in the code sample I've posted).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 13:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488533#M127346</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-08-21T13:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488534#M127347</link>
      <description>&lt;P&gt;Use PIPE engine with FILEVAR option on an INFILE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let target_path=/targetdir;
data _null_;
  set files_to_move;
  length cmd $300 ;
  cmd=catx(' ','mv -f',catx('/',file_path,file_name),"&amp;amp;target_path");
  infile mv pipe filevar=cmd ;
  input ;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can examine the input buffer for error messages if you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 13:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488534#M127347</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-21T13:10:58Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488552#M127357</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Thanks! And the code you've posted is certainly heading into the right direction.&lt;/P&gt;
&lt;P&gt;For some reason I can't explain the code is only moving one file per run.&lt;/P&gt;
&lt;P&gt;Below is real code which I've run in my environment - with two files in sourcedir (file1.txt and file2.txt). For some reason I don't understand the first code execution only moved file1.txt, the 2nd then also moved file2.txt.&lt;/P&gt;
&lt;P&gt;I've also change the input statement to input&amp;nbsp;@@; but this didn't change a thing.&lt;/P&gt;
&lt;P&gt;Any ideas?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let target_path=/tmp/targetdir;
data dirlist;
  path_name='/tmp/sourcedir';
  file_name='file1.txt';
  output;
  file_name='file2.txt';
  output;
run;

data test;
  set dirlist;
  length cmd $1000 ;
  cmd=catx(' ','mv -f',catx('/',path_name,file_name),"&amp;amp;target_path");
  infile mv pipe filevar=cmd ;
  input ;
  put _infile_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And here the log from the first run which moved&amp;nbsp;&lt;SPAN&gt;file1.txt&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;36         data test;
37           set dirlist;
38           length cmd $1000 ;
39           cmd=catx(' ','mv -f',catx('/',path_name,file_name),"&amp;amp;target_path");
40           infile mv pipe filevar=cmd ;
41           input ;
42           put _infile_;
43         run;

NOTE: The infile MV is:
      Pipe command="mv -f /tmp/sourcedir/file1.txt /tmp/targetdir"

NOTE: 0 records were read from the infile MV.
NOTE: There were 1 observations read from the data set WORK.DIRLIST.
NOTE: The data set WORK.TEST has 0 observations and 2 variables.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 13:45:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488552#M127357</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-08-21T13:45:00Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488555#M127358</link>
      <description>&lt;P&gt;Possible the INPUT is reading past the end of file.&lt;/P&gt;
&lt;P&gt;One trick I use sometimes is to use the END= option. This should also let you capture multi-line messages.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;infile mv pipe filevar=cmd end=eof;
while (not eof);
  input ;
  put _infile_;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Aug 2018 13:44:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488555#M127358</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-21T13:44:14Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488558#M127360</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;That's done the trick. Thanks tons for your help!&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 13:53:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488558#M127360</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-08-21T13:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488587#M127372</link>
      <description>&lt;P&gt;You can make a SHELL file ,and execute it once for all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make a shell file which contain&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;mv -f /sourcedir/A.txt /targetdir
mv -f /sourcedir/B.txt /targetdir
mv -f /sourcedir/C.txt /targetdir
mv -f /sourcedir/D.txt /targetdir&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then execute this shell file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token statement"&gt;filename&lt;/SPAN&gt; moveit pipe &lt;SPAN class="token string"&gt;'./home/xxxx.sh'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; _null_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;infile&lt;/SPAN&gt; moveit&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt; _infile_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 14:33:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488587#M127372</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-08-21T14:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488593#M127376</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The solution Tom posted is what I need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The real use case is:&lt;/P&gt;
&lt;P&gt;I'm getting files delivered from an upstream process (dumped into a folder) and I'm also getting metadata for these files inserted into an Oracle table. This process is on-going 24/7.&lt;/P&gt;
&lt;P&gt;I need to create a directory listing of the files, inner join it with the metadata and then only process the files which exist both in metadata and on the file system.&lt;/P&gt;
&lt;P&gt;My process will execute in mini-batches and I can't be sure that metadata and files in the directory are always in sync (that's why I need the inner join). If I find information in both places then I can process the files (move them to a process folder for the next step).&amp;nbsp;And I want some info in the SAS log if/how moving the files worked.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 14:41:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files/m-p/488593#M127376</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-08-21T14:41:40Z</dc:date>
    </item>
  </channel>
</rss>

