DATA Step, Macro, Functions and more

[Data step] read records starting with '1' only

Reply
Occasional Contributor
Posts: 5

[Data step] read records starting with '1' only

I want to use data step to input the following data records (contained in a text file)

Example:

0#startambs

1Peter 13 pilot pass

1Mary 14 teacher pass

2#end ambs

0#startambs

1John 16 pilot pass

1Susan 18 teacher pass

2#end ambs

The first digit indicates the property of record. 0 = header, 1=actual data 2=trailer.

I only need to input actual data only. i.e. only read records starting with 1, ignore those start with 0 or 2

what can i write in the code?

I think i can use an infile statement, but i dont know how to set condition to read records starting with 1 only

Valued Guide
Posts: 2,175

Re: [Data step] read records starting with '1' only

For 2 helpful statements

  1. INFILE  defines the file to read
  2. INPUT  defines the fields you read from the file

you'll find examples and (the very broad) syntax in SAS documentation at these links

For your example, something like the following might work

data ;

   infile 'd:\your\folders\and_file' truncover ;

   input @1 record_type $char1. @ ;

   if record_type NE '1' then delete ;

   input stuff $ ;

run ;

good luck

Occasional Contributor
Posts: 16

[Data step] read records starting with '1' only

This will do the job:

DATA AAA ;

      drop lead;

      length Lead 3 name $ 10 age 3 prof $ 10res $ 10;

      INPUT @1 LEAD 1.@;

      if Lead=1 then do;

            input @2 name : $10. age prof  res ;

            output;

      end;

DATALINES;

0#startambs

1Peter 13 pilot pass

1Mary 14 teacher pass

2#end ambs

0#startambs

1John 16 pilot pass

1Susan 18 teacher pass

2#end ambs

;

Regards,

hobbes

Super User
Posts: 9,681

[Data step] read records starting with '1' only

How about:

filename x 'c:\x.txt';
data want;
infile x truncover length=len;
input row $varying200. len;
if left(row) eq: '1';
run;

Ksharp

Trusted Advisor
Posts: 1,300

Re: [Data step] read records starting with '1' only

data foo;

infile '/temp/foo.txt' truncover length=len;

input @;

if prxmatch('/^1/',_infile_)>0 then row=_infile_; else delete;

run;

Ask a Question
Discussion stats
  • 4 replies
  • 147 views
  • 0 likes
  • 5 in conversation