<?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 reading pipe delimited file with multiple record layouts in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328874#M73492</link>
    <description>&lt;P&gt;Hello.&amp;nbsp; I am trying to read in pipe delimited data that has multiple record layouts.&amp;nbsp; I have tried several methods that get me close but with no success.&amp;nbsp; There are&amp;nbsp;3 issues I am encountering with the data (1) There are no end-of-record pipes (2) The first field will determing the remaining record layouts (3) I do not have position information for the fields, they are only seperated by "|".&amp;nbsp; Below is the first method I have tried in SAS 9.4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Method 1:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; infile indata dlm='|' firstobs=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input @1 rec_type $2.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if rec_type='RA';&lt;/P&gt;&lt;P&gt;&amp;nbsp; input rec_type $2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $2&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Method 2:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data&amp;nbsp;want&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;BR /&gt;&amp;nbsp; %let _EFIERR_ = 0; /* set the ERROR detection macro variable */&lt;BR /&gt;&amp;nbsp; infile&amp;nbsp;indata&lt;BR /&gt;&amp;nbsp; delimiter = '|'&amp;nbsp; DSD lrecl=32767 ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; informat&amp;nbsp;rec_type $2.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;input&amp;nbsp;&amp;nbsp;rec_type $;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if&amp;nbsp;&amp;nbsp;&amp;nbsp;rec_type='RA' then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; informat var2 $2.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; format var2 ;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem&amp;nbsp;is trying to address the changing record layout and telling says where the end of the record is. Attached is a sample of the data file I am working with.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 31 Jan 2017 20:57:53 GMT</pubDate>
    <dc:creator>lslockle</dc:creator>
    <dc:date>2017-01-31T20:57:53Z</dc:date>
    <item>
      <title>reading pipe delimited file with multiple record layouts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328874#M73492</link>
      <description>&lt;P&gt;Hello.&amp;nbsp; I am trying to read in pipe delimited data that has multiple record layouts.&amp;nbsp; I have tried several methods that get me close but with no success.&amp;nbsp; There are&amp;nbsp;3 issues I am encountering with the data (1) There are no end-of-record pipes (2) The first field will determing the remaining record layouts (3) I do not have position information for the fields, they are only seperated by "|".&amp;nbsp; Below is the first method I have tried in SAS 9.4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Method 1:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; infile indata dlm='|' firstobs=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input @1 rec_type $2.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if rec_type='RA';&lt;/P&gt;&lt;P&gt;&amp;nbsp; input rec_type $2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $2&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Method 2:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data&amp;nbsp;want&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;BR /&gt;&amp;nbsp; %let _EFIERR_ = 0; /* set the ERROR detection macro variable */&lt;BR /&gt;&amp;nbsp; infile&amp;nbsp;indata&lt;BR /&gt;&amp;nbsp; delimiter = '|'&amp;nbsp; DSD lrecl=32767 ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; informat&amp;nbsp;rec_type $2.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;input&amp;nbsp;&amp;nbsp;rec_type $;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if&amp;nbsp;&amp;nbsp;&amp;nbsp;rec_type='RA' then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; informat var2 $2.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; format var2 ;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem&amp;nbsp;is trying to address the changing record layout and telling says where the end of the record is. Attached is a sample of the data file I am working with.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 20:57:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328874#M73492</guid>
      <dc:creator>lslockle</dc:creator>
      <dc:date>2017-01-31T20:57:53Z</dc:date>
    </item>
    <item>
      <title>Re: reading pipe delimited file with multiple record layouts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328878#M73496</link>
      <description>&lt;P&gt;Did anyone give you a data dictionary or file layout document? If not, you will need to tell us in excrutiating detail how you know which record format descripter is used for any given line of data and what any given "column" characterstics should be.&lt;/P&gt;
&lt;P&gt;If the same "variables" meaning they will have the same variable name occurs in different row layouts then you will need to specify that as well.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 21:10:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328878#M73496</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-31T21:10:14Z</dc:date>
    </item>
    <item>
      <title>Re: reading pipe delimited file with multiple record layouts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328880#M73497</link>
      <description>&lt;P&gt;Attached are the field descriptions based on the first field of each record&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 21:16:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328880#M73497</guid>
      <dc:creator>lslockle</dc:creator>
      <dc:date>2017-01-31T21:16:53Z</dc:date>
    </item>
    <item>
      <title>Re: reading pipe delimited file with multiple record layouts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328885#M73499</link>
      <description>&lt;P&gt;You just need to know the fields that each record type contains.&lt;/P&gt;
&lt;P&gt;Since you didn't tell us what information is in which type of record, let's just make up simplified example.&lt;/P&gt;
&lt;P&gt;Let's assume the first field is the record type, let's call it TYPE, and the second is the identifier, lets' call it ID.&lt;/P&gt;
&lt;P&gt;Now let's say the type1 contains five fields, let's call them A1-A5. and type2 contain three fields that we can call B1-B3.&lt;/P&gt;
&lt;P&gt;You will probably want to write the record types to different datasets sicne they have different variables.&lt;/P&gt;
&lt;P&gt;So let's setup a program to do that. &amp;nbsp;It is just a minor modifciation from the program you would want to make for reading any delimited file. &amp;nbsp;First you define your variables and set your input location.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length type $3 id $8 A1-A5 $20 B1-B3 $10 ;
infile cards dsd dlm='|' truncover ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Normally then all you need is to write a simple INPUT statement that just lists the variable names in the order they are in the source file. &amp;nbsp;But in this case we need to add something to support writing out two or more output datasets and for reading in different variables based on the record type.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data
      type1 (keep=id A1-A5) 
      type2 (keep=id B1-B3) 
;
  length type $3 id $8 A1-A5 $20 B1-B3 $10 ;
  infile cards dsd dlm='|' truncover ;
  input type id @ ;
  if type='AA' then do;
     input A1-A5 ;
     output type1;
  end;
  else if type='BB' then do;
     input B1-B3 ;
     output type2;
  end;
  else do;
     put 'ERROR: Invalid record type. ' type= / _infile_;
     delete;
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 21:27:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328885#M73499</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-01-31T21:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: reading pipe delimited file with multiple record layouts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328944#M73521</link>
      <description>&lt;P&gt;You can read that data dictionary into a SAS dataset and use it to write the SAS program. You will need to convert descriptive names into valid SAS variable names. You will need to convert the data types into SAS variable types and at least for the date variables also FORMAT and INFORMAT settings. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also to make it easier you might want to just write a separate data step for each record type, rather than doing it all in one data step.&amp;nbsp;The program will be easier to write and understand. And unless you have a hugh data file it shouldn't take long to read it multiple times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ra ;
   infile 'myfile.txt' dsd dlm='|' truncover ;
   input record_type $ @ ;
   if record_type='RA';
   .....
run;

data rs ;
   infile 'myfile.txt' dsd dlm='|' truncover ;
   input record_type $ @ ;
   if record_type='RS';
   .....
run;

....&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Feb 2017 00:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/328944#M73521</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-02-01T00:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: reading pipe delimited file with multiple record layouts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/330477#M74148</link>
      <description>&lt;P&gt;Thank you. I was able to successufly read the files in with your help.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2017 14:38:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reading-pipe-delimited-file-with-multiple-record-layouts/m-p/330477#M74148</guid>
      <dc:creator>lslockle</dc:creator>
      <dc:date>2017-02-07T14:38:16Z</dc:date>
    </item>
  </channel>
</rss>

