Date: Mon, 7 Aug 2000 18:09:52 GMT
Reply-To: Dale McLerran <dmclerra@FHCRC.ORG>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Dale McLerran <dmclerra@FHCRC.ORG>
Organization: Fred Hutchinson Cancer Research Center
Subject: Re: Empty dataset - special variable
Shawn,
There are some questions which must be asked before addressing your
question. While it is possible to generate a SAS dataset with no
observations, it is not something which occurs very often. More often,
the dataset itself is nonexistent and it is necessary to execute code
conditional upon the existence of a dataset. Is this the situation
that you find yourself in? If so, then you can perform a test before
you execute code something like the following:
data _null_;
name="work.testdata";
if exist(name) then do;
set work.testdata;
...
end;
else do;
put "File " name " does not exist";
stop;
end;
run;
Now, supposing that you do know that the file exists, and you really do
want to execute code based upon there being records in the dataset.
Another ugly question raises itself here. Some file types and engines
do not allow you to determine the number of records on the dataset.
For instance, dataset views do not support querying for the number of
observations because observations don't exist until the view is used.
Similarly, if the engine specifies a tape volume, then the number of
observations on the dataset would not be available. You can test for
the availability of the number of observations by employing the
ATTRN function, with the option ANOBS. Once you have determined that
the engine/file type supports determination of the number of observations
on the dataset, then there are a couple of different ways that you can
proceed, employing the ATTRN function with option ANY, or simply setting
the dataset and specifying an NOBS option on the SET statement.
Continuing with the above code, we would have
data _null_;
name="work.testdata";
if exist(name) then do;
dsid=open(name);
if attrn(dsid,anobs) then do;
if attrn(dsid,any) then do;
set work.testdata;
...
end;
else do;
<code to execute when there are no observations>;
end;
end;
else do;
put "File exists but must read file to determine if it has any obs";
end;
end;
else do;
put "File " name " does not exist";
stop;
end;
run;
I will let you investigate the SET statement with NOBS option if you
so desire. The block of code starting with IF ATTRN(DSID,ANY) would
need a little rewriting, but you should be able to figure it out.
Hope I have not thoroughly confused you with a long-winded response to
a simple question. But my experience is that simple questions often
arise because the correspondent is not aware of the complexity of
some issues and so does not necessarily know how to frame the proper
question.
Dale
--------------------------------------
Dale McLerran
Fred Hutchinson Cancer Research Center
Seattle, WA 98109
mailto:dmclerra@fhcrc.org
ph: (206) 667-2926
fax: (206) 667-5977
--------------------------------------
Ribordy_Shawn_C@cat.com (Shawn Ribordy) wrote in <398EDF76.F2CD17B@cat.com>:
>I have been working in a DATA _NULL_ step and I encounter an error in my
>process if I set an empty dataset into the DATA _NULL_ step.
>
>I want to output some default values if the dataset contains no
>records. Is there a special variable that contains the number of
>records in a dataset?
>
>For example, I want to do this
>
>DATA _NULL_;
>SET TESTDATA;
>IF OBSERVATIONS > 0 THEN DO;
> normal processing;
>END;
>ELSE DO;
> default values here;
>END;
>
>