BookmarkSubscribeRSS Feed
chinyong
Calcite | Level 5

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?

6 REPLIES 6
Kurt_Bremser
Super User

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.

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



chinyong
Calcite | Level 5

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.

 

Ksharp
Super User
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 ?


chinyong
Calcite | Level 5

 

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
Ksharp
Super User

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 4938 views
  • 0 likes
  • 3 in conversation