in_user
in_user

Reputation: 1958

Read from multiple lines into single row until delimiter is encountered

I have been trying to read from a text file which has rows like below and has delimiter as semi-colon:

Sun rises
 in the east
  and;
sets in the 
west
;

I am trying to read the data from delimiter to delimiter in single separate records like below variable_name

1 Sun rises in the east and
2 sets in the west

I have tried almost all the options available with infile option for no avail. Is it possible to read like above? How to do it? Any clue/help would be appreciated.

Upvotes: 0

Views: 721

Answers (2)

Joe
Joe

Reputation: 63424

recfm=n is the way to tell SAS to not have 'line breaks'. So:

data want;
  infile "c:\temp\test.txt" recfm=n dsd dlm=';';
  length text $1024;
  input text $;
run;

Note that the line break will be read as just another two characters, so if you want to remove those characters you can use compress with the c option to remove control characters (including LF/FF).

Upvotes: 1

Tom
Tom

Reputation: 51591

Read it word by word and concatenate into longer lines.

data want ;
  infile mydat end=eof  ;
  length word $200 line $2000 ;
  drop word;
  do while (^eof and ^index(word,';'));
    input word @ ;
    line = catx(' ',line,compress(word,';'));
  end;
  if _n_ > 1 or line ne ' ' then output;
run;

Upvotes: 0

Related Questions