DATA Step, Macro, Functions and more

Reading Delimited Text Files into SAS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Reading Delimited Text Files into SAS

Hi,

I need to read Delimited Text Files into SAS.

The delimiter is a single blank (delimiter='20'x).

My problem is that blank that are protected by "" are not a delimiter.

Example:

Fabio USA "San Francisco"

must be:

var1=Fabio

var2=USA

var3="San Francisco"

Thank you very much for your help.

Fabio


Accepted Solutions
Solution
‎11-28-2013 12:33 PM
Super User
Super User
Posts: 7,067

Re: Reading Delimited Text Files into SAS

Because the quotes are not around the full value of the field.

data a;

  infile cards dsd dlm=' ' truncover;

  length var1-var3 $100 ;

  input var1-var3;

  put (var1-var3) (=/);

cards4;

Fabio USA "San Francisco"

test user-agent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink" x

test "user-agent=""Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink""" y

;;;;

You could try using the SCAN() function to parse the line yourself.

data a;

  infile cards dsd dlm=' ' truncover;

  length var1-var3 $100 ;

  array v var1-var3 ;

  input ;

  do i=1 to 3; v(i)=dequote(scan(_infile_,i,' ','QM')); end;

  put (var1-var3) (=/);

cards4;

Fabio USA "San Francisco"

test user-agent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink" x

test "user-agent=""Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink""" y

;;;;

View solution in original post


All Replies
Super User
Super User
Posts: 7,067

Re: Reading Delimited Text Files into SAS

Use the DSD option.

data a;

  infile cards dsd dlm=' ' truncover;

  length var1-var3 $20 ;

  input var1-var3;

  put (var1-var3) (=/);

cards;

Fabio USA "San Francisco"

;;;;

Occasional Contributor
Posts: 11

Re: Reading Delimited Text Files into SAS

Thank you Tom!

But reading this txt:

test user-agent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink"

with you code I don't have

var1= test

var2= user-agent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink"

but

var1= test

var2= user-agent="Mozilla/4.0

var3= (compatible;

....

Why?

Thank you very much

Solution
‎11-28-2013 12:33 PM
Super User
Super User
Posts: 7,067

Re: Reading Delimited Text Files into SAS

Because the quotes are not around the full value of the field.

data a;

  infile cards dsd dlm=' ' truncover;

  length var1-var3 $100 ;

  input var1-var3;

  put (var1-var3) (=/);

cards4;

Fabio USA "San Francisco"

test user-agent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink" x

test "user-agent=""Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink""" y

;;;;

You could try using the SCAN() function to parse the line yourself.

data a;

  infile cards dsd dlm=' ' truncover;

  length var1-var3 $100 ;

  array v var1-var3 ;

  input ;

  do i=1 to 3; v(i)=dequote(scan(_infile_,i,' ','QM')); end;

  put (var1-var3) (=/);

cards4;

Fabio USA "San Francisco"

test user-agent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink" x

test "user-agent=""Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1Smiley Wink""" y

;;;;

Valued Guide
Posts: 2,177

Re: Reading Delimited Text Files into SAS

Fabio

If that is the conmon structure of your data, use that= as delimiter as well as blank.

OK it separates names from values,  but you know about that.

In this way

   DLM= ' ='

Then either blank or equals would be used as delimiters.  Your data provider appears to be properly qualifying (quoting) the values that contain blanks so the DSD option will properly handle those.

Good luck

peter.C

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 318 views
  • 7 likes
  • 3 in conversation