<?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: Split comma separated string on file read in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Split-comma-separated-string-on-file-read/m-p/407529#M99328</link>
    <description>&lt;P&gt;I didn't&amp;nbsp; know this before, but if you drop the DSD option on&amp;nbsp; INFILE, then the quoted comma-separated values will no longer be treated as a single value.&amp;nbsp; Which means that the first value ("A in data line 1) will be read in alone.&amp;nbsp; You can&amp;nbsp; then do a "DO WHILE" loop along the lines you contemplate:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;dm 'clear log';
data t ;
  infile datalines  missover dlm="," ;
  input (c1-c3) (:$10.) C4 :$8. @;

  do while (C4^=' ');
    c4=compress(c4,'"');
    output;
    input c4 :$14. @;
  end;
datalines;
Tab1,   DB1,    S1,        "A,B,C,D,E"
Tab2,   DB1,    S1,        "A,B,C"
Tab3,   DB2,    S1,        "D,E"
Tab4,   DB2,    S3,        "A,B,C,D,E,G"
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works fine as long as you don't have consecutive commas in your raw data as a way to indicate a missing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 26 Oct 2017 03:25:00 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-10-26T03:25:00Z</dc:date>
    <item>
      <title>Split comma separated string on file read</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-comma-separated-string-on-file-read/m-p/407521#M99326</link>
      <description>&lt;P&gt;I have a CSV file that contains a field that is a comma separated list. An example would be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;C1      C2      C3        C4
Tab1   DB1    S1        "A,B,C,D,E"
Tab2   DB1    S1        "A,B,C"
Tab3   DB2    S1        "D,E"
Tab4   DB2    S3        "A,B,C,D,E,G"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output desired would be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;C1         C2           C3          C4
Tab1      DB1        S1           A
Tab1      DB1        S1           B
Tab1      DB1        S1           C
Tab1      DB1        S1           D
Tab1      DB1        S1           E
Tab2      DB1        S1           A
Tab2      DB1        S1           B
Tab2      DB1        S1           C

etc...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am currently able to read the data into SAS with the 4 variables, and then split the C4 variable using SCAN function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I'm curious if this could be handled in the input data step, instead of doing it in two separate steps. Given the size of the input file being in the hundreds of millions of rows, only having to read through the data once would be very helpful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried a few variations of the construct..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data t;
  infile "naming_test.csv" missover dlm="," dsd;
  length NAME $ 50;
  input NAME $ @ ;
  var = 0;
  do while (name ne ' ');
    var+1; /* keep track of a counter for testing */
    output;
    input name $ @;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, I haven't been able to crack it. Thanks for your help!&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 02:30:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-comma-separated-string-on-file-read/m-p/407521#M99326</guid>
      <dc:creator>JoshB</dc:creator>
      <dc:date>2017-10-26T02:30:19Z</dc:date>
    </item>
    <item>
      <title>Re: Split comma separated string on file read</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-comma-separated-string-on-file-read/m-p/407527#M99327</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  file "%sysfunc(pathname(WORK))\t.txt";
  put 'C1    ,  C2  ,    C3    ,    C4           ';
  put 'Tab1  , DB1  ,  S1      ,  "A,B,C,D,E"    ';
  put 'Tab2  , DB1  ,  S1      ,  "A,B,C"        ';
  put 'Tab3  , DB2  ,  S1      ,  "D,E"          ';
  put 'Tab4  , DB2  ,  S3      ,  "A,B,C,D,E,G"  ';
run;
data WANT;
  infile "%sysfunc(pathname(WORK))\t.txt" dlm=',' dsd firstobs=2;
  input C1 $ C2 $ C3 $ C4 : $1.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;C1&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;C2&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;C3&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;C4&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Tab1&lt;/TD&gt;
&lt;TD class="l data"&gt;DB1&lt;/TD&gt;
&lt;TD class="l data"&gt;S1&lt;/TD&gt;
&lt;TD class="l data"&gt;A&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Tab2&lt;/TD&gt;
&lt;TD class="l data"&gt;DB1&lt;/TD&gt;
&lt;TD class="l data"&gt;S1&lt;/TD&gt;
&lt;TD class="l data"&gt;A&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Tab3&lt;/TD&gt;
&lt;TD class="l data"&gt;DB2&lt;/TD&gt;
&lt;TD class="l data"&gt;S1&lt;/TD&gt;
&lt;TD class="l data"&gt;D&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Tab4&lt;/TD&gt;
&lt;TD class="l data"&gt;DB2&lt;/TD&gt;
&lt;TD class="l data"&gt;S3&lt;/TD&gt;
&lt;TD class="l data"&gt;A&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 03:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-comma-separated-string-on-file-read/m-p/407527#M99327</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-10-26T03:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Split comma separated string on file read</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-comma-separated-string-on-file-read/m-p/407529#M99328</link>
      <description>&lt;P&gt;I didn't&amp;nbsp; know this before, but if you drop the DSD option on&amp;nbsp; INFILE, then the quoted comma-separated values will no longer be treated as a single value.&amp;nbsp; Which means that the first value ("A in data line 1) will be read in alone.&amp;nbsp; You can&amp;nbsp; then do a "DO WHILE" loop along the lines you contemplate:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;dm 'clear log';
data t ;
  infile datalines  missover dlm="," ;
  input (c1-c3) (:$10.) C4 :$8. @;

  do while (C4^=' ');
    c4=compress(c4,'"');
    output;
    input c4 :$14. @;
  end;
datalines;
Tab1,   DB1,    S1,        "A,B,C,D,E"
Tab2,   DB1,    S1,        "A,B,C"
Tab3,   DB2,    S1,        "D,E"
Tab4,   DB2,    S3,        "A,B,C,D,E,G"
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works fine as long as you don't have consecutive commas in your raw data as a way to indicate a missing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 03:25:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-comma-separated-string-on-file-read/m-p/407529#M99328</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-26T03:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: Split comma separated string on file read</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-comma-separated-string-on-file-read/m-p/407563#M99335</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t;
infile "...." dlm="," dsd;
input c1 $ c2 $ c3 $ _c4 :$20.;
length c4 $1;
do i = 1 to countw(_c4,',');
  c4 = scan(_c4,i,',');
  output;
end;
drop i _c4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you see you can do it in the input data step already.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 07:27:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-comma-separated-string-on-file-read/m-p/407563#M99335</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-10-26T07:27:00Z</dc:date>
    </item>
  </channel>
</rss>

