<?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: Renaming external files with SAS variables values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825808#M326168</link>
    <description>The log shows no errors, but when I go to the actual file location the file&lt;BR /&gt;name has not changed and is still the old name.&lt;BR /&gt;</description>
    <pubDate>Thu, 28 Jul 2022 01:05:45 GMT</pubDate>
    <dc:creator>zkhodr</dc:creator>
    <dc:date>2022-07-28T01:05:45Z</dc:date>
    <item>
      <title>Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825790#M326160</link>
      <description>&lt;P&gt;I have a dataset with two variables: one is SSN and one is a unique study ID. I have an external folder of image files for each subject, and each image file is named by SSN. I would like to rename all external files from SSN to studyID. Following is the code that I tried, but I think I am missing a big step somewhere for making the macro part work. Thank you in advance!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%MACRO&lt;/STRONG&gt; rename (ssn, studyid);&lt;/P&gt;&lt;P&gt;DATA _NULL_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET data;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF FILEEXIST("L:\Projects\&amp;amp;ssn..docx") THEN DO;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RC = RENAME&amp;nbsp;("L:\Projects\&amp;amp;ssn..docx", "S:\Projects\&amp;amp;studyid..docx",&amp;nbsp;'file');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%MEND&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;RENAME&lt;/EM&gt;&lt;/STRONG&gt; (ssn, studyid);&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2022 21:43:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825790#M326160</guid>
      <dc:creator>zkhodr</dc:creator>
      <dc:date>2022-07-27T21:43:21Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825792#M326162</link>
      <description>&lt;P&gt;Besides passing actual values for SSN and StudyID, not sure what you mean the code looks correct. &lt;BR /&gt;&lt;BR /&gt;What does the log show?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO rename (ssn, studyid);

DATA _NULL_;

      SET data;

      IF FILEEXIST("L:\Projects\&amp;amp;ssn..docx") THEN DO;

            RC = RENAME ("L:\Projects\&amp;amp;ssn..docx", "S:\Projects\&amp;amp;studyid..docx", 'file');
    put RC=;

      END;

RUN;

%MEND;

 
options mprint symbolgen;
%RENAME (ssn, studyid);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jul 2022 21:50:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825792#M326162</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-27T21:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825796#M326163</link>
      <description>&lt;P&gt;You cannot RENAME a file from one disk to another.&amp;nbsp; You have to first COPY the file.&amp;nbsp; You can then DELETE the original.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you have the SET statement?&amp;nbsp; The code does not use anything from the dataset, just the macro variable values passed via the macro call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that if the dataset DATA has multiple observations then your data _NULL_ step will run iterate once per observation.&amp;nbsp; But the IF condition could only be true on the first iteration if the RENAME() actually worked.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2022 22:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825796#M326163</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-27T22:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825807#M326167</link>
      <description>The set statement sets the sas dataset into the data step, so the values of&lt;BR /&gt;the two variables are known. I am able to rename if I just choose one&lt;BR /&gt;specific old and new name and that does not require a macro, but as soon as&lt;BR /&gt;I try to put it in a macro to do it for multiple files it does not work.&lt;BR /&gt;</description>
      <pubDate>Thu, 28 Jul 2022 01:04:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825807#M326167</guid>
      <dc:creator>zkhodr</dc:creator>
      <dc:date>2022-07-28T01:04:45Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825808#M326168</link>
      <description>The log shows no errors, but when I go to the actual file location the file&lt;BR /&gt;name has not changed and is still the old name.&lt;BR /&gt;</description>
      <pubDate>Thu, 28 Jul 2022 01:05:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825808#M326168</guid>
      <dc:creator>zkhodr</dc:creator>
      <dc:date>2022-07-28T01:05:45Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825840#M326178</link>
      <description>&lt;P&gt;What is the use of dataset data, when you do not use a single variable from it?&lt;/P&gt;
&lt;P&gt;We might be able to make a guess about its purpose if you posted it as usable example data (data step with datalines), so we can see the variables, their attributes and their content.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 04:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825840#M326178</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-07-28T04:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825841#M326179</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/287015"&gt;@zkhodr&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;The set statement sets the sas dataset into the data step, so the values of&lt;BR /&gt;the two variables are known. I am able to rename if I just choose one&lt;BR /&gt;specific old and new name and that does not require a macro, but as soon as&lt;BR /&gt;I try to put it in a macro to do it for multiple files it does not work.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you have the names of the files in a dataset, don't waste any more time on creating a macro, as it won't reduce the code required to rename files.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 05:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825841#M326179</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-07-28T05:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825847#M326184</link>
      <description>&lt;P&gt;If you have the variables ssn and studyid in the dataset, the macro is not necessary at all.&lt;/P&gt;
&lt;P&gt;Since RENAME cannot &lt;EM&gt;move&lt;/EM&gt; a file, you need to use a combination of FCOPY and FDELETE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set data;
length
  fref_in fref_out $8
  fname_in fname_out $200
;
fname_in = cats("L:\Projects\",ssn,".docx");
if fileexist(fname_in)
then do;
  rc = filename(fref_in,fname_in,"","recfm=n");
  fname_out = cats("S:\Projects\",studyid,".docx");
  rc = filename(fref_out,fname_out,"","recfm=n");
  rc = fcopy(fref_in,fref_out);
  if rc = 0 then rc = fdelete(fref_in);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 07:05:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/825847#M326184</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-07-28T07:05:17Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/826953#M326650</link>
      <description>&lt;P&gt;Hi! Thank you for your help. I have a folder on my computer with word documents named 1 - 100. I would like to rename all of them with the matching 'newname' variable in the attached SAS dataset. If I run the following code it will rename it for the first file. How do I modify this code to use the SAS dataset to rename all 100 files. Thank you again!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DATA _NULL_;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;IF FILEEXIST("L:\Projects\1.docx") THEN DO;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;RC = RENAME&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;("L:\Projects\\1.docx",&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;"L:\Projects\a.docx",&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;'file');&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;END;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;RUN;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 17:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/826953#M326650</guid>
      <dc:creator>zkhodr</dc:creator>
      <dc:date>2022-08-03T17:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/826967#M326654</link>
      <description>&lt;P&gt;Your Practice.csv file does NOT have file names in it.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;oldname,newname
1,a
2,b
3,c
4,d
5,e
6,f&lt;/PRE&gt;
&lt;P&gt;But perhaps you could build filenames from the text that is in the file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data results;
  infile 'Practice.csv' dsd truncover firstobs=2 ;
  length oldname newname $200 ;
  input oldname newname;
  oldname=cats('L:\Projects\',oldname,'.docx');
  newname=cats('L:\Projects\',newname,'.docx');
  exists=fileexist(oldname);
  if exists then rc=rename(oldname,newname);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To see if it worked checked the EXISTS and RC variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=results;
  tables exist*rc / list missing;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 18:50:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/826967#M326654</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-03T18:50:06Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/826993#M326663</link>
      <description>&lt;P&gt;Thank you!!! This worked.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 20:06:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/826993#M326663</guid>
      <dc:creator>zkhodr</dc:creator>
      <dc:date>2022-08-03T20:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming external files with SAS variables values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/826994#M326664</link>
      <description>&lt;P&gt;Thank you!! This worked.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 20:08:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-external-files-with-SAS-variables-values/m-p/826994#M326664</guid>
      <dc:creator>zkhodr</dc:creator>
      <dc:date>2022-08-03T20:08:48Z</dc:date>
    </item>
  </channel>
</rss>

