BookmarkSubscribeRSS Feed
DerekChan
Calcite | Level 5

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

4 REPLIES 4
Peter_C
Rhodochrosite | Level 12

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

Hobbes
Calcite | Level 5

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

Ksharp
Super User

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

FriedEgg
SAS Employee

data foo;

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

input @;

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

run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1138 views
  • 0 likes
  • 5 in conversation