<?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: Automate file names in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792392#M81492</link>
    <description>&lt;P&gt;At the beginning of your process, do you start with an empty directory (and an empty main.txt), or will there already be files in it?&lt;/P&gt;
&lt;P&gt;What data is written to the new files?&lt;/P&gt;</description>
    <pubDate>Wed, 26 Jan 2022 09:07:58 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-01-26T09:07:58Z</dc:date>
    <item>
      <title>Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792376#M81486</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;I am struggling to automate file names for new text files i am creating. i would like to name the files as follows:&lt;/P&gt;&lt;P&gt;Program1_260122.txt&lt;/P&gt;&lt;P&gt;I have my todays date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/*Get todays date*/
data _null_;
cdate=today();
cdate = today();
call symputx('cdate', put(cdate, ddmmyyn8.));
run;
%put &amp;amp;cdate.;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I am stuck where it must create the text files with the name 'Program1' in front of the date. I tried up to here:&lt;/P&gt;&lt;PRE&gt;data tsave;
  do i = 1 to 5;
	   y = "Program" + i;
       output;
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 08:05:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792376#M81486</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-26T08:05:26Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792377#M81487</link>
      <description>&lt;P&gt;If you want to create a file name in the do loop of the data step, you can write the following&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tsave;
  do i = 1 to 5;
	   y = cats("Program",i,"_&amp;amp;cdate..txt");
       output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 08:10:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792377#M81487</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-01-26T08:10:20Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792379#M81488</link>
      <description>&lt;P&gt;I &lt;U&gt;&lt;STRONG&gt;STRONGLY&lt;/STRONG&gt;&lt;/U&gt; recommend to use a YMD order for dates in filenames, as they will then sort correctly in time order, and to &lt;U&gt;&lt;STRONG&gt;ALWAYS&lt;/STRONG&gt;&lt;/U&gt; use 4-digit years.&lt;/P&gt;
&lt;P&gt;So you should get your date in a single %LET with this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let cdate = %sysfunc(today(),yymmddn8.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I also recommend to use a format for your running count that accommodates the maximum number and adds leading zeroes, so program 11 won't come before program 2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tsave;
do i = 1 to 5;
  y = cats("Program",put(i,z2.),"_&amp;amp;cdate..txt");
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 08:16:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792379#M81488</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T08:16:47Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792384#M81489</link>
      <description>&lt;P&gt;hi&lt;/P&gt;&lt;P&gt;thank you&lt;/P&gt;&lt;P&gt;Variable passes are not working when I pass it into my filename:&lt;/P&gt;&lt;PRE&gt;/*Get todays date*/
data _null_;
cdate=datetime();
call symputx('cdate', put(cdate, datetime20.));
run;
%put &amp;amp;cdate.;
data test;

%let i = 1 ;
%let filename = cats("&amp;amp;i","_&amp;amp;cdate.");
	   proc export 
		data=files
	    outfile="/C:/'&amp;amp;filename'.txt"
	    dbms=tab;
		run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 08:35:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792384#M81489</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-26T08:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792387#M81490</link>
      <description>&lt;P&gt;You cannot run PROCEDURE steps inside a DATA step. The PROC keyword constitutes a step boundary and immediately ends the DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that you want to automatically export a series of datasets to a series of text files? If yes, please supply examples for the dataset names and text file names. We need to see the whole picture to give you proper help.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 08:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792387#M81490</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T08:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792390#M81491</link>
      <description>the only reason I added the dataset was because I received an error saying:Syntax error, expecting one of the following: ;, DATA, DBLABEL, DBMS, DEBUG, FILE, LABEL, OUTFILE, OUTTABLE, REPLACE,&lt;BR /&gt;TABLE, _DEBUG_.&lt;BR /&gt;&lt;BR /&gt;What I am trying to achieve is the following, it is a bot complex:&lt;BR /&gt;Have a main text file which contains a list of all 5 text files in a directory, lets call it main.txt.&lt;BR /&gt;Thereafter text files need to be created using SAS with naming convention Program1_todaysdatetime.I am allowed to create up to 5 of these files thus up to Program5_todaysdatetime and everytime this file gets created, it needs to be recorded in main.txt.&lt;BR /&gt;&lt;BR /&gt;What needs to happen is I need to count number of records in main.txt. If this count is less than 5 then we must create a new text file and populate the name of this new text file into the main text file. This loop needs to continue.&lt;BR /&gt;&lt;BR /&gt;So far I got to the point where is counts the number of records in main.txt and now I am trying to automate the rest.&lt;BR /&gt;&lt;BR /&gt;/*Count records*/&lt;BR /&gt;data files;&lt;BR /&gt;length fname $256 lines 8 cmd $300;&lt;BR /&gt;infile "ls &amp;amp;path.&amp;amp;pattern" pipe truncover ;&lt;BR /&gt;input fname $100.;&lt;BR /&gt;cmd = catx(' ','wc -l',fname);&lt;BR /&gt;infile cmd pipe filevar=cmd truncover;&lt;BR /&gt;input lines @;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;select lines into :linecount from files;&lt;BR /&gt;quit;</description>
      <pubDate>Wed, 26 Jan 2022 08:55:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792390#M81491</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-26T08:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792392#M81492</link>
      <description>&lt;P&gt;At the beginning of your process, do you start with an empty directory (and an empty main.txt), or will there already be files in it?&lt;/P&gt;
&lt;P&gt;What data is written to the new files?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 09:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792392#M81492</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T09:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792394#M81493</link>
      <description>Hi Kurt, empty dir and empty main.txt.&lt;BR /&gt;The new files dont need to have data written in them</description>
      <pubDate>Wed, 26 Jan 2022 09:14:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792394#M81493</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-26T09:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792400#M81494</link>
      <description>&lt;P&gt;This code (tested on my SAS On Demand account) creates 5 files and a main.txt containing the filenames:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let cdate = %sysfunc(today(),yymmddn8.);

data tsave;
length y fname $200;
do i = 1 to 5;
  y = cats("~/Program",put(i,1.),"_&amp;amp;cdate..txt");
  fname = y;
  output;
  file dummy filevar=y;
end;
run;

data _null_;
file "~/main.txt";
set tsave;
put fname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 09:33:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792400#M81494</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T09:33:50Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792460#M81495</link>
      <description>thank you Kurt. I have used the code and it works well. Now I want to create the 5 files with there respective names. however the files are not creating properly and it seems its because of the way I have placed my &amp;amp;fname variable in the path&lt;BR /&gt;&lt;BR /&gt;%let cdate = %sysfunc(datetime(), datetime20.);&lt;BR /&gt;&lt;BR /&gt;data t;&lt;BR /&gt;length y fname $300;&lt;BR /&gt;do i = 1 to 5;&lt;BR /&gt;y = cats("Program",put(i,1.),"_&amp;amp;cdate..txt");&lt;BR /&gt;fname = y;&lt;BR /&gt;file "/C:/&amp;amp;fname.txt";&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;</description>
      <pubDate>Wed, 26 Jan 2022 13:31:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792460#M81495</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-26T13:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792469#M81496</link>
      <description>&lt;P&gt;Please study my code with care, especially the use of the FILEVAR= option to dynamically create files.&lt;/P&gt;
&lt;P&gt;My code creates 5 files and the master file, you just need to change the path in the CATS function so it works on Windows.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 13:55:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792469#M81496</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T13:55:25Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792471#M81497</link>
      <description>&lt;P&gt;Windows version of my code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let cdate = %sysfunc(today(),yymmddn8.);

data tsave;
length y fname $200;
do i = 1 to 5;
  y = cats("C:\Program",put(i,1.),"_&amp;amp;cdate..txt");
  fname = y;
  output;
  file dummy filevar=y;
end;
run;

data _null_;
file "C:\main.txt";
set tsave;
put fname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Add additional path elements as needed. If this does not work as expected, please post the &lt;U&gt;&lt;EM&gt;complete&lt;/EM&gt;&lt;/U&gt; log into a window opened with this button:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54552i914D97BE1B0F21E5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 14:00:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/792471#M81497</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T14:00:14Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/793085#M81502</link>
      <description>&lt;P&gt;Thank you. This works well. I'm now trying to add the name of the file in a macro variable so that the file can then be created with this name via a macro. Not sure how to do that&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jan 2022 10:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/793085#M81502</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-28T10:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: Automate file names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/793171#M81508</link>
      <description>&lt;P&gt;How do you create a single file, and which parts of that code need to be made dynamic? Please post the whole code.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jan 2022 17:53:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Automate-file-names/m-p/793171#M81508</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-28T17:53:12Z</dc:date>
    </item>
  </channel>
</rss>

