<?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: Conditional Infile in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675111#M203356</link>
    <description>&lt;P&gt;Can you describe any rules based on the values in the data that allow you to identify when this has happened? Something like a value, or pattern of values in an unexpected column. It is very hard to test for something that is not there, such as a "missing carriage return"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is possible to examine a line of data. Consider this code:&lt;/P&gt;
&lt;PRE&gt;data _null_;
   infile &amp;lt;options&amp;gt;;
   input @;
   put _infile_;
run;  &lt;/PRE&gt;
&lt;P&gt;The Input statement always creates a SAS automatic variable called _infile_. So you can examine the contents of that variable just like any long string variable. The above code just writes the line to the log so you could test with a small text file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is a moderately common problem with data that originates as a report.&lt;/P&gt;
&lt;P&gt;For example I have written code that parsed lines until a key value like "Clinic: ", which tells me I am at a new start of a report section, and then looking for specific other words to read selected rows for values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use code like:&lt;/P&gt;
&lt;P&gt;If _infile_ =: "Keyvalue" then input &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/105043"&gt;@23&lt;/a&gt; Var1 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/174952"&gt;@55&lt;/a&gt; var2;&lt;/P&gt;
&lt;P&gt;as an example, which checks to see if the line starts with the key value then read two specific variables starting at specified column positions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, rules?&lt;/P&gt;</description>
    <pubDate>Thu, 06 Aug 2020 19:19:05 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-08-06T19:19:05Z</dc:date>
    <item>
      <title>Conditional Infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675096#M203349</link>
      <description>&lt;P&gt;I have a tab delimited flat file that periodically contains structure shifts due to limitations of the software it is exported from. Example lines are below. The last line of the example data shows the problem. When data is missing (outside of the "header" columns), there is not a carriage return to go the next line. The next line just begins where the previous left off.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	27-JAN-2020	1	12-OCT-2019 / 2	 8.000	A	SMITH, JOHN P (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	29-JAN-2020	2	05-MAY-2018 / 2	 8.000	A	SMITH, JOHN R (999999)	EMPLOYEE RELATIONS [3]	PROCESSING [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	21-JAN-2020	3	13-JUN-2009 / 6	 8.000	A	SMITH, JOHN J (999999)	MECHANICAL [3]	MECHANICAL [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	20-JAN-2020	4	07-DEC-2006 / 2	 8.000	A	SMITH, JOHN O (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	19-JAN-2020	5	24-MAR-2001 / 8	 8.000	A	SMITH, JOHN C (999999)	PROCESSING [3]	PROCESSING [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	30-JAN-2020	6	03-NOV-2002 / 4	 8.000	A	SMITH, JOHN G (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	10-FEB-2020	Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	11-FEB-2020	1	22-NOV-2001 / 8	 8.000	A	SMITH, JOHN S (999999)	PACKING [2]	PACKING [2]	
&lt;/PRE&gt;
&lt;P&gt;I would like to be able to handle these situations within the infile read, but I have not been able to consistently / sustainably capture the issue. I am hoping there is a way detect that the current infile line is longer than expected, and thus "move" SAS to the next observation, e.g. the final SAS table should look like..&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	27-JAN-2020	1	12-OCT-2019 / 2	 8.000	A	SMITH, JOHN P (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	29-JAN-2020	2	05-MAY-2018 / 2	 8.000	A	SMITH, JOHN R (999999)	EMPLOYEE RELATIONS [3]	PROCESSING [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	21-JAN-2020	3	13-JUN-2009 / 6	 8.000	A	SMITH, JOHN J (999999)	MECHANICAL [3]	MECHANICAL [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	20-JAN-2020	4	07-DEC-2006 / 2	 8.000	A	SMITH, JOHN O (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	19-JAN-2020	5	24-MAR-2001 / 8	 8.000	A	SMITH, JOHN C (999999)	PROCESSING [3]	PROCESSING [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	30-JAN-2020	6	03-NOV-2002 / 4	 8.000	A	SMITH, JOHN G (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	10-FEB-2020	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	11-FEB-2020	1	22-NOV-2001 / 8	 8.000	A	SMITH, JOHN S (999999)	PACKING [2]	PACKING [2]	
&lt;/PRE&gt;
&lt;P&gt;I've tried combinations of missover / truncover / flowover, as well as _infile_ variable, but I can't seem to get the pointer to move along to the next SAS observation while still within 1 row of the input file.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Aug 2020 18:18:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675096#M203349</guid>
      <dc:creator>JoshB</dc:creator>
      <dc:date>2020-08-06T18:18:58Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675111#M203356</link>
      <description>&lt;P&gt;Can you describe any rules based on the values in the data that allow you to identify when this has happened? Something like a value, or pattern of values in an unexpected column. It is very hard to test for something that is not there, such as a "missing carriage return"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is possible to examine a line of data. Consider this code:&lt;/P&gt;
&lt;PRE&gt;data _null_;
   infile &amp;lt;options&amp;gt;;
   input @;
   put _infile_;
run;  &lt;/PRE&gt;
&lt;P&gt;The Input statement always creates a SAS automatic variable called _infile_. So you can examine the contents of that variable just like any long string variable. The above code just writes the line to the log so you could test with a small text file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is a moderately common problem with data that originates as a report.&lt;/P&gt;
&lt;P&gt;For example I have written code that parsed lines until a key value like "Clinic: ", which tells me I am at a new start of a report section, and then looking for specific other words to read selected rows for values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use code like:&lt;/P&gt;
&lt;P&gt;If _infile_ =: "Keyvalue" then input &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/105043"&gt;@23&lt;/a&gt; Var1 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/174952"&gt;@55&lt;/a&gt; var2;&lt;/P&gt;
&lt;P&gt;as an example, which checks to see if the line starts with the key value then read two specific variables starting at specified column positions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, rules?&lt;/P&gt;</description>
      <pubDate>Thu, 06 Aug 2020 19:19:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675111#M203356</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-06T19:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675123#M203359</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;the only 'rule' here would be that the 8th encountered value would be "Name" instead of 1 or 2 digit number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I'm understanding your reply correctly, I'd need to first check if the current _infile_ record had a string "Name" or not in the 8th tab delimited column. However, I'm still not sure how I would tell SAS, if "Name" then keep the previous 7 read-in values as one observation, and continue reading the current _infile_ but outputting the results to a new observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Aug 2020 19:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675123#M203359</guid>
      <dc:creator>JoshB</dc:creator>
      <dc:date>2020-08-06T19:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675128#M203363</link>
      <description>&lt;P&gt;Next tested code deals with any shifting, not only when&amp;nbsp; var8='Name'.&lt;/P&gt;
&lt;P&gt;All variables saved as char type. you may need add a step to assign your names with appropriate length and format:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
  infile cards dlm='09'x truncover;
  input a_line $250.;
cards;
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	27-JAN-2020	1	12-OCT-2019 / 2	 8.000	A	SMITH, JOHN P (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	29-JAN-2020	2	05-MAY-2018 / 2	 8.000	A	SMITH, JOHN R (999999)	EMPLOYEE RELATIONS [3]	PROCESSING [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	21-JAN-2020	3	13-JUN-2009 / 6	 8.000	A	SMITH, JOHN J (999999)	MECHANICAL [3]	MECHANICAL [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	20-JAN-2020	4	07-DEC-2006 / 2	 8.000	A	SMITH, JOHN O (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	19-JAN-2020	5	24-MAR-2001 / 8	 8.000	A	SMITH, JOHN C (999999)	PROCESSING [3]	PROCESSING [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	30-JAN-2020	6	03-NOV-2002 / 4	 8.000	A	SMITH, JOHN G (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	10-FEB-2020	Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	11-FEB-2020	1	22-NOV-2001 / 8	 8.000	A	SMITH, JOHN S (999999)	PACKING [2]	PACKING [2]	
; run;

%let nvars = 14;
data tmp2;
set temp;
    length var1-var&amp;amp;nvars varx $30;
    array vx {*} $ var1-var&amp;amp;nvars;
    do i=1 to dim(vx);
       varx = scan(a_line,i,'09'x);
       if i&amp;gt;1 and varx = 'Name' then do;  /* issue line */
          output; 
          do j=1 to dim(vx); 
               vx(j) = scan(a_line,i,'09'x);
               i+1; 
          end;
          output;
       end;
       else do; /* normal line */
          vx(i) = varx;
          if i = dim(vx) then output;
       end;
    end;
    drop a_line varx i j;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Aug 2020 20:19:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675128#M203363</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-06T20:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675202#M203405</link>
      <description>&lt;P&gt;You may prefer next code (more simple then my previous solution):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
  infile cards dlm='09'x truncover;
  input a_line $250.;
cards;
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	27-JAN-2020	1	12-OCT-2019 / 2	 8.000	A	SMITH, JOHN P (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	29-JAN-2020	2	05-MAY-2018 / 2	 8.000	A	SMITH, JOHN R (999999)	EMPLOYEE RELATIONS [3]	PROCESSING [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	21-JAN-2020	3	13-JUN-2009 / 6	 8.000	A	SMITH, JOHN J (999999)	MECHANICAL [3]	MECHANICAL [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	20-JAN-2020	4	07-DEC-2006 / 2	 8.000	A	SMITH, JOHN O (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	19-JAN-2020	5	24-MAR-2001 / 8	 8.000	A	SMITH, JOHN C (999999)	PROCESSING [3]	PROCESSING [3]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	30-JAN-2020	6	03-NOV-2002 / 4	 8.000	A	SMITH, JOHN G (999999)	MECHANICAL [2]	MECHANICAL [2]	
Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	10-FEB-2020	Name	Code	Hr	Bid Department	Called-In Abs Dept	Seniority	11-FEB-2020	1	22-NOV-2001 / 8	 8.000	A	SMITH, JOHN S (999999)	PACKING [2]	PACKING [2]	
; run;

data tmp2 (keep=var1-var14);
     length line_out $200  var1-var14 $30;
     array vx {*} $ var1-var14;
 set temp;
     split_pos = index(substr(a_line,5),"Name"); 
     if split_pos = 0 then do;
        line_out = strip(a_line);
        link out;
     end;
     else do;
        line_out = substr(a_line,1,split_pos+3);
        link out;
        line_out = substr(a_line,split_pos+4);
        link out;
     end;
return;
out:
   do i=1 to 14;
      vx(i) = scan(line_out,i,'09'x);
      if missing(vx(i)) then leave;
   end;
   output tmp2;
return;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Aug 2020 09:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Infile/m-p/675202#M203405</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-07T09:54:41Z</dc:date>
    </item>
  </channel>
</rss>

