<?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 with if condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/591095#M169273</link>
    <description>Yes! Thank you so much for your help, I will also check your notes about the procedure to move multiple files by using data null in SAS, it is an important topic related to this operation.</description>
    <pubDate>Tue, 24 Sep 2019 07:39:24 GMT</pubDate>
    <dc:creator>George_SAS</dc:creator>
    <dc:date>2019-09-24T07:39:24Z</dc:date>
    <item>
      <title>Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590859#M169167</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have performed a process in SAS DI 9.4, which&amp;nbsp;consists of a load process of data&amp;nbsp;from an external file to a database. The load process works fine, but I need to add an intermediate step to move this external file from a directory1 to a directory2, in the case that the mentioned process fails&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thereby, I want to move this external file if the process fails (for example, if data in the external file&amp;nbsp;is endowed&amp;nbsp;with a wrong format), but I do not want to move this external file if the process works&amp;nbsp;fine. Thus,&amp;nbsp;I am trying the following sentences in SAS to address this task with an if condition:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data _null_;
	if (&amp;amp;trans_rc ne 0) then do;
		infile '/directory1/externalfile.txt';
   	        file '/directory2/externalfile.txt';
   	        input;
		put _infile_;
	end;
run;
%macro check(file);
%if (&amp;amp;trans_rc ne 0) %then %do;
	%if %sysfunc(fileexist(&amp;amp;file)) ge 1 %then %do;
   	       %let rc=%sysfunc(filename(temp,&amp;amp;file));
   	       %let rc=%sysfunc(fdelete(&amp;amp;temp));
	%end; 
	%else %put The file &amp;amp;file does not exist;
%end;
%else %put Hello World;
%mend check;

%check(/directory1/externalfile.txt)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In&amp;nbsp;these sentences, the variable trans_rc describes the status of the job, so that its value is 0 if the job works fine and the process should not move the external file in such a case. However, by using these sentences, the job always moves the external file from the directory1 to the directory2, even though the&amp;nbsp;process works fine (i.e. even though the value of trans_rc is 0).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea?&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 10:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590859#M169167</guid>
      <dc:creator>George_SAS</dc:creator>
      <dc:date>2019-09-23T10:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590864#M169171</link>
      <description>&lt;P&gt;How do you set trans_rc?&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 10:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590864#M169171</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-23T10:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590865#M169172</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282097"&gt;@George_SAS&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how about:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro skip(trans_rc);&lt;BR /&gt;%put **&amp;amp;=trans_rc.**;
%if (&amp;amp;trans_rc. ne 0) %then %do;
  data _null_;
    infile '/directory1/externalfile.txt';
    file '/directory2/externalfile.txt';
    input;
    put _infile_;
  run;
%end;
%mend skip;
%skip(&amp;amp;trans_rc.)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 10:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590865#M169172</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-09-23T10:35:07Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590871#M169175</link>
      <description>I think that SAS automatically sets the variable trans_rc to 0 when the job starts, so a job without errors preserves this value for trans_rc, but perhaps this condition is not applying appropiately within my step and the problem could be related to this issue...</description>
      <pubDate>Mon, 23 Sep 2019 11:14:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590871#M169175</guid>
      <dc:creator>George_SAS</dc:creator>
      <dc:date>2019-09-23T11:14:18Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590877#M169178</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;, I have applied these sentences and they work completely fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please, can you roughly explain them to understand how&amp;nbsp;they work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In addition, how can I use them to move a variable set of external files, instead of a unique external file? I am thinking to use a "externalfile*" string in the "infile" line, but I do not know how implementing this possibility in the "file" line. For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro skip(trans_rc);%put **&amp;amp;=trans_rc.**;
%if (&amp;amp;trans_rc. ne 0) %then %do;
  data _null_;
    infile '/directory1/externalfile*';
    file '/directory2/externalfile*';
    input;
    put _infile_;
  run;
%end;
%mend skip;
%skip(&amp;amp;trans_rc.)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would these sentences work to move a set externalfile_1, …, externalfile_n of files to a directory2?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 11:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590877#M169178</guid>
      <dc:creator>George_SAS</dc:creator>
      <dc:date>2019-09-23T11:34:18Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590903#M169193</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282097"&gt;@George_SAS&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When you execute the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename f TEMP;
%let trans_rc=1;

data _null_;
	if (&amp;amp;trans_rc. ne 0) then do;
		file f;
    put "test";
	end;
run;


filename f TEMP;
%let trans_rc=0;

data _null_;
	if (&amp;amp;trans_rc. ne 0) then do;
		file f;
    put "test";
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the log prints out:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;1
2    filename f TEMP;
3    %let trans_rc=1;
4
5    data _null_;
6      if (&amp;amp;trans_rc. ne 0) then do;
7        file f;
8        put "test";
9      end;
10   run;

NOTE: The file F is:
      Filename=C:\SAS_Temporary_Files\_TD1308_H48LBFI12PS0510_\#LN00049,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=23Sep2019:15:05:41,
      Create Time=23Sep2019:15:05:41

NOTE: 1 record was written to the file F.
      The minimum record length was 4.
      The maximum record length was 4.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds


11
12
13   filename f TEMP;
14   %let trans_rc=0;
15
16   data _null_;
17     if (&amp;amp;trans_rc. ne 0) then do;
18       file f;
19       put "test";
20     end;
21   run;

NOTE: The file F is:
      Filename=C:\SAS_Temporary_Files\_TD1308_H48LBFI12PS0510_\#LN00050,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=23Sep2019:15:05:41,
      Create Time=23Sep2019:15:05:41

NOTE: 0 records were written to the file F.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.03 seconds&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The first datastep creates file with 1 record (i.e. non empty file), the second one creates file with _zero_ records&amp;nbsp; (i.e. empty). And Since you've been testing for file existence... So whenever there is an occurrence of the FILE statement inside the datastep code the file is created. My version is simply suppressing execution of whole datastep if&amp;nbsp;trans_rc is 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As for the second part. The asterisk won't resolve the issue. I would rather consider something like FCOPY() function combined with FILEMAME() function and DOPEN(), DCLOSE(), DNUM(), and&amp;nbsp;&lt;SPAN&gt;DREAD() to traverse the directory and copy files. Check out the doc for more details and examples:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n0hpa8p9kacbran1ndqiw3krwohq.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n0hpa8p9kacbran1ndqiw3krwohq.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;All the best&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Bart&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 13:14:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590903#M169193</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-09-23T13:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590923#M169199</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282097"&gt;@George_SAS&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that was me using DIS then I'd be go for the status handling tab in the transformation or if the transformation doesn't have such a tab then use the Return Code Check transformation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here a dummy example how this could be set-up using the Return Code Check transformation.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32675i0B09DC897E64E348/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically: In case of an error condition you call a macro stored in a folder which is part of the SAS Autocall Facility which moves the file (passed in as parameter).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As for the code moving the file:&lt;/P&gt;
&lt;P&gt;I wouldn't use a data _null_step as this doesn't move the file but writes a new one. I'd be using an OS command - i.e. &lt;EM&gt;mv&lt;/EM&gt; in a Unix/Linux environment.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 13:55:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590923#M169199</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-09-23T13:55:03Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590926#M169202</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt; for your help. As mentioned above, I have verified that your first code copies the files to the directory2 but I have just seen that it does not delete the file. In this sense, I am trying the following sentences to delete them, because I need to move the file (i.e. not just a copy, but a direct movement where the directory1 has to be empty after the copy), but it does not actually delete the external file:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dele(trans_rc);%put **&amp;amp;=trans_rc.**;
%if (&amp;amp;trans_rc. eq 0) %then %do;
    data _null_;
	fname='externalfile.txt';
        rc=filename(fname,'directory1/externalfile.txt');
        if rc = 0 and fexist(fname) then
      	   rc=fdelete(fname);
        rc=filename(fname);
    run;
%end;
%mend dele;&lt;BR /&gt;%dele(&amp;amp;trans_rc.)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any idea?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for the help.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 14:08:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590926#M169202</guid>
      <dc:creator>George_SAS</dc:creator>
      <dc:date>2019-09-23T14:08:41Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590928#M169203</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you mean something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro skip(trans_rc
in,
out,
);

%put **&amp;amp;=trans_rc.**;
%if (&amp;amp;trans_rc. ne 0) %then %do;
  data _null_;
    rc = filename("_IN_", &amp;amp;in.);
    if rc = 0 and fexist("_IN_") then
      do;
        rc = filename('_OUT_',&amp;amp;out.);
        rc = fcopy("_IN_", '_OUT_');      
        rc = fdelete("_IN_"); rc = filename('_OUT_');
      end;
  rc = filename("_IN_");  run;
%end;
%mend skip;&lt;BR /&gt;
%skip(&amp;amp;trans_rc.
,'/directory1/externalfile1'
,'/directory2/externalfile1'
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 14:22:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590928#M169203</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-09-23T14:22:56Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590934#M169208</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282097"&gt;@George_SAS&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even though you've already accepted a different solution I'm of the strong opinion that the "correct" DIS way of doing things is to use status handling.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 14:26:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/590934#M169208</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-09-23T14:26:20Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/591095#M169273</link>
      <description>Yes! Thank you so much for your help, I will also check your notes about the procedure to move multiple files by using data null in SAS, it is an important topic related to this operation.</description>
      <pubDate>Tue, 24 Sep 2019 07:39:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/591095#M169273</guid>
      <dc:creator>George_SAS</dc:creator>
      <dc:date>2019-09-24T07:39:24Z</dc:date>
    </item>
    <item>
      <title>Re: Move external files with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/591097#M169275</link>
      <description>&lt;P&gt;Thanks for your note too. I tried that procedure a few days ago but I got an error of permissions in SAS, so that I had to apply the alternative data null step in order to accomplish this task.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2019 07:45:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-external-files-with-if-condition/m-p/591097#M169275</guid>
      <dc:creator>George_SAS</dc:creator>
      <dc:date>2019-09-24T07:45:22Z</dc:date>
    </item>
  </channel>
</rss>

