DATA Step, Macro, Functions and more

How to read 3rd field from text file?

Reply
Respected Advisor
Posts: 4,173

How to read 3rd field from text file?

Hi

Nicole's "Reading a CSV file" reminded me that I'm having this long lasting unanswered question.

I believe it's not possible but I would be happy to be told otherwise.

O.K., here it comes:

Have: A pipe delimited file
Want: Read the 3rd column without having to map column 1 and 2 only using input statement.


data have;
infile datalines dlm='|' dsd truncover;
input VarForCol_C $;
datalines;
a1|b_1|c1
a2||c2
;
run;


I know that I could do something like...
...
input;
VarForCol_C=scan(_infile_,3,'|');
...

...but I'm wondering if I'm missing something and it could be done with some "switches" for the input statement without having to create any unnnecessary variables.

I'm looking for a generic solution which works for any number of fields(i.e. read field 15 in string with 50 fields).


Cheers
Patrick
Respected Advisor
Posts: 3,799

Re: How to read 3rd field from text file?

The input statement allows (variable-list) to have an N multiplier value. I'm not sure where it is documented. The multiplier is documented for (informat-list).

Anyway you can read a dummy variable skip n number of times. It can be $1 so it doesn't need to read any extra data as in the example below.

[pre]
data have;
infile datalines dlm='|' dsd truncover;
*input VarForCol_C $;
input (2*_dummy_)(:$1.) c $;
drop _dummy_;
datalines;
a1|b_1|c1
a2||c2
;;;;
run;
proc print;
run;
[/pre]
Respected Advisor
Posts: 4,173

Re: How to read 3rd field from text file?

Posted in reply to data_null__
Nice! Thanks for this one.
It really bothered me not to know how to do this.
Cheers
Patrick
Ask a Question
Discussion stats
  • 2 replies
  • 133 views
  • 0 likes
  • 2 in conversation