<?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 trouble in reading CSV file in SAS, comma within a field in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/trouble-in-reading-CSV-file-in-SAS-comma-within-a-field/m-p/873963#M345259</link>
    <description>&lt;P&gt;Hi, I have trouble in reading a csv file in SAS, because comma exists in a string, which should be one column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The CSV file is like below:&lt;/P&gt;
&lt;PRE&gt;id,x1,x2,text,dt,x3,x4,x5,x6
101,"","","VISTOR said, \"good."\",2022-11-03,0,0,x,y
310380,2,Inf,"Flu, but not get it yet, but said \"don't want it\". Education provided.",2022-11-04,"","",0,0&lt;/PRE&gt;
&lt;P&gt;My code:&lt;/P&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data&amp;nbsp; aa;
infile&amp;nbsp;
"c:\test.csv"&amp;nbsp;
MISSOVER DSD lrecl=40000 firstobs=2 TERMSTR=CRLF delimiter= ',' ;
informat id $20. ;
informat x1 $10. ;
informat x2 $10. ;
informat text $300. ;
informat dt $10. ;
informat x3 $2. ;
informat x4 $2. ;
informat x5 $2.;
informat x6 $2.;

format id $20. ;
format x1 $10.&amp;nbsp; ;
format x2 $10. ;
format text $300. ;
format dt $10. ;
format x3 $2. ;
format x4 $2. ;
format x5 $2. ;
format x6 $2.;

input
id&amp;nbsp; $
x1 $&amp;nbsp;
x2&amp;nbsp; $
text&amp;nbsp; $
dt $&amp;nbsp;
x3 $&amp;nbsp;
x4 $&amp;nbsp;
x5 $&amp;nbsp;
x6 $
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Then the output is messed up and like :&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="wwwz_0-1683224371295.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83630i640F73AB8F2CF195/image-size/medium?v=v2&amp;amp;px=400" role="button" title="wwwz_0-1683224371295.png" alt="wwwz_0-1683224371295.png" /&gt;&lt;/span&gt;
&lt;P&gt;How do I fix this?&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;/DIV&gt;</description>
    <pubDate>Thu, 04 May 2023 19:15:36 GMT</pubDate>
    <dc:creator>wwwz</dc:creator>
    <dc:date>2023-05-04T19:15:36Z</dc:date>
    <item>
      <title>trouble in reading CSV file in SAS, comma within a field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/trouble-in-reading-CSV-file-in-SAS-comma-within-a-field/m-p/873963#M345259</link>
      <description>&lt;P&gt;Hi, I have trouble in reading a csv file in SAS, because comma exists in a string, which should be one column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The CSV file is like below:&lt;/P&gt;
&lt;PRE&gt;id,x1,x2,text,dt,x3,x4,x5,x6
101,"","","VISTOR said, \"good."\",2022-11-03,0,0,x,y
310380,2,Inf,"Flu, but not get it yet, but said \"don't want it\". Education provided.",2022-11-04,"","",0,0&lt;/PRE&gt;
&lt;P&gt;My code:&lt;/P&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data&amp;nbsp; aa;
infile&amp;nbsp;
"c:\test.csv"&amp;nbsp;
MISSOVER DSD lrecl=40000 firstobs=2 TERMSTR=CRLF delimiter= ',' ;
informat id $20. ;
informat x1 $10. ;
informat x2 $10. ;
informat text $300. ;
informat dt $10. ;
informat x3 $2. ;
informat x4 $2. ;
informat x5 $2.;
informat x6 $2.;

format id $20. ;
format x1 $10.&amp;nbsp; ;
format x2 $10. ;
format text $300. ;
format dt $10. ;
format x3 $2. ;
format x4 $2. ;
format x5 $2. ;
format x6 $2.;

input
id&amp;nbsp; $
x1 $&amp;nbsp;
x2&amp;nbsp; $
text&amp;nbsp; $
dt $&amp;nbsp;
x3 $&amp;nbsp;
x4 $&amp;nbsp;
x5 $&amp;nbsp;
x6 $
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Then the output is messed up and like :&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="wwwz_0-1683224371295.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83630i640F73AB8F2CF195/image-size/medium?v=v2&amp;amp;px=400" role="button" title="wwwz_0-1683224371295.png" alt="wwwz_0-1683224371295.png" /&gt;&lt;/span&gt;
&lt;P&gt;How do I fix this?&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;/DIV&gt;</description>
      <pubDate>Thu, 04 May 2023 19:15:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/trouble-in-reading-CSV-file-in-SAS-comma-within-a-field/m-p/873963#M345259</guid>
      <dc:creator>wwwz</dc:creator>
      <dc:date>2023-05-04T19:15:36Z</dc:date>
    </item>
    <item>
      <title>Re: trouble in reading CSV file in SAS, comma within a field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/trouble-in-reading-CSV-file-in-SAS-comma-within-a-field/m-p/873976#M345264</link>
      <description>&lt;P&gt;Thing is that for SAS&amp;nbsp; (in contrary to C, R or Python) backslash is not an escape character so (\") does not escape "quotes inside quotes".&lt;/P&gt;
&lt;P&gt;For sas escape symbol id double doublequote ("")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Translate \" into "" and your file should read-in ok.&lt;/P&gt;
&lt;P&gt;Example how to translate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename T TEMP;

data  _null_;
infile "c:\test.csv" 
lrecl=40000 firstobs=2;
file T lrecl=40000 TERMSTR=CRLF;
input;
length x $ 32767; 
x=tranwrd(_infile_,'\"','""');
put x;
run;


data  aa;
infile 
T 
DSD lrecl=40000 /*firstobs=2*/ TERMSTR=CRLF delimiter= ',' MISSOVER;
informat id $20. ;
informat x1 $10. ;
informat x2 $10. ;
informat text $300. ;
informat dt $10. ;
informat x3 $2. ;
informat x4 $2. ;
informat x5 $2.;
informat x6 $2.;
 
format id $20. ;
format x1 $10.  ;
format x2 $10. ;
format text $300. ;
format dt $10. ;
format x3 $2. ;
format x4 $2. ;
format x5 $2. ;
format x6 $2.;

input
id  $
x1 $ 
x2  $
text  $
dt $ 
x3 $ 
x4 $ 
x5 $ 
x6 $
;

run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 18:45:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/trouble-in-reading-CSV-file-in-SAS-comma-within-a-field/m-p/873976#M345264</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-05-04T18:45:26Z</dc:date>
    </item>
    <item>
      <title>Re: trouble in reading CSV file in SAS, comma within a field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/trouble-in-reading-CSV-file-in-SAS-comma-within-a-field/m-p/873988#M345269</link>
      <description>&lt;P&gt;Fix the process that created the file so that makes a valid CSV file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to add quotes around values that include the delimiter.&amp;nbsp; That means that if the value also contains quotes you need to double those quotes so they do not cause the quoted string to end too soon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your text file should look this:&lt;/P&gt;
&lt;PRE&gt;id,x1,x2,text,dt,x3,x4,x5,x6
101,,,"VISTOR said, ""good.""",2022-11-03,0,0,x,y
310380,2,Inf,"Flu, but not get it yet, but said ""don't want it"". Education provided.",2022-11-04,,,0,0
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looks your file is something intended to be read by the Unix shell instead of an actual CSV file.&amp;nbsp; Unix shell is an example of system that uses the backslash character as an "ESCAPE" character instead of following the normal process for quoting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the lines in your file are short enough (less than 32K bytes) you can fix it on the fly by modifying the _INFILE_ buffer character before trying to read the fields.&amp;nbsp; &amp;nbsp;For example you could use TRANWRD() function to convert the \" combinations into "" instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's convert your posted lines into an actual file we can test with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename csv temp;
options parmcards=csv;
parmcards4;
id,x1,x2,text,dt,x3,x4,x5,x6
101,"","","VISTOR said, \"good."\",2022-11-03,0,0,x,y
310380,2,Inf,"Flu, but not get it yet, but said \"don't want it\". Education provided.",2022-11-04,"","",0,0
;;;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;For your file this simple data step will work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data aa;
  infile csv dsd truncover firstobs=2;
  length id $20 X1 X2 $10 text $300 dt 8 x3-x6 $2 ;
  informat dt yymmdd.;
  format dt yymmdd10.;
  input @;
  _infile_=tranwrd(_infile_,'\"','""');
  input id -- x6 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs   id       X1   X2    text                                                                             dt   x3   x4   x5   x6

 1    101                 VISTOR said, "good."                                                     2022-11-03   0    0    x    y
 2    310380   2    Inf   Flu, but not get it yet, but said "don't want it". Education provided.   2022-11-04             0    0
&lt;/PRE&gt;
&lt;P&gt;If you also have some literal backslash characters in the data you will need to use slightly more complicated logic to avoid making the mistake of converting&lt;FONT face="courier new,courier"&gt; \\"&lt;/FONT&gt; to&lt;FONT face="courier new,courier"&gt; \""&lt;/FONT&gt; instead of&lt;FONT face="courier new,courier"&gt; \"&lt;/FONT&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  _infile_=tranwrd(_infile_,'\\','00'x);
  _infile_=tranwrd(_infile_,'\"','""');
  _infile_=tranwrd(_infile_,'00'x,'\');
&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, 04 May 2023 19:34:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/trouble-in-reading-CSV-file-in-SAS-comma-within-a-field/m-p/873988#M345269</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-04T19:34:23Z</dc:date>
    </item>
    <item>
      <title>Re: trouble in reading CSV file in SAS, comma within a field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/trouble-in-reading-CSV-file-in-SAS-comma-within-a-field/m-p/874034#M345291</link>
      <description>Thank both, both are working well!!</description>
      <pubDate>Fri, 05 May 2023 01:23:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/trouble-in-reading-CSV-file-in-SAS-comma-within-a-field/m-p/874034#M345291</guid>
      <dc:creator>wwwz</dc:creator>
      <dc:date>2023-05-05T01:23:19Z</dc:date>
    </item>
  </channel>
</rss>

