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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 720 views
  • 0 likes
  • 2 in conversation