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
For 2 helpful statements
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
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
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
data foo;
infile '/temp/foo.txt' truncover length=len;
input @;
if prxmatch('/^1/',_infile_)>0 then row=_infile_; else delete;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.