DATA Step, Macro, Functions and more

Reading a CSV having a custom delimiter

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 127
Accepted Solution

Reading a CSV having a custom delimiter

Hi all,

Would you know how I could read a CSV in which the delimiter as been set to a double pipe ('||') ? An empty value in this file would thus appear like 4 consecutive pipe ('||||'). We are still using the version 8 of SAS :smileylaugh:

I already know how to create such a file but not how to read it :smileyconfused:

Many thanks in advance for your help !

Kind regards,

Florent


Accepted Solutions
Solution
‎05-22-2012 11:35 AM
Valued Guide
Posts: 765

Re: Reading a CSV having a custom delimiter

hi ... I think that  _INFILE_ has been around since V8, so maybe ...

data x;

infile datalines dsd;

input @;

_infile_ = tranwrd(_infile_,'||',',');

input a b c;

datalines;

1||2||||

4||5||6

||8||||

;

a    b    c

1    2    .

4    5    6

.    8    .

ps ... some reading material ...

More _Infile_ Magic, Peter Crawford

http://www2.sas.com/proceedings/sugi28/086-28.pdf

Now _INFILE_ is an Automatic Variable − So What?, Howard Schreier,

http://www.nesug.org/proceedings/nesug01/cc/cc4018bw.pdf

View solution in original post


All Replies
Solution
‎05-22-2012 11:35 AM
Valued Guide
Posts: 765

Re: Reading a CSV having a custom delimiter

hi ... I think that  _INFILE_ has been around since V8, so maybe ...

data x;

infile datalines dsd;

input @;

_infile_ = tranwrd(_infile_,'||',',');

input a b c;

datalines;

1||2||||

4||5||6

||8||||

;

a    b    c

1    2    .

4    5    6

.    8    .

ps ... some reading material ...

More _Infile_ Magic, Peter Crawford

http://www2.sas.com/proceedings/sugi28/086-28.pdf

Now _INFILE_ is an Automatic Variable − So What?, Howard Schreier,

http://www.nesug.org/proceedings/nesug01/cc/cc4018bw.pdf

Frequent Contributor
Posts: 127

Re: Reading a CSV having a custom delimiter

Thanks, it makes the job ! Smiley Wink

Super User
Posts: 5,498

Re: Reading a CSV having a custom delimiter

Nice solution, but I'm not sure you should use dsd here.  The way I understood the data structure, a double delimiter indicates there is a blank variable.  If that's the case, dsd should become  dlm=','


Valued Guide
Posts: 765

Re: Reading a CSV having a custom delimiter

Posted in reply to Astounding

hi ... DSD implies a few things:  a comma-delimiter; two consecutive commas indicate a missing value; considers delimiters within quotes as character data and strips quotes from quoted values

DLM=',' only does the comma-delimter portion

my example doesn't work with just DLM

Super User
Posts: 5,498

Re: Reading a CSV having a custom delimiter

You're right, I'm temporarily brain-fried.  Sorry about that.

Respected Advisor
Posts: 4,919

Re: Reading a CSV having a custom delimiter

Hi, My concern would be that a file separated this way might contain commas in numeric or character fields.  It might be safer to keep the pipe character as in:

data x;

infile datalines delimiter='|' dsd;

input @;

_infile_ = tranwrd(_infile_,'||','|');

input (a b c) (:$);

datalines;

1||2,5||||

4||5||6

||8||||

;

PG

PG
Respected Advisor
Posts: 3,799

Re: Reading a CSV having a custom delimiter

I agree that changing the delimiter to comma might cause problem.  I would choose a character that is even more obscure.  Something untypeable perhaps.

Respected Advisor
Posts: 4,919

Re: Reading a CSV having a custom delimiter

Posted in reply to data_null__

The NUL character, of course! :smileylaugh:

PG

PG
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 329 views
  • 3 likes
  • 5 in conversation