Date: Fri, 8 Mar 2002 12:40:49 -0600
Reply-To: Jonathan Goldberg <jonathan@MATLOCK.WUSTL.EDU>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jonathan Goldberg <jonathan@MATLOCK.WUSTL.EDU>
Subject: Re: Empty datasets..
In-Reply-To: <sc878338.094@firsthealth.com>
Content-Type: TEXT/PLAIN; charset=US-ASCII
Well, live and learn. Jack is right about the name issue. I was relying
on this, from the V8 SCL documentation:
FETCH
--------------------------------------------------------------------------------
Reads the next nondeleted observation from a SAS data set into the Data
Set Data Vector (DDV) and returns a value
but, running in a data step nothing was added to the PDV. So, the PDV and
the DDV are not the same thing?
I was assuming that they were, and that therefore there had to *be* an
active PDV, and that therefore fetch could only be used in a data step.
But I now realize this is wrong. I also saw the example using
%sysfunc(fetch(xxx)) in the documentation. Since this happens at macro
execution time, my earlier opinion was an example of macro vs. data step
timing confusion. I should know better.
The same documentation says:
FETCH returns a 0 if the operation was successful, 0 if it was not
successful, and - 1 if the end of the data set is reached. FETCH skips
observations marked for deletion.
I ran the following little test before I posted:
*-- test data;
1 data one;
2 do i = 1 to 6;
3 var = ranuni(0);
4 output;
5 end;
6 stop;
7 run;
*-- fetch it in a data step and look at the return codes;
8 data _null_;
9 dsid = open('one');
10 put dsid=;
11 rc = fetch(dsid);
12 put 'rc from fetch = ' rc;
13 rc = close(dsid);
14 put 'rc from close = ' rc;
15 run;
dsid=1
rc from fetch = 0
rc from close = 0
*-- fetch it in a macro and look at the return codes;
16 %macro fetchtest(mydsn);
17 %let dsid = %sysfunc(open(&mydsn));
18 %put dsid = &dsid;
19 %let rc = %sysfunc(fetch(&dsid));
20 %put rc from fetch = &rc;
21 %let rc = %sysfunc(close(&dsid));
22 %put rc from close = &rc;
23 %mend fetchtest;
24 %fetchtest('one')
dsid = 0
rc from fetch = 70021
rc from close = 70021
Now I am confused. I would have interpreted the macro rc as failure;
perhaps the data set dosen't exist, or has no observations. In the data
step everything looks fine. Would anyone care to untangle this?
Jonathan Goldberg
Missouri Alcoholism Research Center
Dept. of Psychiatry
Washington University School of Medicine
40 N. Kingshighway, Suite One
St. Louis, MO 63108
314-286-2212
On Thu, 7 Mar 2002, Jack Hamilton wrote:
> "Jonathan Goldberg" <jonathan@MATLOCK.WUSTL.EDU> 03/07/2002 1:25 PM
> wrote:
>
> >Regarding:
>
> >> Kemp and I posted yesterday does. This can also be done in a
> macro,
> >> using fetch() rather than attrn() (if I recall correctly).
> >
> >you do recall correctly, although needing to read all the variables
> into
> >the DSDV could cause namespace pollution.
>
> The default is for SAS *not* to read the variables into the PDV or
> macro space. To get the value of a variable, you must use GETVARC(),
> GETVARN() or CALL SET().
>
> >Also, a macro using fetch can
> >only be run within a data step.
>
> Perhaps this was true in version 6, but it's not true in version 8.
> The online documentation for FETCH() gives an example of using it in a
> macro.
>
> Here's a revised macro to print a message if there are no observations
> in a data set:
>
> -----
> %macro noobs(data=_last_,
> message='No observations in data set',
> row=5,
> col=5);
>
> %local dsid rc;
>
> %let dsid = %sysfunc(open(&DATA.));
> %let rc=%sysfunc(fetch(&DSID));
>
> %if &RC. = -1 %then
> %do;
> data _null_;
> file print;
> put #&ROW. @&COL. &MESSAGE.;
> run;
> %end;
>
> %let rc=%sysfunc(close(&DSID));
>
> %mend;
> -----
>
> I think this will work for any data set or view. It could use a bit
> more error checking (Valid data set passed? Valid row and column?
> Other return codes?).
>
>
>
>
>
>
> --
> JackHamilton@FirstHealth.com
> Manager, Technical Development
> METRICS Department, First Health
> West Sacramento, California USA
>
>
>