Date: Mon, 11 May 2009 20:47:11 -0400
Reply-To: Lou <lpogoda@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Lou <lpogoda@HOTMAIL.COM>
Organization: A noiseless patient Spider
Subject: Re: how to read trailer lines into SAS
Let's take it step by step;
On the first pass through the DATA step, DONE = 0 as execution begins.
Since DONE = 0, the INPUT statement in the first DO loop executes and the
first record is read in. Assuming there is more than one record in the
file, this process repeats - let's take that as a given - until the DATA
step gets to the very last record in the input file.
On that last iteration, DONE is still equal to 0 at the top of the DATA
step. That being the case, the last record is read in by the first DO loop,
just like all the others in the file. Then, and only then, DONE is set to
1, because then and only then does the step "know" it's reached the end of
the file.
Since now DONE =1, the step goes on to attempt to execute the INPUT
statement in the second do loop. However, the file pointer is sitting at
the end of the file and there's nothing to read - I'd expect to see a LOST
CARD note on the log - and variable TOT_SUM never gets a value. Since this
is not a fatal error, the OUTPUT statement still gets executed, and I'd
expect to see variables ID, QUANTITY, COST and TOT_SUM in dataset TRAILER,
though only ID would have a non-missing value.
You restrict the variables in an output dataset by the use of DROP or KEEP
dataset options - your DATA statement could read
DATA HAVE (DROP = TOT_SUM) TRAILER (KEEP = TOT_SUM);
for instance.
As to determining where to output each record, in the absence of some flag
on the record itself, you'll have to examine the result of the INPUT
statement and decide where you are, and output to the appropriate dataset.
Something like the following **untested** code might do the trick;
data have (drop = tot_sum) trailer (keep = tot_sum)
length dummy1 $ 4
dummy2 - dummy3 8
id $ 3
quantity cost tot_cost 8;
infile 'your infile stuff here';
input dummy1 dummy2 dummy3;
if length dummy1 = 3 then do;
id = dummy1;
quantity = dummy2;
cost = dummy3;
output have;
end;
if length dummy1 = 1 then do;
(your example doesn't say what you want to do with the next to last
line)
end;
if index(dummy1, '.') then do;
tot_cost = input(dummy1, best.);
output trailer;
end;
run;
no new material below, included for reference only
"Ruby" <windofoct@gmail.com> wrote in message
news:f1712419-94cb-46ab-b465-5db2f9e4cf0f@j12g2000vbl.googlegroups.com...
> Hello SAS experts,
>
> I have a raw .txt file with two trailer lines at the end of this file.
> I created a very simple example showed below to illustrate my
> question. First, I don't know how to read the line second to last line
> in. Second, I don't know what's wrong with my sas code when I tried to
> just read the last line into trailer dataset. Hope somebody could help
> me fix the problem. Thanks very much!
>
> 312 2 10
> 234 1 3
> 333 1 30
> 3
> 53.00
>
>
> data have trailer;
> infile 'desktop\temp.txt' dlm='09'X missover end=last;
> if not last then do;
> input ID $ quatity cost;
> output have;
> end;
> else do;
> input tot_sum;
> output trailer;
> end;
> run;