<?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: Copy or Move the file based on file name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712068#M219445</link>
    <description>Check function FCOPY()  FDELETE() .</description>
    <pubDate>Mon, 18 Jan 2021 11:13:03 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-01-18T11:13:03Z</dc:date>
    <item>
      <title>Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712064#M219442</link>
      <description>&lt;P&gt;Everyday client will be placing files ('n' number of files) in one of the shared folder where SAS can access. Now I want to copy the files to '/var/nonlife'&amp;nbsp; if the file name contains 5601, 6010 or 6020 and rename the file name by adding a string 'copied' before .CSV (e.g.&amp;nbsp;IFR_RIS_1_ACC_5601_1_20200118_copied.csv ) If the file name contains 0169, 1070 or 0417 it have to be moved to '/var/nonlife' otherwise it has to move to '/var/life'. If the file name has 'copied' then do not copy or move the file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;File name looks like,&lt;/P&gt;
&lt;P&gt;IFR_RIS_1_ACC_1070_1_20200118.csv&lt;/P&gt;
&lt;P&gt;IFR_RIS_1_ACC_5601_1_20200118.csv&lt;/P&gt;
&lt;P&gt;IFR_RIS_1_ACC_1889_1_20200118.csv&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appreciate if someone of you help me understand to copy or move the file conditionally based on the file name.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 11:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712064#M219442</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-18T11:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712065#M219443</link>
      <description>&lt;P&gt;You don't want to do anything with the data in the CAV files, you just want to move them, correct?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 10:58:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712065#M219443</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-18T10:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712066#M219444</link>
      <description>&lt;P&gt;Yes, I don't want to do anything with data in CSV files. I just want to copy/ move the file depends on the file name.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 11:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712066#M219444</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-18T11:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712068#M219445</link>
      <description>Check function FCOPY()  FDELETE() .</description>
      <pubDate>Mon, 18 Jan 2021 11:13:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712068#M219445</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-01-18T11:13:03Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712075#M219449</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;How to move files as fcopy is only for copy? Per initial post, I've to copy or move based on file names.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 11:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712075#M219449</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-18T11:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712080#M219451</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;try the approach given in the blog post&amp;nbsp;&lt;A href="https://blogs.sas.com/content/sasdummy/2011/06/17/how-to-use-sas-data-step-to-copy-a-file-from-anywhere/" target="_self"&gt;How to use SAS DATA step to copy a file from anywhere&lt;/A&gt;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 11:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712080#M219451</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-18T11:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712088#M219453</link>
      <description>If you could use OS command ,that would be a easy task , just invoke OS command like MV CP .....&lt;BR /&gt;If not , after FCOPY() these files and using FDELETE() to delete the original files .&lt;BR /&gt;Write some data step code to pick up the filename you need to copy or move ,that is easy I think .</description>
      <pubDate>Mon, 18 Jan 2021 12:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712088#M219453</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-01-18T12:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712165#M219494</link>
      <description>I was not allowed to use OS command to copy/move the files. I have to use&lt;BR /&gt;SAS functions or statements only.&lt;BR /&gt;</description>
      <pubDate>Mon, 18 Jan 2021 17:11:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712165#M219494</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-18T17:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712175#M219500</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;i'm not sure how we can re-name the file automatically. But below code will help you to move and copy the files to new folder based on their names.you have to update the folder in below code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;FILENAME PATH=LOCATION OF ORIGINAL FILE FOLDER ;
data have;
 rc=filename("fileref","&amp;amp;PATH");
  my_dir=dopen("fileref");
  if my_dir &amp;gt; 0 then do;
   f_num=dnum(my_dir);
    do i=1 to f_num;
	  filename_=dread(my_dir,i);
     output;
	end;
  end;
  rc=dclose(my_dir);
run;

data w_1;
set have;
Fother_other=scan(scan(filename_,1,'.'),-3,'_');
o_path="ORIGINAL LOCATION";
m_path="MOVE FOLDER LOCATION";
C_path="COPY FOLDER LOCATION";
o_path1=cats(o_path,'\',filename_);
m_path1=cats(m_path,'\',filename_);
C_path1=cats(C_path,'\',filename_);
run;

proc sql noprint;
select  count(*)  into :Cc  from w_1 where Fother_other in ('5601','6010','6020');
select o_path1 into :CO_file1 - :CO_file%LEFT(%TRIM(&amp;amp;Cc)) from w_1 where Fother_other in ('5601','6010','6020');
select C_path1 into :CC_file1 - :CC_file%LEFT(%TRIM(&amp;amp;Cc)) from w_1 where Fother_other in ('5601','6010','6020');

select  count(*)  into :MC  from w_1 where Fother_other in ('0169','1070','0417');
select o_path1 into :MO_file1 - :MO_file%LEFT(%TRIM(&amp;amp;MC)) from w_1 where Fother_other in ('0169','1070','0417');
select m_path1 into :MM_file1 - :MM_file%LEFT(%TRIM(&amp;amp;MC)) from w_1 where Fother_other in ('0169','1070','0417');
quit;


%macro move;
%do i=1 %to &amp;amp;MC;
X "MOVE &amp;amp;&amp;amp;MO_file&amp;amp;I &amp;amp;&amp;amp;MM_file&amp;amp;I";
%END;
%do i=1 %to &amp;amp;CC;
X "COPY &amp;amp;&amp;amp;CO_file&amp;amp;I &amp;amp;&amp;amp;CC_file&amp;amp;I";
%END;
%MEND;

%MOVE;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Jan 2021 18:24:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712175#M219500</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2021-01-18T18:24:26Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712178#M219502</link>
      <description>&lt;P&gt;If you cannot use OS commands then you will need to use SAS functions to both get the list of files and move the files.&lt;/P&gt;
&lt;P&gt;There is no MOVE function, just FCOPY() and FDELETE(), so you will want to first use FCOPY() and if it works then use FDELETE() to remove the original file to simulate a MOVE operation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To determine the target directory you can use the INDEXW() or FINDW() function to test if any of the strings exists between underscore delimiters in the filename.&lt;/P&gt;
&lt;P&gt;To change the filename you might want to remove the extension and the append the new suffix and then the re-append the extension.&lt;/P&gt;
&lt;P&gt;Let's start by getting the list of filenames to copy into a dataset.&amp;nbsp; That way we can look at the dataset to make sure the logic is working.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let inpath=/var/source/ ;
%let path1=/var/life/;
%let path2=/var/nonlife/;

filename source "&amp;amp;inpath";

data files ;
  length id 8 msg filename source target $256 ;
  did=dopen(source);
  if did&amp;lt;=0 then do;
      msg=sysmsg(); 
      put msg; 
      stop;
  end;
  do id=1 to dnum(did);
    filename=dread(did,id);
    if scan(lowcase(filename),-1,'.')='csv' and not index(lowcase(filename),'_copied.csv ') then do;
      source="&amp;amp;inpath" || filename;
      if indexw(filename,'5601','_') or indexw(filename,'6010','_') or indexw(filename,'6020','_') then do;
        target="&amp;amp;path1"||substr(filename,1,length(filename)-4)||'_copied.csv';
      end;
      else if indexw(filename,'0169','_') or indexw(filename,'1070','_') or indexw(filename,'0417','_') then do;
        target="&amp;amp;path2"||filename ;
      end;
      else target="&amp;amp;path1"||filename;
    end;
    output;
  end;
  did=dclose(did);
  drop did msg;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have a list of SOURCE and TARGET filenames you can use that to do the "move".&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data files_moved;
  set files ;
  length rc1-rc4 8 msg $256 ;
  rc1=filename('from',source);
  rc2=filename('to',target);
  rc3=fcopy('from','to');
  if rc3 then do;
    msg=sysmsg(); 
    put 'ERROR: Unable to copy. ' source= target= rc3= msg=;
  end;
  else do;
    rc4=fdelete('from');
  end;
  output;
  rc1=filename('from');
  rc2=filename('to');
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Jan 2021 18:27:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712178#M219502</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-18T18:27:16Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712330#M219610</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;I've got the error after running your program. Are we missing something?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ERROR: No logical assign for filename&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2021 07:05:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712330#M219610</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-19T07:05:55Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712351#M219618</link>
      <description>&lt;P&gt;As always, please post the&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;complete&lt;/EM&gt;&lt;/STRONG&gt; log and not just the ERROR message. The message needs to be seen in its context.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 09:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712351#M219618</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-19T09:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712358#M219622</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp; Here you go.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;26         %let inpath=/var/sasdata/GTM;
27         %let path1=/var/sasdata/LH;
28         %let path2=/var/sasdata/NL_GTM;
29         
30         filename source "&amp;amp;inpath";
SYMBOLGEN:  Macro variable INPATH resolves to /var/sasdata/GTM
31         
32         data files ;
33           length id 8 msg filename source target $256 ;
34           did=dopen(source);
35           if did&amp;lt;=0 then do;
36               msg=sysmsg();
37               put msg;
38               stop;
39           end;
40           do id=1 to dnum(did);
41             filename=dread(did,id);
42             if scan(lowcase(filename),-1,'.')='csv' and not index(lowcase(filename),'_copied.csv ') then do;
43               source="&amp;amp;inpath" || filename;
SYMBOLGEN:  Macro variable INPATH resolves to /var/sasdata/GTM
44               if indexw(filename,'5601','_') or indexw(filename,'6010','_') or indexw(filename,'6020','_') then do;
45                 target="&amp;amp;path1"||substr(filename,1,length(filename)-4)||'_copied.csv';
SYMBOLGEN:  Macro variable PATH1 resolves to /var/sasdata/LH
46               end;
47               else if indexw(filename,'0169','_') or indexw(filename,'1070','_') or indexw(filename,'0417','_') then do;
48                 target="&amp;amp;path2"||filename ;
SYMBOLGEN:  Macro variable PATH2 resolves to /var/sasdata/NL_GTM
49               end;
50               else target="&amp;amp;path1"||filename;
SYMBOLGEN:  Macro variable PATH1 resolves to /var/sasdata/LH
51             end;
52             output;
53           end;
54           did=dclose(did);
55           drop did msg;
56         run;

ERROR: No logical assign for filename .&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2021 09:18:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712358#M219622</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-19T09:18:04Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712363#M219624</link>
      <description>&lt;P&gt;Ah, I see. This statement needs quotes:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  did=dopen("source");
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2021 09:27:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712363#M219624</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-19T09:27:46Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712370#M219627</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;Your fix has worked, thank you.I do have one more question. I tried to add two more 'else if' condition in the program and also added one macro variable but it's not working. Objective is to move the file&amp;nbsp; to the destination folder based on the value of macro variable and if file name has string '_copied'. After copying it&amp;nbsp;I've to remove the string '_copied' in the target folder. I yet to add this logic as I couldn't find any so far.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Source folder is always same for any files. Macro variable and else if which I added in the &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;program are,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let function=NL; 
 /*added*/
      else if scan(lowcase(filename),-1,'.')='csv' and  index(lowcase(filename),'_copied.csv ') and "&amp;amp;function" EQ 'NL' then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      target="&amp;amp;path2"||"/"||filename;
      end;
      /*added*/
      else if scan(lowcase(filename),-1,'.')='csv' and  index(lowcase(filename),'_copied.csv ') and "&amp;amp;function" EQ 'LH' then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      target="&amp;amp;path1"||"/"||filename;
      end;&lt;/PRE&gt;
&lt;P&gt;Error message:&lt;/P&gt;
&lt;PRE&gt;26         %let function=NL;
27 
28         %let inpath=/var/sasdata/GTM;
29         %let path1=/var/sasdata/LH;
30         %let path2=/var/sasdata/NL_GTM;
31         
32         filename source "&amp;amp;inpath";
33         
34         data files ;
35           length id 8 msg filename source target $256 ;
36           did=dopen("source");
37           if did&amp;lt;=0 then do;
38               msg=sysmsg();
39               put msg;
40               stop;
41           end;
42           do id=1 to dnum(did);
43             filename=dread(did,id);
44             if scan(lowcase(filename),-1,'.')='csv' and not index(lowcase(filename),'_copied.csv ') then do;
45               source="&amp;amp;inpath" ||"/"|| filename;
46               if indexw(filename,'5601','_') or indexw(filename,'6010','_') or indexw(filename,'6020','_') then do;
47                 target="&amp;amp;path1"||substr(filename,1,length(filename)-4)||'_copied.csv';
48               end;
49               else if indexw(filename,'0169','_') or indexw(filename,'1070','_') or indexw(filename,'0417','_') then do;
50                 target="&amp;amp;path2"||"/"||filename;
51               end;
52               /*added*/
53               else if scan(lowcase(filename),-1,'.')='csv' and  index(lowcase(filename),'_copied.csv ') and "&amp;amp;function" EQ 'NL'
53       ! then do;
54               source="&amp;amp;inpath" ||"/"|| filename;
55               target="&amp;amp;path2"||"/"||filename;
56               end;
2                                                         
57               /*added*/
58               else if scan(lowcase(filename),-1,'.')='csv' and  index(lowcase(filename),'_copied.csv ') and "&amp;amp;function" EQ 'LH'
58       ! then do;
59               source="&amp;amp;inpath" ||"/"|| filename;
60               target="&amp;amp;path1"||"/"||filename;
61               end;
62               else target="&amp;amp;path1"||"/"||filename;
63             end;
64             output;
65           end;
66           did=dclose(did);
67           drop did msg;
68         run;

NOTE: The data set WORK.FILES has 1 observations and 4 variables.
NOTE: Compressing data set WORK.FILES increased size by 100.00 percent. 
      Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
      

69         
70         data files_moved;
71           set files ;
72           length rc1-rc4 8 msg $256 ;
73           rc1=filename('from',source);
74           rc2=filename('to',target);
75           rc3=fcopy('from','to');
76           if rc3 then do;
77             msg=sysmsg();
78             put 'ERROR: Unable to copy. ' source= target= rc3= msg=;
79           end;
80           else do;
81             rc4=fdelete('from');
82           end;
83           output;
84           rc1=filename('from');
85           rc2=filename('to');
86         run;

ERROR: Unable to copy. SOURCE=  TARGET=  RC3=20004 MSG=ERROR: No logical assign for filename FROM.
NOTE: There were 1 observations read from the data set WORK.FILES.
NOTE: The data set WORK.FILES_MOVED has 1 observations and 9 variables.
NOTE: Compressing data set WORK.FILES_MOVED increased size by 100.00 percent. 
      Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
&lt;/PRE&gt;
&lt;P&gt;Any leads to help resolve this error and the logic to remove the string '_copied' from the filename in the target folder?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 10:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712370#M219627</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2021-01-19T10:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712380#M219630</link>
      <description>&lt;P&gt;Run this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename source "&amp;amp;inpath";

data files ;
  length msg filename $256 ;
  did=dopen("source");
  if did&amp;lt;=0 then do;
      msg=sysmsg();
      put msg;
      stop;
  end;
  do id=1 to dnum(did);
    filename=dread(did,id);
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and look at the contents of dataset FILES. You will find that the value is not caught by any of your conditions, so SOURCE and TARGET stay empty.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 11:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712380#M219630</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-19T11:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712387#M219635</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;- I've corrected the condition in 'else if' statements but still source and target value is empty in 'files' dataset. Any idea why still it's empty as the filename and macro variable value is statisfying the below else if?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;File Name&amp;nbsp;is IFR_ITM_TRN_2_6010_1_20201009T075212_copied.csv&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro variable value which I passed in the program is,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; function=NL;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/*added*/
      else if  index(lowcase(filename),'_copied.csv ') and "&amp;amp;function" EQ 'NL' then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      target="&amp;amp;path2"||"/"||filename;
      end;
      /*added*/
      else if  index(lowcase(filename),'_copied.csv ') and "&amp;amp;function" EQ 'LH' then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      target="&amp;amp;path1"||"/"||filename;
      end;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2021 12:06:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712387#M219635</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-19T12:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712394#M219636</link>
      <description>&lt;P&gt;If your filename is actually that, source and target CANNOT be empty:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let function=NL;

%let inpath=/var/sasdata/GTM;
%let path1=/var/sasdata/LH;
%let path2=/var/sasdata/NL_GTM;

data test;
filename = "IFR_ITM_TRN_2_6010_1_20201009T075212_copied.csv";
    if scan(lowcase(filename),-1,'.')='csv' and not index(lowcase(filename),'_copied.csv ') then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      if indexw(filename,'5601','_') or indexw(filename,'6010','_') or indexw(filename,'6020','_') then do;
        target="&amp;amp;path1"||substr(filename,1,length(filename)-4)||'_copied.csv';
      end;
      else if indexw(filename,'0169','_') or indexw(filename,'1070','_') or indexw(filename,'0417','_') then do;
        target="&amp;amp;path2"||"/"||filename;
      end;
      /*added*/
      else if scan(lowcase(filename),-1,'.')='csv' and  index(lowcase(filename),'_copied.csv ') and "&amp;amp;function" EQ 'NL'
then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      target="&amp;amp;path2"||"/"||filename;
      end;
                                               
      /*added*/
      else if scan(lowcase(filename),-1,'.')='csv' and  index(lowcase(filename),'_copied.csv ') and "&amp;amp;function" EQ 'LH'
then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      target="&amp;amp;path1"||"/"||filename;
      end;
      else target="&amp;amp;path1"||"/"||filename;
    end;
    output;
put source= target=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log excerpt:&lt;/P&gt;
&lt;PRE&gt; 105        put source= target=;
 106        run;
 
 source=/var/sasdata/GTM/IFR_ITM_TRN_2_6010_1_20201009T075212_copied.csv
 target=/var/sasdata/LHIFR_ITM_TRN_2_6010_1_20201009T075212_copied_copied.csv
&lt;/PRE&gt;
&lt;P&gt;Run the data step I gave you earlier. Then take the dataset, and apply your conditions. If that does not do it, create your dataset as a data step with datalines (you can use the macro from my footnotes for that), and rerun the code. If that still does result in empty variables, post both steps here.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 12:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712394#M219636</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-19T12:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712407#M219639</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;What will be best idea to&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;remove the string '_copied' from the filename in the target folder?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 14:28:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712407#M219639</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-19T14:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: Copy or Move the file based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712412#M219640</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;Still SOURCE and TARGET value is empty even after simplying the last 'else if'. My code is,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let function=NL;   

%let inpath=/var/sasdata/GTM;
%let path1=/var/sasdata/LH;
%let path2=/var/sasdata/NL_GTM;

filename source "&amp;amp;inpath";

data files ;
  length msg filename $256 ;
  did=dopen("source");
  if did&amp;lt;=0 then do;
      msg=sysmsg();
      put msg;
      stop;
  end;
  do id=1 to dnum(did);
    filename=dread(did,id);
    output;
  end;
  did=dclose(did);
run;

data files_;
  set files;
    if scan(lowcase(filename),-1,'.')='csv' and not index(lowcase(filename),'_copied.csv ') then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      if indexw(filename,'5601','_') or indexw(filename,'6010','_') or indexw(filename,'6020','_') then do;
        target="&amp;amp;path1"||substr(filename,1,length(filename)-4)||'_copied.csv';
      end;
      else if indexw(filename,'0169','_') or indexw(filename,'1070','_') or indexw(filename,'0417','_') then do;
        target="&amp;amp;path2"||"/"||filename;
      end;
      /*added*/
      else if  index(lowcase(filename),'_copied.csv ') ne 0 then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      target="&amp;amp;path2"||"/"||filename;
      end;

      else target="&amp;amp;path1"||"/"||filename;
    end;
    output;
    put source= target=;
run;
&lt;/PRE&gt;
&lt;P&gt;Last else if should have executed for my file as it has the string '_copied'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;26         %let function=NL;
27         
28         %let inpath=/var/sasdata/GTM;
29         %let path1=/var/sasdata/LH;
30         %let path2=/var/sasdata/NL_GTM;
31         
32         filename source "&amp;amp;inpath";
33         
34         
35         
36         
37         data files ;
38           length msg filename $256 ;
39           did=dopen("source");
40           if did&amp;lt;=0 then do;
41               msg=sysmsg();
42               put msg;
43               stop;
44           end;
45           do id=1 to dnum(did);
46             filename=dread(did,id);
47             output;
48           end;
49           did=dclose(did);
50         run;

NOTE: The data set WORK.FILES has 1 observations and 4 variables.
51         
52         data files_;
53           set files;
54             if scan(lowcase(filename),-1,'.')='csv' and not index(lowcase(filename),'_copied.csv ') then do;
55               source="&amp;amp;inpath" ||"/"|| filename;
56               if indexw(filename,'5601','_') or indexw(filename,'6010','_') or indexw(filename,'6020','_') then do;
57                 target="&amp;amp;path1"||substr(filename,1,length(filename)-4)||'_copied.csv';
58               end;
59               else if indexw(filename,'0169','_') or indexw(filename,'1070','_') or indexw(filename,'0417','_') then do;
60                 target="&amp;amp;path2"||"/"||filename;
61               end;
62               /*added*/
63               else if  index(lowcase(filename),'_copied.csv ') ne 0 then do;
64               source="&amp;amp;inpath" ||"/"|| filename;
65               target="&amp;amp;path2"||"/"||filename;
66               end;
67               /*added*/
68         /*      else if  index(lowcase(filename),'_copied.csv ')=0  then do;*/
69         /*      source="&amp;amp;inpath" ||"/"|| filename;*/
70         /*      target="&amp;amp;path1"||"/"||filename;*/
71         /*      end;*/
72               else target="&amp;amp;path1"||"/"||filename;
73             end;
74             output;
75             put source= target=;
76         /*  end;*/
77         /*  did=dclose(did);*/
78         /*  drop did msg;*/
79         run;

SOURCE=  TARGET= 
NOTE: There were 1 observations read from the data set WORK.FILES.
NOTE: The data set WORK.FILES_ has 1 observations and 6 variables.
&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2021 14:42:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-or-Move-the-file-based-on-file-name/m-p/712412#M219640</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-19T14:42:08Z</dc:date>
    </item>
  </channel>
</rss>

