BookmarkSubscribeRSS Feed
Patrick
Opal | Level 21
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
2 REPLIES 2
data_null__
Jade | Level 19
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]
Patrick
Opal | Level 21
Nice! Thanks for this one.
It really bothered me not to know how to do this.
Cheers
Patrick

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
  • 2 replies
  • 1167 views
  • 0 likes
  • 2 in conversation