<?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: Import .txt file and convert to .csv file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311227#M270714</link>
    <description>&lt;P&gt;Use list input without the DSD option to read; use list output with DLM=',' to create the csv file.&lt;/P&gt;
&lt;P&gt;For the percent values, use the PERCENTw.d format to read and write.&lt;/P&gt;
&lt;P&gt;All in all a very simple task.&lt;/P&gt;</description>
    <pubDate>Sun, 13 Nov 2016 08:32:24 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2016-11-13T08:32:24Z</dc:date>
    <item>
      <title>Import .txt file and convert to .csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311225#M270713</link>
      <description>&lt;P&gt;Appreciate if someone guide me with the program to import .txt file which has records as follows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Filesystem GB blocks Free %Used Iused %Iused Mounted on&lt;BR /&gt;/dev/sasuserl 1419.00 503.79 65% 48937 1% /sasuser&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After import, I want to export to .csv as follows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="540"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;Filesystem&lt;/TD&gt;
&lt;TD width="93"&gt;&amp;nbsp;GB blocks&lt;/TD&gt;
&lt;TD width="64"&gt;Free&lt;/TD&gt;
&lt;TD width="64"&gt;%Used&lt;/TD&gt;
&lt;TD width="67"&gt;Iused&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;%Iused&lt;/TD&gt;
&lt;TD width="83"&gt;Mounted on&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;/dev/sasuserl&lt;/TD&gt;
&lt;TD&gt;1419.00&lt;/TD&gt;
&lt;TD&gt;503.79&lt;/TD&gt;
&lt;TD&gt;65%&lt;/TD&gt;
&lt;TD&gt;48937%&lt;/TD&gt;
&lt;TD&gt;1%&lt;/TD&gt;
&lt;TD&gt;/sasuser&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for any help you offer me.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2016 07:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311225#M270713</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2016-11-13T07:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: Import .txt file and convert to .csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311227#M270714</link>
      <description>&lt;P&gt;Use list input without the DSD option to read; use list output with DLM=',' to create the csv file.&lt;/P&gt;
&lt;P&gt;For the percent values, use the PERCENTw.d format to read and write.&lt;/P&gt;
&lt;P&gt;All in all a very simple task.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2016 08:32:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311227#M270714</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-13T08:32:24Z</dc:date>
    </item>
    <item>
      <title>Re: Import .txt file and convert to .csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311228#M270715</link>
      <description>&lt;P&gt;Both .csv and .txt are text files. They just have different delimiters. So in your case you could simply read the source text file into the input buffer, exchange the source delimiter with a comma and then write the result straight back to the output file. There is no need to map the data to SAS variables and input source strings into SAS numeric variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* create source text file with delimiter '|' (tab or whatever in your real data) */&lt;BR /&gt;filename have temp;&lt;BR /&gt;data _null_;&lt;BR /&gt; file have;&lt;BR /&gt; infile datalines;&lt;BR /&gt; input;&lt;BR /&gt; put _infile_;&lt;BR /&gt; datalines;&lt;BR /&gt;Filesystem|GB blocks|Free|%Used|Iused|%Iused|Mounted on&lt;BR /&gt;/dev/sasuserl|1419.00|503.79|65%|48937|1%|/sasuser&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/* read source file "want" into input buffer and replace the source file delimiter (tab?) with comma */&lt;BR /&gt;filename want temp;&lt;BR /&gt;data _null_;&lt;BR /&gt; file want;&lt;BR /&gt; infile have;&lt;BR /&gt; input;&lt;BR /&gt; _infile_=translate(_infile_,',','|');&lt;BR /&gt; put _infile_;&lt;BR /&gt; /* putlog just here to see in sample code what gets written to file "want" */&lt;BR /&gt; putlog _infile_;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And in case your source text file doesn't have a delimiter and data is just written out positional with blanks then the following code version could deal with that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create source text file */
filename have temp;
data _null_;
  file have;
  infile datalines;
  input;
  put _infile_;
  datalines;
Filesystem    GB blocks Free   %Used Iused %Iused Mounted on
/dev/sasuserl 1419.00   503.79 65%   48937 1%     /sasuser
;
run;

/* read source file "want" into input buffer and replace selected blanks with comma */
filename want temp;
data _null_;
  file want;
  infile have;
  input;
  _infile_=prxchange('s/(?!\sblocks|\son|\s+$)\s+/,/oi',-1,_infile_);
  put _infile_;
  /* putlog just here to see in sample code what gets written to file "want" */
  putlog _infile_;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Nov 2016 09:28:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311228#M270715</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-11-13T09:28:14Z</dc:date>
    </item>
    <item>
      <title>Re: Import .txt file and convert to .csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311231#M270716</link>
      <description>&lt;P&gt;Thank you for quick response. However, I'm unable to view the file (#LN00019) which was created in second data step.See the log below. &amp;nbsp;I'm in a need output .csv file after reading the input .txt file. Also in your first data step, you've hard coded the values which was in input .txt. Could you please tell me how to accomplish this task by reading the .txt file without hardcoding the values in it?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: The file WANT is:
      Filename=/saswork/v9.4/SAS_work97110001A_ksnsnk02/#LN00019,
      Owner Name=rxxx,Group Name=sas,
      Access Permission=-rw-rw-r--,
      Last Modified=13Nov2016:14:50:55

NOTE: The infile HAVE is:
      Filename=/saswork/v9.4/SAS_work97110001A_ksnsnk02/#LN00018,
      Owner Name=rxxx,Group Name=sas,
      Access Permission=-rw-rw-r--,
      Last Modified=13Nov2016:14:50:55,
      File Size (bytes)=162&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2016 09:44:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311231#M270716</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2016-11-13T09:44:16Z</dc:date>
    </item>
    <item>
      <title>Re: Import .txt file and convert to .csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311232#M270717</link>
      <description>&lt;P&gt;&lt;EM&gt;I'm unable to view the file (#LN00019) which was created in second data step&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;That's why I've also used "putlog" so you can see the output in the log.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For your real code you want of course to write the file to a permanent location and not to WORK so your code should look like:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename have '&amp;lt;input path&amp;gt;/&amp;lt;name of source text file&amp;gt;';
filename want '&amp;lt;output path&amp;gt;/&amp;lt;name of target csv file&amp;gt;';
data _null_;
  file want;
  infile have;
  input;
  _infile_=prxchange('s/(?!\sblocks|\son|\s+$)\s+/,/oi',-1,_infile_);
  put _infile_;
run;
filename have clear;
filename want clear;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2016 10:13:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311232#M270717</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-11-13T10:13:53Z</dc:date>
    </item>
    <item>
      <title>Re: Import .txt file and convert to .csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311233#M270718</link>
      <description>&lt;P&gt;Or even this way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  file '&amp;lt;output path&amp;gt;/&amp;lt;name of target csv file&amp;gt;';
  infile '&amp;lt;input path&amp;gt;/&amp;lt;name of source text file&amp;gt;';
  input;
  _infile_=prxchange('s/(?!\sblocks|\son|\s+$)\s+/,/oi',-1,_infile_);
  put _infile_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW: If you're the one creating the initial report via Unix command then you could of course also pipe the result directly to a RegEx and create the .csv directly without any SAS involved.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2016 10:22:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311233#M270718</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-11-13T10:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: Import .txt file and convert to .csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311234#M270719</link>
      <description>&lt;P&gt;The most simple code to convert text file (blank delimiter) into csv (comma delimiter) is by:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;length a_row $100;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;infile '... input path and filename.txt' truncover;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;input a_row $;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a_row = translate(compbl(a_row), ',' , ' ');&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a_row = compress(a_row, '%');&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;file '... output path and filename.csv';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;put a_row ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2016 10:43:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-txt-file-and-convert-to-csv-file/m-p/311234#M270719</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-13T10:43:53Z</dc:date>
    </item>
  </channel>
</rss>

