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?
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.
Can you attach a TEXT file to let us test the code.
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:
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 ?
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.