DATA Step, Macro, Functions and more

Reading CSV file with embedded double quotes, comma & CRLF(multiple lines)

Reply
Occasional Contributor
Posts: 6

Reading CSV file with embedded double quotes, comma & CRLF(multiple lines)

I'm having trouble in importing a csv with embedded double quotes, comma & CRLF.

The file was generated by source system and able to import into Microsoft Access without any problem.

 

But when I attempt to import into SAS, the output is not correct.

 

Sample File Content (4 varables & 3 observations:

a,b,c,d<CRLF>

"a1<CRLF>
2<CRLF>
3<CRLF>
4",,c,d<CRLF>
"aaa a a,""<CRLF>
a a <CRLF>
a<CRLF>
a","bb""bb"", b .<CRLF>
b<CRLF>
b<CRLF>
b<CRLF>
b<CRLF>
b<CRLF>
b<CRLF>
b",c,d

 

My program:

DATA test;
INFILE 'D:\sas_data\test.csv'
LRECL=32767
ENCODING="UTF-8"
DLM=','
DSD termstr=CRLF;
INPUT
col1 : $CHAR100.
col2 : $CHAR100.
col3 : $CHAR100.
col4 : $CHAR100.
@@;
RUN;

 

Anyone have any suggestion for resolving this issue?

Super User
Posts: 7,863

Re: Reading CSV file with embedded double quotes, comma & CRLF(multiple lines)

In my experience, SAS can't really deal with linebreaks in quoted strings. The linebreak overrides the quotes. This might be an issue how the operating system treats files through the "read next record" system call.

 

My solution has been to write a C program that replaces linebreaks with a <BR> tag at places where the count of quotes is uneven.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,046

Re: Reading CSV file with embedded double quotes, comma & CRLF(multiple lines)

Can you attach a TEXT file to let us test the code.



Occasional Contributor
Posts: 6

Re: Reading CSV file with embedded double quotes, comma & CRLF(multiple lines)

Hi All,

 

Thank you very much for your reply. I had attached the file into this post.

The "test.csv" is a sample file that I created to simulate the problem.

 

The original file is about 900MB and it is automatically generated by system in UNIX and FTP to Windows file server in ascii mode (LF converted to CRLF).

There is one column with multiple lines free text format (10000 characters and this is the problematic part).

 

the file has been used in Microsoft Access without any problem for the past 2 years.

 

Challanges faced:

  1. There is no differenciation of LF and CRLF pattern similar to Excel generated CSV. If the multiple lines are generated by Microsoft Excel, we can use termstr=CRLF. But in this case, all New Lines are end with CRLF.
  2. There will be embedded comma, double quotation marks as well.

 

Super User
Posts: 10,046

Re: Reading CSV file with embedded double quotes, comma & CRLF(multiple lines)

That is way too messed up.
How do I know 

"aaa a a,""
a a 
a
a",

belong to A
while

"aaa a a,""
a a 
a
a","bb""bb"", 

not belong to A ?


Occasional Contributor
Posts: 6

Re: Reading CSV file with embedded double quotes, comma & CRLF(multiple lines)

 

yes, it look way too messed up but surprisingly, Microsoft Access is able to handle it well and it has been used for years before I attempt to import into SAS.

 

you may look at the attached screenshot for how it arranged in Microsoft Access.


Capture.PNG
Super User
Posts: 10,046

Re: Reading CSV file with embedded double quotes, comma & CRLF(multiple lines)


data x;
infile '/folders/myfolders/test.txt' termstr=lf;
input x $1. @@;
run;
data x;
n=0;
do until(last.x);
 set x;
 by x notsorted;
 n+1;
end;

do until(last.x);
 set x;
 by x notsorted;
 if x='"' then count=n;
 output;
end;
drop n;
run;


data x;
 set x;
 mod=mod(count,2);
run;
data x;
 set x;
 array xx{0:1} $ 4 _temporary_ ('off' 'on');
 length flag $ 4;
 retain flag 'off';
 by x notsorted;
 if last.x and mod=1 then do;
  n+1; flag=xx{mod(n,2)};
  group+1;
 end;
 if flag='off' and x=',' then group+1;
drop n;
run;
data temp;
length value $ 200;
 do until(last.group);
  set x;
  by group notsorted;
  value=cats(value,x);
 end;
 value=prxchange('s/^[",]+//',1,value);
 if value not in (' ' '0D'x) then output;
run;
data temp;
 set temp;
 if mod(_n_,4)=1 then obs+1;
run;
proc transpose data=temp out=want(drop=_name_);
by obs;
var value;
run;

x.png
Ask a Question
Discussion stats
  • 6 replies
  • 346 views
  • 0 likes
  • 3 in conversation