DATA Step, Macro, Functions and more

How do I skip garbage lines from input file

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

How do I skip garbage lines from input file

Hi There,

i few sample data placed in datalines, I just wanted to skip those lines which has got "name" and "age" values,

i just wanted to read only real data.

I tried this below code, but it's not cool, I am looking for some better options, like #N or / options to skip certain lines, but

I do not know how to use above tools?

 

data have;
input value $ @;
if value eq "name" then delete;
else; input name $ age;
drop value;
datalines;
name age
Dharmu 20
Vivian 18
name age
Tina 20
Kath 21
name age
John 30
Tim 30
;
run;

 

Thanks!!


Accepted Solutions
Solution
‎06-26-2016 11:51 PM
Super User
Posts: 5,081

Re: How do I skip garbage lines from input file

If you are positive of the pattern (one garbage line followed by two good lines), you can use:

 

data have;
input / name $ age;

output;

input name $ age;

output;
datalines;
name age
Dharmu 20
Vivian 18
name age
Tina 20
Kath 21
name age
John 30
Tim 30
;

 

But if you don't know the pattern ahead of time, there's no way for you to tell SAS the pattern (through your programming statements) ahead of time.

View solution in original post


All Replies
Super Contributor
Posts: 408

Re: How do I skip garbage lines from input file

What's not to like about what you have? I think it's cool enough.

 

A shorter alternative would be:

 

data have;
input name $ @;
if value eq "name" then delete;
else; input age;
datalines;
name age
Dharmu 20
Vivian 18
name age
Tina 20
Kath 21
name age
John 30
Tim 30
;
run;

Super Contributor
Posts: 408

Re: How do I skip garbage lines from input file

By the way, your original code has an issue. Be aware that if you hold the input record with a trailing '@' the pointer will not be at the beginning of the record for the next input statement. You will have to reset it:

 

...
else input @1 name $ age;
...

Hope this helps,

- Jan.

Contributor
Posts: 37

Re: How do I skip garbage lines from input file

Yes Thanks, I figured it out when saw the output, thanks for pointing that out,

but can we use those line pointer to skip the lines, I mean just curius to learn that.

 

Super Contributor
Posts: 408

Re: How do I skip garbage lines from input file


LittlesasMaster wrote:

but can we use those line pointer to skip the lines, I mean just curius to learn that.

 


Yes you can. Use the forward slash in your INPUT statement. Or simply use another INPUT statement.

 

See http://support.sas.com/documentation/cdl/en/basess/68381/HTML/default/viewer.htm#p0s16wvzu0z9q7n0zmx...

 

- Jan

Respected Advisor
Posts: 4,644

Re: How do I skip garbage lines from input file

I would do:

 

data have;
input @;
if _infile_ eq "name age" then input;
else do;
    input name $ age;
    output;
    end;
datalines;
name age
Dharmu 20
Vivian 18
name age
Tina 20
Kath 21
name age
John 30
Tim 30
;
PG
Solution
‎06-26-2016 11:51 PM
Super User
Posts: 5,081

Re: How do I skip garbage lines from input file

If you are positive of the pattern (one garbage line followed by two good lines), you can use:

 

data have;
input / name $ age;

output;

input name $ age;

output;
datalines;
name age
Dharmu 20
Vivian 18
name age
Tina 20
Kath 21
name age
John 30
Tim 30
;

 

But if you don't know the pattern ahead of time, there's no way for you to tell SAS the pattern (through your programming statements) ahead of time.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 481 views
  • 3 likes
  • 4 in conversation