<?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 importing text (txt) file with delimiter using data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/916338#M360947</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I want to import a text file data into SAS. The file contain some issues that you will see in my example.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Some of the data is missing&amp;nbsp; and the data is not well organized as you can see. Student_id and d_pin are character variable. I would like to learn how to use either infile or proc import or both to import the data CORRECTLY.&lt;/P&gt;&lt;P&gt;Here is sample code. I have over 10,000 records.&lt;/P&gt;&lt;P&gt;Student_ID|D_PIN|SERVICE_DATE&lt;BR /&gt;|1624413098|11/08/2023&lt;BR /&gt;|1628543409|11/08/2023&lt;BR /&gt;001020084|1615413808|10/12/2023&lt;BR /&gt;001021665|1616220906|03/31/2023&lt;/P&gt;&lt;P&gt;002781314|1462647405|07/19/2023&lt;BR /&gt;|1331118504|11/02/2023&lt;BR /&gt;|1611325610|12/26/2023&lt;/P&gt;&lt;P&gt;080504901||11/08/2023&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data STUDENT; *;
infile 'c:\dog\dummydata.txt' truncover delimiter = '|' firstobs=2;
format SERVICE_DATE mmddyy10.;
input Student_ID $ 1-9 D_PIN $ 11-20 SERVICE_DATE :mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am getting many of the notes below. When I checked it seems it's because the original text file is not well arranged.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: Invalid data for SERVICE_DATE in line 1756 21-22.&lt;BR /&gt;RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0&lt;BR /&gt;1756 |1331118504|11/02/2023 22&lt;BR /&gt;SERVICE_DATE=. Student_ID=|13311185 &lt;SPAN&gt;D_PIN&lt;/SPAN&gt;=4|11/02/20 _ERROR_=1 _N_=1755&lt;BR /&gt;NOTE: Invalid data for SERVICE_DATE in line 1757 21-22.&lt;BR /&gt;1757 |1611325610|12/26/2023 22&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 15 Feb 2024 17:45:45 GMT</pubDate>
    <dc:creator>CathyVI</dc:creator>
    <dc:date>2024-02-15T17:45:45Z</dc:date>
    <item>
      <title>importing text (txt) file with delimiter using data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/916338#M360947</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I want to import a text file data into SAS. The file contain some issues that you will see in my example.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Some of the data is missing&amp;nbsp; and the data is not well organized as you can see. Student_id and d_pin are character variable. I would like to learn how to use either infile or proc import or both to import the data CORRECTLY.&lt;/P&gt;&lt;P&gt;Here is sample code. I have over 10,000 records.&lt;/P&gt;&lt;P&gt;Student_ID|D_PIN|SERVICE_DATE&lt;BR /&gt;|1624413098|11/08/2023&lt;BR /&gt;|1628543409|11/08/2023&lt;BR /&gt;001020084|1615413808|10/12/2023&lt;BR /&gt;001021665|1616220906|03/31/2023&lt;/P&gt;&lt;P&gt;002781314|1462647405|07/19/2023&lt;BR /&gt;|1331118504|11/02/2023&lt;BR /&gt;|1611325610|12/26/2023&lt;/P&gt;&lt;P&gt;080504901||11/08/2023&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data STUDENT; *;
infile 'c:\dog\dummydata.txt' truncover delimiter = '|' firstobs=2;
format SERVICE_DATE mmddyy10.;
input Student_ID $ 1-9 D_PIN $ 11-20 SERVICE_DATE :mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am getting many of the notes below. When I checked it seems it's because the original text file is not well arranged.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: Invalid data for SERVICE_DATE in line 1756 21-22.&lt;BR /&gt;RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0&lt;BR /&gt;1756 |1331118504|11/02/2023 22&lt;BR /&gt;SERVICE_DATE=. Student_ID=|13311185 &lt;SPAN&gt;D_PIN&lt;/SPAN&gt;=4|11/02/20 _ERROR_=1 _N_=1755&lt;BR /&gt;NOTE: Invalid data for SERVICE_DATE in line 1757 21-22.&lt;BR /&gt;1757 |1611325610|12/26/2023 22&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2024 17:45:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/916338#M360947</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2024-02-15T17:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: importing text (txt) file with delimiter using data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/916339#M360948</link>
      <description>&lt;P&gt;I presume the first two data lines in your sample actually have no student_id values, yes?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have to make a couple of changes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Include the "DSD" option on the INFILE statement.&amp;nbsp; DSD (for delimiter sensitive data) will allow the INPUT statement to assume a missing value for a text field with a length of 0 (e.g. for student_id in your first two obs).&lt;/LI&gt;
&lt;LI&gt;But specifying columns in your INPUT statement counter-productively overrides the DSD option.&amp;nbsp; One option is to use an INFORMAT statement as I have done in the code below.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data STUDENT; 
 * infile 'c:\dog\dummydata.txt' truncover delimiter = '|' firstobs=2;
  infile datalines delimiter = '|' dsd firstobs=2;
  informat student_id $9.  D_PIN $10.  service_date mmddyy10. ;
  format SERVICE_DATE mmddyy10.;
 * input Student_ID $ 1-9 D_PIN $ 11-20 SERVICE_DATE :mmddyy10.;
  input Student_ID D_PIN SERVICE_DATE ;
datalines;
Student_ID|D_PIN|SERVICE_DATE
|1624413098|11/08/2023
|1628543409|11/08/2023
001020084|1615413808|10/12/2023
001021665|1616220906|03/31/2023
002781314|1462647405|07/19/2023
|1331118504|11/02/2023
|1611325610|12/26/2023
080504901||11/08/2023
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've preceded each of my additions with your analogous statement, commented out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice the INPUT statement merely lists variable names, not locations, which means INPUT will utilize expected formats in the INFORMAT statement.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2024 18:13:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/916339#M360948</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-02-15T18:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: importing text (txt) file with delimiter using data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/916349#M360949</link>
      <description>&lt;P&gt;File looks well arranged to me.&amp;nbsp; You have a DELIMITED file.&amp;nbsp; So tell SAS that in the INFILE statement by adding DSD option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's convert your example lines into an actual file so we have something we can test agains.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options parmcards=text;
filename text temp;

parmcards;
Student_ID|D_PIN|SERVICE_DATE
|1624413098|11/08/2023
|1628543409|11/08/2023
001020084|1615413808|10/12/2023
001021665|1616220906|03/31/2023
002781314|1462647405|07/19/2023
|1331118504|11/02/2023
|1611325610|12/26/2023
080504901||11/08/2023
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we can start writing a data step to read the file.&amp;nbsp; First is the DATA statement to name the dataset that will be created.&amp;nbsp; Then the INFILE statement to tell where to find the data to be READ .&amp;nbsp; Then define the variables.&amp;nbsp; Then attach INFORMAT to any variables that NEED them.&amp;nbsp; And attach FORMAT to any variables that NEED them. Then INPUT the variables, if you have defined the variables in the order they appear in the source file then the INPUT statement can just use a simple positional variable list.&amp;nbsp; Then end the step with a RUN (or if you are using in-line data a CARDS or DATALINES block).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile text dsd dlm='|' truncover firstobs=2;
  length Student_ID $10 D_PIN $10 SERVICE_DATE 8;
  informat SERVICE_DATE mmddyy.;
  format SERVICE_DATE yymmdd10.;
  input Student_ID -- SERVICE_DATE ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1708025225481.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93790i312F4BA3D6721F92/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1708025225481.png" alt="Tom_0-1708025225481.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HInts.&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Putting the FORMAT statement before you have defined the variables (or let the INPUT statement define them by implying a type based on the usage) will cause the variables to be in a different order in the dataset.&amp;nbsp; That is just going to make looking at your dataset more cumbersome.&lt;/LI&gt;
&lt;LI&gt;Displaying dates in either MDY or DMY order will confuse half of your audience. Use DATE or YYMMDD format to display dates to avoid confusion.&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Thu, 15 Feb 2024 19:32:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/916349#M360949</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-15T19:32:13Z</dc:date>
    </item>
    <item>
      <title>Re: importing text (txt) file with delimiter using data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/917410#M361388</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; Thank you. I found something interesting this morning. I noticed there was a particular row in the txt file I imported with only the service date. e.g.,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Student_ID|D_PIN|SERVICE_DATE&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|12/04/2023&lt;/P&gt;&lt;P&gt;The student_ID and D_PIN are both blank in the text file. I just noticed that this line did not output correctly with the codes provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what the output looks like. The date is displayed on the D_PIN column&lt;/P&gt;&lt;DIV&gt;&lt;SPAN&gt;Student_ID&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt; &amp;nbsp; &amp;nbsp;D_PIN&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SERVICE_DATE&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12/04/2023&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;How can I resolved this, Thanks in advance.&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 22 Feb 2024 17:31:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/917410#M361388</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2024-02-22T17:31:25Z</dc:date>
    </item>
    <item>
      <title>Re: importing text (txt) file with delimiter using data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/917413#M361389</link>
      <description>&lt;P&gt;The problem with that line is it only has TWO values instead of THREE because there is only one DELIMITER.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look carefully at the whole file with a text editor that displays text using a FIXED width font.&amp;nbsp; Do the values of SERVICE_DATE always start in the same column?&amp;nbsp; If so you can read the file as&amp;nbsp;FIXED columns instead of as DELIMITED.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If instead there are not extra spaces to pad the short or missing values so that D_PIN and SERVICE_DATE always start in the same place you will have to treat it as delimited.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If just a few have this issue of missing delimiters and they all follow this same pattern then you could possible adapt the code to test how many delimiters there are and read the line differently depending on the test.&lt;/P&gt;
&lt;P&gt;Something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile 'myfile.txt' dsd dlm='|' truncover;
  length Student_ID D_PIN $10 SERVICE_DATE 8;
  input @;
  if countc(_infile_,'|') = 2 then input Student_ID :$10. D_PIN :$10. SERVICE_DATE :mmddyy.;
  else input Student_ID SERVICE_DATE :mmddyy.;
  format service_date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 17:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-text-txt-file-with-delimiter-using-data/m-p/917413#M361389</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-22T17:43:00Z</dc:date>
    </item>
  </channel>
</rss>

