<?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: Simplified creation of files with dynamic filenames. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Simplified-creation-of-files-with-dynamic-filenames/m-p/21545#M4575</link>
    <description>You need to look at the FILE statement and using the FILEVAR= keyword to dynamically change the target/output file within a DATA step.  Check the SAS DOC and also the SAS forum archives for prior posts on FILEVAR.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
    <pubDate>Thu, 13 May 2010 19:05:51 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2010-05-13T19:05:51Z</dc:date>
    <item>
      <title>Simplified creation of files with dynamic filenames.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Simplified-creation-of-files-with-dynamic-filenames/m-p/21544#M4574</link>
      <description>Hey folks,&lt;BR /&gt;
    I have a simplified version of my program to create dynamically named files:&lt;BR /&gt;
&lt;BR /&gt;
data _NULL_;&lt;BR /&gt;
  length fName $10;&lt;BR /&gt;
  do i = 1 to 3;&lt;BR /&gt;
    fName = strip("test") || strip(i) || strip(".txt");&lt;BR /&gt;
    putlog "FNAME = " fName;&lt;BR /&gt;
    &lt;BR /&gt;
    if (i &amp;gt; 1) then do;&lt;BR /&gt;
      fid = filename('out',"");&lt;BR /&gt;
    end;&lt;BR /&gt;
    fid = filename('out', strip(fName));&lt;BR /&gt;
&lt;BR /&gt;
    file out lrecl=20 notitle noprint;&lt;BR /&gt;
    put "Testing";&lt;BR /&gt;
    put i;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
It results in the following output:&lt;BR /&gt;
&lt;BR /&gt;
1    data _NULL_;&lt;BR /&gt;
2      length fName $10;&lt;BR /&gt;
3      do i = 1 to 3;&lt;BR /&gt;
4        fName = strip("test") || strip(i) || strip(".txt");&lt;BR /&gt;
5        putlog "FNAME = " fName;&lt;BR /&gt;
6&lt;BR /&gt;
7        if (i &amp;gt; 1) then do;&lt;BR /&gt;
8          fid = filename('out',"");&lt;BR /&gt;
9        end;&lt;BR /&gt;
10       fid = filename('out', strip(fName));&lt;BR /&gt;
11&lt;BR /&gt;
12       file out lrecl=20 notitle noprint;&lt;BR /&gt;
13       put "Testing";&lt;BR /&gt;
14       put i;&lt;BR /&gt;
15     end;&lt;BR /&gt;
16   run;&lt;BR /&gt;
&lt;BR /&gt;
NOTE: Numeric values have been converted to character&lt;BR /&gt;
      values at the places given by: (Line):(Column).&lt;BR /&gt;
      4:36&lt;BR /&gt;
NOTE: The file OUT is:&lt;BR /&gt;
      Filename=\\Dgrfs-0\pgms\tsb\upid\upid7_html\out.dat,&lt;BR /&gt;
      RECFM=V,LRECL=20,File Size (bytes)=0,&lt;BR /&gt;
      Last Modified=13May2010:12:27:56,&lt;BR /&gt;
      Create Time=13May2010:11:14:25&lt;BR /&gt;
&lt;BR /&gt;
FNAME = test1.txt&lt;BR /&gt;
FNAME = test2.txt&lt;BR /&gt;
FNAME = test3.txt&lt;BR /&gt;
NOTE: 6 records were written to the file OUT.&lt;BR /&gt;
      The minimum record length was 1.&lt;BR /&gt;
      The maximum record length was 7.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.03 seconds&lt;BR /&gt;
      cpu time            0.01 seconds&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
The ideal result is that out would equal (at each iteration) test1.txt, test2.txt, and test3.txt, each new file containing "Testing" and their iteration number.&lt;BR /&gt;
&lt;BR /&gt;
What I get instead is out.dat holding:&lt;BR /&gt;
Testing&lt;BR /&gt;
1&lt;BR /&gt;
Testing&lt;BR /&gt;
2&lt;BR /&gt;
Testing&lt;BR /&gt;
3&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Am I using FILE or FILENAME improperly?  Is there a way to create multiple dynamically named files in the data step?  (due to the method of naming for the end program's files, they must be created in a data step, and not a macro).&lt;BR /&gt;
&lt;BR /&gt;
Thanks much!&lt;BR /&gt;
   -Jim</description>
      <pubDate>Thu, 13 May 2010 18:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Simplified-creation-of-files-with-dynamic-filenames/m-p/21544#M4574</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-05-13T18:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: Simplified creation of files with dynamic filenames.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Simplified-creation-of-files-with-dynamic-filenames/m-p/21545#M4575</link>
      <description>You need to look at the FILE statement and using the FILEVAR= keyword to dynamically change the target/output file within a DATA step.  Check the SAS DOC and also the SAS forum archives for prior posts on FILEVAR.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 13 May 2010 19:05:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Simplified-creation-of-files-with-dynamic-filenames/m-p/21545#M4575</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-05-13T19:05:51Z</dc:date>
    </item>
    <item>
      <title>Re: Simplified creation of files with dynamic filenames.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Simplified-creation-of-files-with-dynamic-filenames/m-p/21546#M4576</link>
      <description>Thank you very much!  That worked excellently.  It was exactly what I needed &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;BR /&gt;
&lt;BR /&gt;
-Jim</description>
      <pubDate>Thu, 13 May 2010 19:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Simplified-creation-of-files-with-dynamic-filenames/m-p/21546#M4576</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-05-13T19:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: Simplified creation of files with dynamic filenames.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Simplified-creation-of-files-with-dynamic-filenames/m-p/21547#M4577</link>
      <description>Hi:&lt;BR /&gt;
  Using the FILEVAR option of the FILE statement would make your life MUCH easier. This example is a slightly different example, but shows creating 3 files named:&lt;BR /&gt;
[pre]&lt;BR /&gt;
c:\temp\wombat1.txt&lt;BR /&gt;
c:\temp\wombat2.txt&lt;BR /&gt;
c:\temp\wombat3.txt&lt;BR /&gt;
[/pre]&lt;BR /&gt;
              &lt;BR /&gt;
and THEN, in a second program, adds some information to the files created in the first program. The key is the use of the FILEVAR option of the FILE statement.&lt;BR /&gt;
Documentation:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000171874.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000171874.htm&lt;/A&gt;&lt;BR /&gt;
  &lt;BR /&gt;
Specifically, my example is a modification of the documentation example #5, entitled, "Dynamically Changing the Current Output File".&lt;BR /&gt;
      &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
** First method create the files new each time;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
  length namevar $200;&lt;BR /&gt;
  do i = 1 to 3;&lt;BR /&gt;
     Namevar = cats('c:\temp\','wombat',put(i,1.0),'.txt');&lt;BR /&gt;
     file out filevar=namevar;&lt;BR /&gt;
     put "Twas brillig and the slithy toves";&lt;BR /&gt;
     put i;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
           &lt;BR /&gt;
** Now Add to the file created above with the MOD option;&lt;BR /&gt;
** Note that the NAMEVAR needs to be the same in both programs;&lt;BR /&gt;
** for this to work;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
  length namevar $200;&lt;BR /&gt;
  do i = 1 to 3;&lt;BR /&gt;
     Namevar = cats('c:\temp\','wombat',put(i,1.0),'.txt');&lt;BR /&gt;
     file out filevar=namevar mod;&lt;BR /&gt;
     put "Did gyre and gimble in the wabe.";&lt;BR /&gt;
     put i;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
  &lt;BR /&gt;
[/pre]</description>
      <pubDate>Thu, 13 May 2010 19:22:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Simplified-creation-of-files-with-dynamic-filenames/m-p/21547#M4577</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-05-13T19:22:21Z</dc:date>
    </item>
  </channel>
</rss>

