<?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 Conditionally rename the file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712096#M219455</link>
    <description>&lt;P&gt;I want to scan for files in one shared folder and if file name contains 5601, 6010 or 6020 then I want to rename the file name by adding a string 'copied' before .CSV (e.g.&amp;nbsp;IFR_RIS_1_ACC_5601_1_20200118_copied.csv ). Just don't do anything if filename don't have 5601, 6010 or 6020&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_5601_1_20200118.csv&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jan 2021 12:32:14 GMT</pubDate>
    <dc:creator>David_Billa</dc:creator>
    <dc:date>2021-01-18T12:32:14Z</dc:date>
    <item>
      <title>Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712096#M219455</link>
      <description>&lt;P&gt;I want to scan for files in one shared folder and if file name contains 5601, 6010 or 6020 then I want to rename the file name by adding a string 'copied' before .CSV (e.g.&amp;nbsp;IFR_RIS_1_ACC_5601_1_20200118_copied.csv ). Just don't do anything if filename don't have 5601, 6010 or 6020&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_5601_1_20200118.csv&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 12:32:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712096#M219455</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-18T12:32:14Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712097#M219456</link>
      <description>&lt;P&gt;Do you have the code that scans the files in the shared folder in place?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 12:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712097#M219456</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-18T12:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712101#M219458</link>
      <description>Yes&lt;BR /&gt;</description>
      <pubDate>Mon, 18 Jan 2021 12:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712101#M219458</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-18T12:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712139#M219480</link>
      <description>&lt;P&gt;Please show the code for scanning, which is necessary for us to help you do the file renames. And (just in case the code doesn't make it evident) what is the operating system that is being used to store the files?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 15:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712139#M219480</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-18T15:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712143#M219483</link>
      <description>We are  using windows to keep the files. I can't use OS commands though.&lt;BR /&gt;Have to tackle with SAS functions/statements.&lt;BR /&gt;&lt;BR /&gt;As SAS server is down now, I'm unable to show you the code. However you can&lt;BR /&gt;point me to any examples which is similar to my post.&lt;BR /&gt;</description>
      <pubDate>Mon, 18 Jan 2021 16:03:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712143#M219483</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-18T16:03:13Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712287#M219582</link>
      <description>&lt;P&gt;By&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;I can't use OS commands though.&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I presume you mean you can't use the X "command" statement in SAS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro t2(folder=);
  data _null_;
    rc=filename("mydir","&amp;amp;folder");
    did=dopen("mydir");
    mem_count=dnum(did);

    length fname new_name $64;

    do i=1 to mem_count;
      fname=dread(did,i); 
      copy='no ';
      new_name=' ';
      if countw(fname,'.')&amp;gt;1 and upcase(scan(fname,-1,'.'))='CSV' then do;
       if find(fname,'5601') then copy='yes'; else 
       if find(fname,'6010') then copy='yes'; else 
       if find(fname,'6020') then copy='yes';
       if copy='yes' then do;
         new_name=tranwrd(fname,".csv","_opened.csv");
         rename_rc=rename (cats("&amp;amp;path,/",fname),cats("&amp;amp;path,/",new_name),"FILE");
       end;
      end;
    end;
    rc_close=dclose(did);
  run;
%mend;
options mprint;
%t2(folder=c:\temp);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2021 01:49:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712287#M219582</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-19T01:49:40Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712289#M219584</link>
      <description>Thanks. Hope it works.&lt;BR /&gt;&lt;BR /&gt;Now there is slight change in the requirement. If the file name has&lt;BR /&gt;'_copied' then I want to move the files to the target folder. How can we&lt;BR /&gt;handle this in your program?&lt;BR /&gt;</description>
      <pubDate>Tue, 19 Jan 2021 01:59:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712289#M219584</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-19T01:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712291#M219586</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; &amp;nbsp;I want to rename the file name by adding a string 'copied'&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;You can add something like this to the data step that processes the file names:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if index(FILENAME,'5601')
  | index(FILENAME,'5610')
  | index(FILENAME,'5620')
  then RC=rename(FILENAME, scan(FILENAME,1,'.') || '_copied.csv', 'file' );
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 02:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712291#M219586</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-01-19T02:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712292#M219587</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;&amp;nbsp;I want to move the files to the target folder&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;There is no SAS function to move files under Windows. You need to use fcopy() then fdelete().&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 02:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712292#M219587</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-01-19T02:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712293#M219588</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt; if index(FILENAME,'5601')
  | index(FILENAME,'5610')
  | index(FILENAME,'5620')
  then RC=rename(FILENAME, scan(FILENAME,1,'.') || '_copied.csv', 'file' );
&lt;/LI-CODE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This can be shortened to&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if prxmatch('/5601|5610|5620/',FILENAME) then RC=rename(FILENAME, scan(FILENAME,1,'.') || '_copied.csv', 'file' );
&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;</description>
      <pubDate>Tue, 19 Jan 2021 02:12:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712293#M219588</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-01-19T02:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712295#M219590</link>
      <description>Thanks. I got it. But one more question.&lt;BR /&gt;&lt;BR /&gt;If the file name has 'copied' after moving that file to the target folder I&lt;BR /&gt;want to rename the filename by removing the string '_copied' from the file&lt;BR /&gt;name. How to tackle this with substr and rename function?&lt;BR /&gt;</description>
      <pubDate>Tue, 19 Jan 2021 02:16:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712295#M219590</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-19T02:16:13Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally rename the file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712301#M219594</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks. I got it. But one more question.&lt;BR /&gt;&lt;BR /&gt;If the file name has 'copied' after moving that file to the target folder I&lt;BR /&gt;want to rename the filename by removing the string '_copied' from the file&lt;BR /&gt;name. How to tackle this with substr and rename function?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your welcome, but I have one more comment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I write code like this to help understanding.&amp;nbsp; I try to stop when I am merely being asked to do someone else's job.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code I provided shows the principle of how to do the new task.&amp;nbsp; Use it and learn.&amp;nbsp; It can be like swimming the Hellespont - difficult but worth it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 03:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-rename-the-file/m-p/712301#M219594</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-19T03:47:01Z</dc:date>
    </item>
  </channel>
</rss>

