Date: Thu, 18 Jan 2007 23:44:49 -0500
Reply-To: Don Henderson <donaldjhenderson@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Don Henderson <donaldjhenderson@HOTMAIL.COM>
Subject: Re: question regarding eof
In-Reply-To: <BAY123-F37AC71A299CFFFB565F666DEA90@phx.gbl>
Content-Type: text/plain; charset="us-ascii"
Thanks Toby and Art for supporting my soapbox.
But your posts highlighted to me that I actually did not even look at
Mindy's real problem - detecting that the data set is empty.
Both you and Art offered a solution that uses the NOBS option to get the
number of observations and then checked for 0. The only problem with that
approach is that it does not always work, e.g., for views, sequential (for
us Jurassic types - tape data sets), some/most data base tables.
But now I can come back with another approach that also highlights the point
about END=. First let me say that I use either LastRec or LR for the END=
variable name as it highlights what it is actually checking.
And if we remember that such automatic variables are retained until the next
execution of the data step, the following code to set a macro variable that
specifies whether the data set has any observations now makes more sense:
data _null_;
if _n_ =1 and LR then call symput('HaveData','0');
set [my data set name] END=LR;
call symput('HaveData','1');
stop;
run;
If you are on the first execution of the data step (i.e., _N_ = 1) and you
have not yet read any data (i.e., you are before the SET statement), and SAS
has set the flag that says you have read the last record, the data set is
empty. As an aside, you can also think of doing this check before the SET is
actually doing an end-of-file check.
And this technique can work with a WHERE clause - in which case it checks if
any observations exist that meet the where clause criteria.
-don
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of toby
> dunn
> Sent: Thursday, January 18, 2007 11:14 PM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Re: question regarding eof
>
> Don ,
>
> You beat me to the email I was going to write. Well since you have
> seniority it is fitting that it come from one of your experience and
> wisdom.
>
> I would like to add even if one was to add EOF instead of End as the
> variable in the if statement it would not work, for the very reasons you
> have mentioned. There are ways to do what Mindy wants but the end =
> oprion
> is not the way.
>
> Another grip of the code is the message, to say that the data set does not
> exist is different from saying that the data is empty, which is what the
> intent of the code Mindy has is trying to express.
>
> Now to offer Mindy a working example of what she wants to do;
>
> Data _Null_;
>
> If ( NOBS ) Then Put "Note: Data Set Has Data" ;
> Else Put "Warning: Data Set Is Empty" ;
>
> Set Try2 Nobs = Nobs ;
>
> Stop ;
>
> Run ;
>
>
> Know where you are at, where you want to go, and then how to get there.
> Once you reach your destination look back and assess how you got there.
>
>
>
>
> Toby Dunn
>
> To sensible men, every day is a day of reckoning. ~John W. Gardner
>
> The important thing is this: To be able at any moment to sacrifice that
> which we are for what we could become. ~Charles DuBois
>
> Don't get your knickers in a knot. Nothing is solved and it just makes you
> walk funny. ~Kathryn Carpenter
>
>
>
>
>
>
> From: Don Henderson <donaldjhenderson@HOTMAIL.COM>
> Reply-To: Don Henderson <donaldjhenderson@HOTMAIL.COM>
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Re: question regarding eof
> Date: Thu, 18 Jan 2007 22:44:53 -0500
>
> Permit me to get on my soapbox please.
>
> But first let me say the Wensui is correct that you need to use the
> variable
> name in your IF statement. In the case of your code, the variable name is
> EOF.
>
> <SOAPBOX>
>
> The END= option allows you to specify the name of a variable whose value
> is
> set to 1 when the last record in the data set (or sets in the case of the
> merge statement or a set statement with multiple data sets) has been read.
> It is absolutely, categorically not an end-of-file flag (there is by the
> way
> an EOF option on the INFILE statement that is a true end of file check). I
> know that lots of examples that SAS provides use END=EOF but IMO that is
> bad
> practice as terminology is important to many people (including new users).
> They see EOF and assume that it really does reflect end of file as opposed
> to last record. And as a result their programs do not produce the correct
> results.
>
> </SOAPBOX>
>
> And my apologies for the rant :-).
>
> Regards,
> -don h
>
> > -----Original Message-----
> > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
> > Wensui Liu
> > Sent: Thursday, January 18, 2007 9:26 PM
> > To: SAS-L@LISTSERV.UGA.EDU
> > Subject: Re: question regarding eof
> >
> > mindy,
> > you should code it as 'if eof=0 then ...'.
> > the variable name should be eof instead of end.
> >
> > hth.
> >
> > On 1/18/07, Mindy <master2005_sas@yahoo.com> wrote:
> > > Hey, guys,
> > >
> > > I have a program as below:
> > >
> > > data try;
> > > input a b;
> > > cards;
> > > 1 2
> > > 3 4
> > > 3 8
> > > ;
> > > run;
> > >
> > > data try2;
> > > set try;
> > > where a=5;
> > > run;
> > > data _null_;
> > > set try2 end=eof;
> > > if end=0 then put "Warning: data set doesn't exist";
> > > run;
> > >
> > > I hope to have warning in log, but the from the log it says Variable
> > > end is uninitialized. Even I change condition to a=3, the warning
> > > variable end is unintialized is still there. Can anyone explain?
> > > thanks.
> > >
> > > Mindy
> > >
> >
> >
> > --
> > WenSui Liu
> > A lousy statistician who happens to know a little programming
> > (http://spaces.msn.com/statcompute/blog)
>
> _________________________________________________________________
> Get in the mood for Valentine's Day. View photos, recipes and more on your
> Live.com page.
> http://www.live.com/?addTemplate=ValentinesDay&ocid=T001MSN30A0701
|