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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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