<?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 Read multiple .TXT files into SAS with a different format for each row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Read-multiple-TXT-files-into-SAS-with-a-different-format-for/m-p/438391#M109298</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope you can help me!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've a folder of .TXT files which I try to read in SAS into on table. This is working - by itself - fine. However, I've also .TXT files for which I've to use a different INPUT / FORMAT combination based on the first two digits of each row. This is also working - by itself - fine. Now, I try to combine both which is resulting in a&amp;nbsp;program which is running forever...&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HISTORY_&amp;amp;BORD_NAME;
SET FILELIST;

FILEPATH = "&amp;amp;DIRNAME"||FILE_NAME;
INFILE TEMP FILEVAR = FILEPATH END = DONE TRUNCOVER;

INPUT @1 RECORD_ID $2. @;

DO WHILE(NOT DONE);
	IF RECORD_ID = "11" THEN
		INPUT
		        @1   RECORD_ID	 		$2.
		        @3   DEALER_NUMBER		$4.
		;
		FORMAT
				RECORD_ID		 		$CHAR2.
				DEALER_NUMBER    		$CHAR4.
		;
	ELSE IF RECORD_ID = "22" THEN 
		INPUT 
	        	@1   RECORD_ID	 		$2.
		    	@3   INVOICE_NUMBER		$7.
		;	
		FORMAT
				RECORD_ID		 		$CHAR2.
				INVOICE_NUMBER    		$CHAR4.
		;
	OUTPUT;
END;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What's the mistake in the above which is causing the "forever" run?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Many thanks!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;Sven&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 19 Feb 2018 13:16:07 GMT</pubDate>
    <dc:creator>DjeezD</dc:creator>
    <dc:date>2018-02-19T13:16:07Z</dc:date>
    <item>
      <title>Read multiple .TXT files into SAS with a different format for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-multiple-TXT-files-into-SAS-with-a-different-format-for/m-p/438391#M109298</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope you can help me!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've a folder of .TXT files which I try to read in SAS into on table. This is working - by itself - fine. However, I've also .TXT files for which I've to use a different INPUT / FORMAT combination based on the first two digits of each row. This is also working - by itself - fine. Now, I try to combine both which is resulting in a&amp;nbsp;program which is running forever...&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HISTORY_&amp;amp;BORD_NAME;
SET FILELIST;

FILEPATH = "&amp;amp;DIRNAME"||FILE_NAME;
INFILE TEMP FILEVAR = FILEPATH END = DONE TRUNCOVER;

INPUT @1 RECORD_ID $2. @;

DO WHILE(NOT DONE);
	IF RECORD_ID = "11" THEN
		INPUT
		        @1   RECORD_ID	 		$2.
		        @3   DEALER_NUMBER		$4.
		;
		FORMAT
				RECORD_ID		 		$CHAR2.
				DEALER_NUMBER    		$CHAR4.
		;
	ELSE IF RECORD_ID = "22" THEN 
		INPUT 
	        	@1   RECORD_ID	 		$2.
		    	@3   INVOICE_NUMBER		$7.
		;	
		FORMAT
				RECORD_ID		 		$CHAR2.
				INVOICE_NUMBER    		$CHAR4.
		;
	OUTPUT;
END;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What's the mistake in the above which is causing the "forever" run?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Many thanks!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;Sven&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 13:16:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-multiple-TXT-files-into-SAS-with-a-different-format-for/m-p/438391#M109298</guid>
      <dc:creator>DjeezD</dc:creator>
      <dc:date>2018-02-19T13:16:07Z</dc:date>
    </item>
    <item>
      <title>Re: Read multiple .TXT files into SAS with a different format for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-multiple-TXT-files-into-SAS-with-a-different-format-for/m-p/438394#M109300</link>
      <description>&lt;P&gt;If you have two different data metadata, then write two different data import programs, simple then.&amp;nbsp; You could do a pre-check of a file and call the relevant code from that:&lt;/P&gt;
&lt;PRE&gt;%macro import_data (f=);
  data _null_;
    infile "&amp;amp;f." obs=1;
    input;
    if _input_="01" then call execute(cats('%Import_seta (f=',&amp;amp;f.,'));'));
    else call execute('cats('%impor_setb (f=',&amp;amp;f.,'));'));
  run;

%mend import_data;



data _null_;
  set listoffiles;
  call execute(cats('%import_data (f=',filemname,');));
run;&lt;/PRE&gt;
&lt;P&gt;Something like that, so for each file, a line is read in, if the text=per the if, then call one macro else call another.&amp;nbsp; Of course I am flying blind here in your process, this is just an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please avoid coding in capitals, it really makes it unreadable.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 13:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-multiple-TXT-files-into-SAS-with-a-different-format-for/m-p/438394#M109300</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-02-19T13:34:18Z</dc:date>
    </item>
    <item>
      <title>Re: Read multiple .TXT files into SAS with a different format for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-multiple-TXT-files-into-SAS-with-a-different-format-for/m-p/438398#M109303</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;What's the mistake in the above which is causing the "forever" run?&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Could be that one of your data files contains a record which does not begin with "11" or "22" - in which case your datastep will not execute any INPUT statements, only the final OUTPUT statement, and will be stuck in the DO loop. Or it may be a problem with the END=DONE condition, which may not be set correctly&amp;nbsp;when you read from multiple infiles like that. In the last case, you may want to take a look at the EOF= option for infiles.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All in all, I would try something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HISTORY_&amp;amp;BORD_NAME;
  Next_file:
  SET FILELIST;

  FILEPATH = "&amp;amp;DIRNAME"||FILE_NAME;
  INFILE TEMP FILEVAR = FILEPATH EOF=Next_file TRUNCOVER;

  DO WHILE(1);
    INPUT @1 RECORD_ID $2. @;
    IF RECORD_ID = "11" THEN
      INPUT
        @3   DEALER_NUMBER            $4.
        ;
    ELSE IF RECORD_ID = "22" THEN
      INPUT
        @3   INVOICE_NUMBER            $7.
        ;
    else do;  /* In case we have an invalid(?) record ID */
      input;  /* skip to next line */
      error;  /* write the stuff to log */
      continue; /* do not output */
      end;
    OUTPUT;
    END;
 FORMAT
   RECORD_ID        $CHAR2.
   DEALER_NUMBER    $CHAR4.
   INVOICE_NUMBER   $CHAR4.
   ;
RUN;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This EOF= option makes the program jump to the label indicated when a read after end of file occurs. And the SET statement terminates the datastep, when there are no more records in FILELIST.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I moved the FORMAT statement to the end, as it is not executable (it makes no sense to put it inside a conditionally executed block).&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 14:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-multiple-TXT-files-into-SAS-with-a-different-format-for/m-p/438398#M109303</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-02-19T14:09:08Z</dc:date>
    </item>
  </channel>
</rss>

