<?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: Adding a variable based on the filename and exporting the file as its original type in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532634#M145962</link>
    <description>&lt;P&gt;Yeah, PROC IMPORT definitely has the possibility of not reading in the data correctly and then consequently writing it out incorrectly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;I understand your code, I wasn't sure the OP would, so s/he shouldn't run it :).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rationale for this type of 'rule' is that if you don't understand it enough you cannot make changes or understand what's happening when something goes wrong. Edge cases can also slip through if you're not checking your cases correctly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your solution is the one I would recommend as being the most correct. However, the macro approach will work as well.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Understood. And actually the write out of _infile_ is partially to avoid any of the issues that arise when "best" formats may get applied from proc import and such things as account numbers.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 04 Feb 2019 17:07:28 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-02-04T17:07:28Z</dc:date>
    <item>
      <title>Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532157#M145787</link>
      <description>&lt;DIV class="lia-message-body"&gt;&lt;DIV class="lia-message-body-content"&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I would like to post my question again, because the last time I did not get a satisfactry answer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to add variable YEAR from 1992 to 2017 based on the filename for each file available in comma delimited .txt type.&amp;nbsp; The file names are&amp;nbsp; ID1992, ID1993, ..., ID2017,and export it back as its original file, that is .txt.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Each file has 137 variables and more than 50,000 rows of data - character and numeric. I don't want to interfere within the files, but just want to add another variable YEAR for each row of each file.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate your time and help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 01 Feb 2019 18:56:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532157#M145787</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-02-01T18:56:11Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532178#M145797</link>
      <description>Show the code that you can use to successfully read a single file. Then that code can be modified to accomplish this. After reviewing your previous question, I believe this would be enough information to solve your problem.</description>
      <pubDate>Fri, 01 Feb 2019 19:54:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532178#M145797</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-01T19:54:08Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532195#M145803</link>
      <description>&lt;P&gt;I strongly suggest backing up you text files before you start such things. Any error on your part could destroy your original file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will you need to insert a column header?&lt;/P&gt;
&lt;PRE&gt;data _null_;
   infile "&amp;lt;path&amp;gt;\id1922.txt" lrecl=10000;/* needs to be at least as long as longest input line*/
   file "&amp;lt;path&amp;gt;\newid1922.txt";
   input;
   if _n_=1 then outline= catx(',',_infile_,'Year');
   else outline=catx(',',_infile_,'1992');
   put outline;
run;&lt;/PRE&gt;
&lt;P&gt;Replace path with your location. If you write to a different folder then you could use the same file name.&lt;/P&gt;
&lt;P&gt;This provides a header row. If you don't need one remove the IF - else only have the last outline.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is sufficient then steps can be made to automate this.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 20:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532195#M145803</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-02-01T20:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532201#M145807</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;thank you!&lt;/P&gt;&lt;P&gt;Here is my code that is doing the job one by one. I want this code to be automated to all at once.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile='E:\Idaho\ID2017.txt'
dbms=dlm out=work.test;
delimiter =",";
getnames=yes;
guessingrows=100000;
run;
data Work.ID2017ay;
set test;
Inspection_year=2017;
run;
proc export data=Work.ID2017ay replace
outfile = 'E:\Ad_Year\ID2017ay'
dbms=dlm;
delimiter=',';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Feb 2019 21:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532201#M145807</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-02-01T21:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532206#M145811</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253384"&gt;@mmhxc5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;thank you!&lt;/P&gt;
&lt;P&gt;Here is my code that is doing the job one by one. I want this code to be automated to all at once.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Did you try my example?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 21:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532206#M145811</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-02-01T21:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532214#M145818</link>
      <description>&lt;P&gt;If you wanted to automate that, you could turn it into a macro and then call it for each year.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a tutorial for that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; solution will work as well, but I'm not a huge fan of running code I don't understand.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 21:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532214#M145818</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-01T21:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532224#M145823</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro runner(yy=);
	proc import datafile="E:\Idaho\ID&amp;amp;yy..txt"
		dbms=dlm out=work.test;
		delimiter =",";
		getnames=yes;
		guessingrows=100000;
	run;
	data Work.ID&amp;amp;yy.ay;
		set test;
		Inspection_year=&amp;amp;yy.;
	run;
	proc export data=Work.ID&amp;amp;yy.ay replace
		outfile = "E:\Ad_Year\ID&amp;amp;yy.ay"&lt;BR /&gt;/* or do you want the output file to be Text&lt;BR /&gt;	use this&lt;BR /&gt;		Outfile = "E:\Ad_Year\ID&amp;amp;yy.ay.txt"&lt;BR /&gt;*/
		dbms=dlm;
		delimiter=',';
	run;&lt;BR /&gt;proc datasets lib=work;&lt;BR /&gt;delete test;&lt;BR /&gt;quit;
%mend runner;
%runner(yy=1992);
%runner(yy=1993);
.
.
%runner(yy=2017);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; for the eagle eye added a delete.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 23:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532224#M145823</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-02-01T23:55:28Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532227#M145825</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122002"&gt;@VDD&lt;/a&gt;, should probably drop the test data set at the end of the loop. Otherwise if the import files, it'll use the previous years data.</description>
      <pubDate>Fri, 01 Feb 2019 23:09:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532227#M145825</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-01T23:09:07Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532232#M145828</link>
      <description>&lt;P&gt;Do NOT write the files back to the same name.&amp;nbsp; What if something goes wrong.&lt;/P&gt;
&lt;P&gt;Are you going to write the files back to the same directory?&amp;nbsp; Or a different directory.&lt;/P&gt;
&lt;P&gt;Do the files have header rows?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 23:44:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532232#M145828</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-01T23:44:36Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532233#M145829</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253384"&gt;@mmhxc5&lt;/a&gt; has an in directory Idaho and an out directory Ad_year&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 23:51:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532233#M145829</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-02-01T23:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532234#M145830</link>
      <description>&lt;P&gt;Do want to still have multiple files? Or can you just make one file?&lt;/P&gt;</description>
      <pubDate>Sat, 02 Feb 2019 00:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532234#M145830</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-02T00:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532237#M145833</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If you wanted to automate that, you could turn it into a macro and then call it for each year.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a tutorial for that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank" rel="noopener"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; solution will work as well, but I'm not a huge fan of running code I don't understand.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My example is basically a buffer manipulation. The Input places the entire line of data into the automatic variable _infile_ then depending on whether we need a header row or not appends a comma, since the OP says the file is CSV, and either the text YEAR for a header or the value of a year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The FILE statement documentation even has hints on this:&lt;/P&gt;
&lt;PRE&gt;Updating an External File in Place

You can use the FILE statement with the INFILE and PUT statements to update an external file &lt;BR /&gt;in place, updating either an entire record or only selected fields within a record. &lt;BR /&gt;Follow these guidelines: 
 
Always place the INFILE statement first. 

Specify the same fileref or physical filename in the INFILE and FILE statements. 
 
Use options that are common to both the INFILE and FILE statements &lt;BR /&gt;in the INFILE statement. (Any such options that are used in the FILE statement are ignored.) 
 
Use the SHAREBUFFERS option in the INFILE statement to allow the INFILE and FILE statements &lt;BR /&gt;to use the same buffer, which saves CPU time and enables you to update individual fields &lt;BR /&gt;instead of entire records. 
&lt;/PRE&gt;
&lt;P&gt;I could have used the _file_ variable as well instead of the&amp;nbsp;variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Feb 2019 00:49:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532237#M145833</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-02-02T00:49:21Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532239#M145834</link>
      <description>&lt;P&gt;It sounds like you have multiple files like this:&lt;/P&gt;
&lt;PRE&gt;&amp;gt;more ID*.txt | more
::::::::::::::
ID1992.txt
::::::::::::::
X,Y
1,2
::::::::::::::
ID1993.txt
::::::::::::::
X,Y
3,4
&lt;/PRE&gt;
&lt;P&gt;And you want to add a new column to each row.&lt;/P&gt;
&lt;P&gt;So just read in each row and write them back out.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let inpath=~/test/example;
%let outpath=&amp;amp;inpath/new ;

data _null_;
  length year infilename outfilename $200;
  infile "&amp;amp;inpath/ID*.txt" filename=infilename;
  input ;
  outfilename = catx('/',"&amp;amp;outpath",scan(infilename,-1,'/\'));
  year = substr(scan(infilename,-2,'/.\'),3);
  file out filevar=outfilename ;
  if infilename ne lag(infilename) then put 'YEAR,' @;
  else put year +(-1) ',' @;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;-&amp;gt;more ID*.txt | more
::::::::::::::
ID1992.txt
::::::::::::::
YEAR,X,Y
1992,1,2
::::::::::::::
ID1993.txt
::::::::::::::
YEAR,X,Y
1993,3,4&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Feb 2019 00:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532239#M145834</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-02T00:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532302#M145854</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;I understand your code, I wasn't sure the OP would, so s/he shouldn't run it :).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rationale for this type of 'rule' is that if you don't understand it enough you cannot make changes or understand what's happening when something goes wrong. Edge cases can also slip through if you're not checking your cases correctly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your solution is the one I would recommend as being the most correct. However, the macro approach will work as well.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Feb 2019 17:50:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532302#M145854</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-02T17:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532342#M145864</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122002"&gt;@VDD&lt;/a&gt;, thank you for your nice code. I run the code, it works well, but it adds only the Inspection_Year for 2017. Other years are blank. Could you please look at the code and see if there is any error? I really appreciate it. Thank you!&lt;/P&gt;</description>
      <pubDate>Sun, 03 Feb 2019 04:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532342#M145864</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-02-03T04:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532348#M145867</link>
      <description>Did you add in the macro call for the other years? &lt;BR /&gt;</description>
      <pubDate>Sun, 03 Feb 2019 05:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532348#M145867</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-03T05:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532352#M145871</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253384"&gt;@mmhxc5&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Others already provided solutions so not adding to this. I'd like to ask the question though why you would want to add this YEAR column to the text file in first place as you've got this information already in the filename itself. It's not hard to extract the year from the filename when reading the data into SAS (code below).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename in 'c:\temp\id*.txt';
data sample;
  length thisFile $300 year 8;
  infile in lrecl=255 truncover dlm=',' filename=thisFile;
  input; /* here your variable mapping reading the data */
  year=input(scan(thisFile,-2,'id.','ti'),best32.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Feb 2019 06:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532352#M145871</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-02-03T06:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532425#M145886</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, Yes, I added the macros for the other years. Here is the complete code I run, it works neatly, but only adds the Inspection_Year for 2017 and the other years are left blank.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro runner(yy=);
	proc import datafile="E:\Desktop\Idaho\ID&amp;amp;yy..txt"
		dbms=dlm out=work.test;
		delimiter =",";
		getnames=yes;
		guessingrows=1000000;
	run;
	data Work.ID&amp;amp;yy.ay;
		set test;
		Inspection_year=&amp;amp;yy.;
	run;
	proc export data=Work.ID&amp;amp;yy.ay replace
		outfile = "E:\Desktop\Idaho\Ad_ear\ID&amp;amp;yy.txt"
		dbms=dlm;
		delimiter=',';
	run;proc datasets lib=work;delete test;quit;
%mend runner;
%runner(yy=1992);
%runner(yy=1993);
%runner(yy=1994);
%runner(yy=1995);
%runner(yy=1996);
%runner(yy=1997);
%runner(yy=1998);
%runner(yy=1999);
%runner(yy=2000);
%runner(yy=2001);
%runner(yy=2002);
%runner(yy=2003);
%runner(yy=2004);
%runner(yy=2005);
%runner(yy=2006);
%runner(yy=2007);
%runner(yy=2008);
%runner(yy=2009);
%runner(yy=2010);
%runner(yy=2011);
%runner(yy=2012);
%runner(yy=2013);
%runner(yy=2014);
%runner(yy=2015);
%runner(yy=2016);
%runner(yy=2017);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Feb 2019 16:46:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532425#M145886</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-02-03T16:46:41Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532426#M145887</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;, I need the Year column in each file, whereas it is only provided on the filename. Now I use the filename to add the Year column inside each file.&lt;/P&gt;</description>
      <pubDate>Sun, 03 Feb 2019 16:44:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532426#M145887</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-02-03T16:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable based on the filename and exporting the file as its original type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532472#M145903</link>
      <description>Add:&lt;BR /&gt;&lt;BR /&gt;options mprint symbolgen;&lt;BR /&gt;&lt;BR /&gt;Run it for two files and post the log please.</description>
      <pubDate>Mon, 04 Feb 2019 01:18:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-based-on-the-filename-and-exporting-the-file/m-p/532472#M145903</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-04T01:18:51Z</dc:date>
    </item>
  </channel>
</rss>

