BookmarkSubscribeRSS Feed
Patrick
Opal | Level 21
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
5 REPLIES 5
DanielSantos
Barite | Level 11
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
data_null__
Jade | Level 19
[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]
Patrick
Opal | Level 21
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
DanielSantos
Barite | Level 11
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;
Peter_C
Rhodochrosite | Level 12
just use a KEEP.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 918 views
  • 0 likes
  • 4 in conversation