<?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: How to add a variable column based on the filename in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/531428#M145440</link>
    <description>&lt;P&gt;If you want to copy the text then use a DATA _NULL_ add a FILE statement to point to the NEW file you want to create. Then use the _INFILE_ automatic variable to copy the line.&amp;nbsp; So after you have figured out the year instead of trying the read the data from the line your code looks something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;file "newfilename";
put year _infile_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might need to set a LRECL= on the FILE statement if the lines are longer than the default 32K bytes.&lt;/P&gt;
&lt;P&gt;You might want to put a delimiter character after the YEAR value and before the rest of the line.&lt;/P&gt;
&lt;P&gt;You might want to eliminate the space that SAS will normally put after the value of YEAR.&lt;/P&gt;
&lt;P&gt;You might be able to do both by adding the DSD and DLM= option to the FILE statement. Test it and see.&amp;nbsp; But you could just do it in the PUT statement.&lt;/P&gt;
&lt;P&gt;For example if you wanted to add a comma right after the value of year.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put year +(-1) ',' _infile_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 30 Jan 2019 16:26:15 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-01-30T16:26:15Z</dc:date>
    <item>
      <title>How to add a variable column based on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530417#M145045</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I would like import several comma delimited .txt files at once with names&amp;nbsp; ID1992, ID1993, ..., ID2017, add variable YEAR from 1992, to 2017 based on the file name for each file 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;I appreciate your time and help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jan 2019 04:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530417#M145045</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-27T04:44:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a variable column based on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530419#M145047</link>
      <description>&lt;P&gt;Use the FILENAME= option on the INFILE statement to know what file you are reading.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=/folder/with/the/files;
data want ;
  length filename $200 ;
  input "&amp;amp;path/ID*.txt" dsd filename=filename truncover ;
  input @ ;
  year = input(substr(scan(filename,-2,'./\'),3),4.);
  input var1 var2 ....;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 27 Jan 2019 04:59:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530419#M145047</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-27T04:59:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a variable column based on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530480#M145079</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, Thank you for your reply.&lt;/P&gt;&lt;P&gt;This is what I wrote.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Let path =E:\Desktop\Idaho;
data want;
	length filename $200;
	input "&amp;amp;path/ID*.txt" dsd filename=filename truncover;
input @;
year = input(substr(scan(filename,-2'./\'),3,4.);
input Year;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and the following is the error I am getting.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 556px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26607i3036A6A95D96AF6A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jan 2019 15:32:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530480#M145079</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-27T15:32:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a variable column based on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530486#M145080</link>
      <description>&lt;P&gt;The statement with the path and truncover and other options is INFILE not INPUT.&lt;/P&gt;
&lt;P&gt;In the assignment statement for YEAR you are missing a comma between the last two arguments of the SCAN() function.&lt;/P&gt;
&lt;P&gt;You don't want to INPUT the variable YEAR since you just gave it a value on the line above.&amp;nbsp; You want to input the variable(s) that are actually in your csv files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also if your CSV files each have a header rows then you will need to add this before the last INPUT statement so that you can skip the header.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if filename ne lag(filename) then delete;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 27 Jan 2019 16:59:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530486#M145080</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-27T16:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a variable column based on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530487#M145081</link>
      <description>&lt;P&gt;Actually, you had INPUT in your example &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other error&amp;nbsp; is due to a missing comma after -2.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jan 2019 17:55:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530487#M145081</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2019-01-27T17:55:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a variable column based on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530537#M145094</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, Thanks for correcting me. I got the code a little further, but still I get another error.&lt;/P&gt;&lt;P&gt;Here is what I wrote.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Let path =E:\Desktop\Idaho;
data want;
	length filename $200;
	infile "&amp;amp;path/ID*.txt" dsd filename=filename truncover;
infile @;
year = input(substr(scan(filename,-2,'./\'),3),4.);
if filename ne lag(filename) then delete;
input Inspection_Year;                
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and the following is the error I get.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 557px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26615i87A035632E7D9196/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 03:19:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530537#M145094</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-28T03:19:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a variable column based on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530539#M145096</link>
      <description>&lt;P&gt;You changed the first INPUT statement (with the trailing @ )&amp;nbsp; that was reading and holding the next line of data so that the FILENAME variable would get set to an invalid INFILE statement.&amp;nbsp; &amp;nbsp;Convert it back to in INPUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What data is in the files?&amp;nbsp; Do they really only have one number per line?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 05:31:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/530539#M145096</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-28T05:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a variable column based on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/531408#M145435</link>
      <description>&lt;DIV&gt;&lt;DIV&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;, Thanks for your reply. I revised the code and added INPUT statement (with the trailing @), the code worked without any error. As I said in my first post, I want to add a column YEAR within each file based on its filename and export it as it is. Based on your code I get a data set combined as SAS dataset with variables I select from the .txt files. I don't want to interfere with the variables inside the files, I just want to add the column YEAR for each file and export it as .txt.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Each file has 137 variables and more than 50,000 rows of data - character and numeric. I really appreciate your help.&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 30 Jan 2019 15:50:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/531408#M145435</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-30T15:50:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a variable column based on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/531428#M145440</link>
      <description>&lt;P&gt;If you want to copy the text then use a DATA _NULL_ add a FILE statement to point to the NEW file you want to create. Then use the _INFILE_ automatic variable to copy the line.&amp;nbsp; So after you have figured out the year instead of trying the read the data from the line your code looks something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;file "newfilename";
put year _infile_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might need to set a LRECL= on the FILE statement if the lines are longer than the default 32K bytes.&lt;/P&gt;
&lt;P&gt;You might want to put a delimiter character after the YEAR value and before the rest of the line.&lt;/P&gt;
&lt;P&gt;You might want to eliminate the space that SAS will normally put after the value of YEAR.&lt;/P&gt;
&lt;P&gt;You might be able to do both by adding the DSD and DLM= option to the FILE statement. Test it and see.&amp;nbsp; But you could just do it in the PUT statement.&lt;/P&gt;
&lt;P&gt;For example if you wanted to add a comma right after the value of year.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put year +(-1) ',' _infile_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 16:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-a-variable-column-based-on-the-filename/m-p/531428#M145440</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-30T16:26:15Z</dc:date>
    </item>
  </channel>
</rss>

