DATA Step, Macro, Functions and more

Selective Input statement

Reply
Respected Advisor
Posts: 3,886

Selective Input statement

Hi All
For a case like in the example below:
Is there an efficient and elegant way to tell the input statement that I only want to read the field after the second delimiter?
Is there a way how I can avoid having to map and then drop columns a and b?

data _null_;
infile datalines dsd dlm='|' truncover;
input a $ b $ c $;
put c=;
drop a b;
datalines;
aaaa|bbb|ccc||ee
u|ww||yy|z
1|222|3333333|4|5
;

Thanks, Patrick
Super Contributor
Posts: 474

Re: Selective Input statement

Hi, if you just need C, you could parse directly from the input buffer, like this.

data _null_;
infile datalines dsd /* dlm='|' */ truncover;
input /* a $ b $ c $ */;
C=scan(_infile_,3,'|');
put c=;
/* drop a b; */
datalines;
aaaa|bbb|ccc||ee
u|ww||yy|z
1|222|3333333|4|5
;

be sure to prealocate a large enough length for the C var.

Cheers from Portugal.

Daniel Santos @ www.cgd.pt
Respected Advisor
Posts: 3,777

Re: Selective Input statement

[pre]
19 data _null_;
520 infile datalines dsd dlm='|' truncover;
521 input @'|' @'|' c:$8.;
522 put c=;
523 list;
524 datalines;

c=ccc
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
525 aaaa|bbb|ccc||ee
c=
526 u|ww||yy|z
c=3333333
527 1|222|3333333|4|5
[/pre]
Respected Advisor
Posts: 3,886

Re: Selective Input statement

Thank you guys!

Didn't think about the approach data _null_ took. Too bad that there is no such thing as a "repeater" (i.e. 5*@'|').

Thanks, Patrick
Super Contributor
Posts: 474

Re: Selective Input statement

Oh... but you code easily code that:

%macro repeat(N);
%do I=1 %to &N;
@ '|'
%end;
%mend repeat;

options mprint;

data _null_;
infile datalines dsd dlm='|' truncover;
input %repeat(2) c:$8.;
put c=;
list;
datalines;
aaaa|bbb|ccc||ee
u|ww||yy|z
1|222|3333333|4|5
run;
Valued Guide
Posts: 2,174

Re: Selective Input statement

just use a KEEP.
Ask a Question
Discussion stats
  • 5 replies
  • 167 views
  • 0 likes
  • 4 in conversation