<?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 text file with extra delimiter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874581#M345558</link>
    <description>Thank you</description>
    <pubDate>Mon, 08 May 2023 23:12:10 GMT</pubDate>
    <dc:creator>kuppusamy</dc:creator>
    <dc:date>2023-05-08T23:12:10Z</dc:date>
    <item>
      <title>Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874549#M345535</link>
      <description>&lt;P&gt;I have data in below format as text file&lt;/P&gt;&lt;P&gt;MSGID|MNEMONIC|LINENO|LEVEL|TEXT|PBUTTONS&lt;BR /&gt;1036|CL_DOWN|1|O|Down|SASHELP.FSP.OK.SLIST&lt;BR /&gt;1037|CL_UP||1|O|Up|SASHELP.FSP.OK.SLIST&lt;BR /&gt;1038||CL_LEFT|1|O|Left|SASHELP.FSP.OK.SLIST&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here, first record is correct, in second and third record there is an extra pipe symbol(after second and first column), this is data error. but i need to solve this issue in SAS while importing. please anybody help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i need output like below&lt;/P&gt;&lt;P&gt;MSGID|MNEMONIC|LINENO|LEVEL|TEXT|PBUTTONS&lt;BR /&gt;1036|CL_DOWN|1|O|Down|SASHELP.FSP.OK.SLIST&lt;BR /&gt;1037|CL_UP|1|O|Up|SASHELP.FSP.OK.SLIST&lt;BR /&gt;1038|CL_LEFT|1|O|Left|SASHELP.FSP.OK.SLIST&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 20:50:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874549#M345535</guid>
      <dc:creator>kuppusamy</dc:creator>
      <dc:date>2023-05-08T20:50:38Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874552#M345537</link>
      <description>Can you show the code you're currently using to read the file?&lt;BR /&gt;</description>
      <pubDate>Mon, 08 May 2023 20:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874552#M345537</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-05-08T20:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874554#M345538</link>
      <description>&lt;P&gt;File import code,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;/* --------------------------------------------------------------------&lt;BR /&gt;Code generated by a SAS task&lt;BR /&gt;&lt;BR /&gt;Generated on Monday, May 8, 2023 at 3:20:00 PM&lt;BR /&gt;By task: Import Data Wizard&lt;BR /&gt;&lt;BR /&gt;Source file: C:\Users\PL4K760\Documents\SAMPLE.txt&lt;BR /&gt;Server: Local File System&lt;BR /&gt;&lt;BR /&gt;Output data: WORK.SAMPLE_0000&lt;BR /&gt;Server: SASApp&lt;BR /&gt;&lt;BR /&gt;Note: In preparation for running the following code, the Import&lt;BR /&gt;Data wizard has used internal routines to transfer the source data&lt;BR /&gt;file from the local file system to SASApp. There is no SAS code&lt;BR /&gt;available to represent this action.&lt;BR /&gt;-------------------------------------------------------------------- */&lt;/P&gt;&lt;P&gt;DATA WORK.SAMPLE_0000;&lt;BR /&gt;LENGTH&lt;BR /&gt;MSGID 8&lt;BR /&gt;MNEMONIC $ 28&lt;BR /&gt;LINENO $ 7&lt;BR /&gt;LEVEL $ 1&lt;BR /&gt;TEXT $ 119&lt;BR /&gt;PBUTTONS $ 20&lt;BR /&gt;F7 $ 20 ;&lt;BR /&gt;FORMAT&lt;BR /&gt;MSGID BEST4.&lt;BR /&gt;MNEMONIC $CHAR28.&lt;BR /&gt;LINENO $CHAR7.&lt;BR /&gt;LEVEL $CHAR1.&lt;BR /&gt;TEXT $CHAR119.&lt;BR /&gt;PBUTTONS $CHAR20.&lt;BR /&gt;F7 $CHAR20. ;&lt;BR /&gt;INFORMAT&lt;BR /&gt;MSGID BEST4.&lt;BR /&gt;MNEMONIC $CHAR28.&lt;BR /&gt;LINENO $CHAR7.&lt;BR /&gt;LEVEL $CHAR1.&lt;BR /&gt;TEXT $CHAR119.&lt;BR /&gt;PBUTTONS $CHAR20.&lt;BR /&gt;F7 $CHAR20. ;&lt;BR /&gt;INFILE '/saswork1/SAS_work28BB00001BF6_prodsascom5l/#LN00048'&lt;BR /&gt;LRECL=156&lt;BR /&gt;ENCODING="LATIN1"&lt;BR /&gt;TERMSTR=CRLF&lt;BR /&gt;DLM='7F'x&lt;BR /&gt;MISSOVER&lt;BR /&gt;DSD ;&lt;BR /&gt;INPUT&lt;BR /&gt;MSGID : ?? BEST4.&lt;BR /&gt;MNEMONIC : $CHAR28.&lt;BR /&gt;LINENO : $CHAR7.&lt;BR /&gt;LEVEL : $CHAR1.&lt;BR /&gt;TEXT : $CHAR119.&lt;BR /&gt;PBUTTONS : $CHAR20.&lt;BR /&gt;F7 : $CHAR20. ;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output im getting is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kuppusamy_0-1683579844421.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83730iEA713CB23F93AF3E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kuppusamy_0-1683579844421.png" alt="kuppusamy_0-1683579844421.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;here 1, 2,4 and 5th record is bad, i need record like 3rd&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 21:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874554#M345538</guid>
      <dc:creator>kuppusamy</dc:creator>
      <dc:date>2023-05-08T21:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874560#M345543</link>
      <description>Copy the code from the log and use that. Remove the DSD option and change MISSOVER to TRUNCOVER.</description>
      <pubDate>Mon, 08 May 2023 21:40:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874560#M345543</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-05-08T21:40:44Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874562#M345545</link>
      <description>Its not worked, because of extra delimiter value is shifting to next column&lt;BR /&gt;</description>
      <pubDate>Mon, 08 May 2023 21:45:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874562#M345545</guid>
      <dc:creator>kuppusamy</dc:creator>
      <dc:date>2023-05-08T21:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874564#M345546</link>
      <description>&lt;P&gt;As long as the extra delimiters are just appearing at the end (or beginning) of a field you can just remove the DSD option and SAS will treat the adjacent delimiters as one delimiter.&amp;nbsp; Just like it treats spaces in normal LIST MODE input.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's get your lines of text into a file we can use to play with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options parmcards=csv;
filename csv temp;
parmcards4;
MSGID|MNEMONIC|LINENO|LEVEL|TEXT|PBUTTONS
1036|CL_DOWN|1|O|Down|SASHELP.FSP.OK.SLIST
1037|CL_UP||1|O|Up|SASHELP.FSP.OK.SLIST
1038||CL_LEFT|1|O|Left|SASHELP.FSP.OK.SLIST
;;;;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your IMPORT task wrote some really ugly SAS code.&lt;/P&gt;
&lt;P&gt;Do you know if it also made that ugly text file?&amp;nbsp; Is the original file a text file or did the import task make the text file for you?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Write your own data step and the code can be much simpler and clearer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use your knowledge of what the variables are to define them properly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example that last one looks like a four level SAS object name.&amp;nbsp; &amp;nbsp;Since a LIBREF can only be 8 character and the maximum length for any other name is 32 characters&amp;nbsp;the maximum length it could be is 8+32*3+3 = 107.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first one looks like an ID variable so it should be CHARACTER since arithmetic on an ID has no meaning.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LINENO is probably a number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your data step could look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile csv dlm='|' truncover firstobs=2;
  length MSGID $8 MNEMONIC $28 LINENO 8 LEVEL $1 TEXT $10 PBUTTONS $107 ;
  input msgid -- pbuttons;
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-1683583175335.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83731i630790B4ADE74111/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1683583175335.png" alt="Tom_0-1683583175335.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 22:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874564#M345546</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-08T22:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874566#M345548</link>
      <description>&lt;P&gt;That's odd, seemed to work for me.&lt;/P&gt;
&lt;P&gt;Tom's answer is better though &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.SAMPLE_0000;
    LENGTH MSGID 8 MNEMONIC $ 28 LINENO $ 7 LEVEL $ 1 TEXT $ 119 PBUTTONS $ 20 
        F7 $ 20;
    FORMAT MSGID BEST4.
MNEMONIC $CHAR28.
LINENO $CHAR7.
LEVEL $CHAR1.
TEXT $CHAR119.
PBUTTONS $CHAR20.
F7 $CHAR20.;
    INFORMAT MSGID BEST4.
MNEMONIC $CHAR28.
LINENO $CHAR7.
LEVEL $CHAR1.
TEXT $CHAR119.
PBUTTONS $CHAR20.
F7 $CHAR20.;
    INFILE cards DLM='|' TRUNCOVER;
    INPUT MSGID 
MNEMONIC 
LINENO 
LEVEL 
TEXT 
PBUTTONS 
F7 ;
    cards;
1036|CL_DOWN|1|O|Down|SASHELP.FSP.OK.SLIST
1037|CL_UP||1|O|Up|SASHELP.FSP.OK.SLIST
1038||CL_LEFT|1|O|Left|SASHELP.FSP.OK.SLIST
;
    ;
    ;;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 May 2023 22:10:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874566#M345548</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-05-08T22:10:10Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874570#M345550</link>
      <description>It is working, thank you&lt;BR /&gt;I have records like&lt;BR /&gt;1036|CL_DOWN||||SASHELP.FSP.OK.SLIST&lt;BR /&gt;continuous missing values also there ,&lt;BR /&gt;in this case what should i do ? please help</description>
      <pubDate>Mon, 08 May 2023 22:25:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874570#M345550</guid>
      <dc:creator>kuppusamy</dc:creator>
      <dc:date>2023-05-08T22:25:58Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874571#M345551</link>
      <description>I have records like&lt;BR /&gt;1036|CL_DOWN||||SASHELP.FSP.OK.SLIST&lt;BR /&gt;continuous missing values also there ,&lt;BR /&gt;in this case what should i do ? please help</description>
      <pubDate>Mon, 08 May 2023 22:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874571#M345551</guid>
      <dc:creator>kuppusamy</dc:creator>
      <dc:date>2023-05-08T22:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874574#M345554</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/99537"&gt;@kuppusamy&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I have records like&lt;BR /&gt;1036|CL_DOWN||||SASHELP.FSP.OK.SLIST&lt;BR /&gt;continuous missing values also there ,&lt;BR /&gt;in this case what should i do ? please help&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Find the author of the file and get them to re-create it in a usable format.&lt;/P&gt;
&lt;P&gt;The values that contain delimiters should have been in quotes, then the DSD option would work properly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1037|"CL_UP|"|1|O|Up|SASHELP.FSP.OK.SLIST
1038|"|CL_LEFT"|1|O|Left|SASHELP.FSP.OK.SLIST&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Without that the file is not parsable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Personally I would just fix it by hand.&amp;nbsp;&lt;/STRONG&gt;You are lucky in this case that the file only has a few variables.&lt;/P&gt;
&lt;P&gt;You could use a program to split the records into those with the right number of | and those with the wrong number.&amp;nbsp; Then you only have to edit the smaller number of corrupt lines.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename csv 'have.txt';
filename good 'good.txt';
filename bag 'bad.txt';
data _null_;
  infile csv ;
  input;
  if _n_=1 or countc(_infile_,'|')=5 then do;
     file good;
     put _infile_;
  end;
  if _n_=1 or countc(_infile_,'|') ne 5 then do;
     file bad;
     put _infile_;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;6774   data _null_;
6775     infile bad;
6776     input;
6777     put _infile_;
6778   run;

NOTE: The infile BAD is:
      (system-specific pathname),
      (system-specific file attributes)

MSGID|MNEMONIC|LINENO|LEVEL|TEXT|PBUTTONS
1037|CL_UP||1|O|Up|SASHELP.FSP.OK.SLIST
1038||CL_LEFT|1|O|Left|SASHELP.FSP.OK.SLIST
NOTE: 3 records were read from the infile (system-specific pathname).
      The minimum record length was 39.
      The maximum record length was 43.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


6779
6780   data _null_;
6781     infile good;
6782     input;
6783     put _infile_;
6784   run;

NOTE: The infile GOOD is:
      (system-specific pathname),
      (system-specific file attributes)

MSGID|MNEMONIC|LINENO|LEVEL|TEXT|PBUTTONS
1036|CL_DOWN|1|O|Down|SASHELP.FSP.OK.SLIST
1036|CL_DOWN||||SASHELP.FSP.OK.SLIST
NOTE: 3 records were read from the infile (system-specific pathname).
      The minimum record length was 36.
      The maximum record length was 42.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 22:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874574#M345554</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-08T22:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874579#M345557</link>
      <description>&lt;P&gt;If the extra | only occurs in the second field then perhaps you could do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options parmcards=csv;
filename csv temp;
parmcards4;
MSGID|MNEMONIC|LINENO|LEVEL|TEXT|PBUTTONS
1036|CL_DOWN|1|O|Down|SASHELP.FSP.OK.SLIST
1037|CL_UP||1|O|Up|SASHELP.FSP.OK.SLIST
1038||CL_LEFT|1|O|Left|SASHELP.FSP.OK.SLIST
1036|CL_DOWN||||SASHELP.FSP.OK.SLIST
;;;;

data xx;
  infile csv dsd dlm='|' truncover firstobs=2;
  length MSGID $8 MNEMONIC $28 LINENO 8 LEVEL $1 TEXT $10 PBUTTONS $107 ;
  length dummy $20 ;
  input msgid MNEMONIC @;
  if countc(_infile_,'|')&amp;gt;5 then do;
    input dummy @;
    MNEMONIC=cats(MNEMONIC,'|',dummy);
  end;
  input lineno -- pbuttons;
  drop dummy ;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1683587224428.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83734i5DDA6F440F05762F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1683587224428.png" alt="Tom_0-1683587224428.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 23:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874579#M345557</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-08T23:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: Import text file with extra delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874581#M345558</link>
      <description>Thank you</description>
      <pubDate>Mon, 08 May 2023 23:12:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-text-file-with-extra-delimiter/m-p/874581#M345558</guid>
      <dc:creator>kuppusamy</dc:creator>
      <dc:date>2023-05-08T23:12:10Z</dc:date>
    </item>
  </channel>
</rss>

