In SAS, how to output at least one line in a data step when SET has zero records?

378 views Asked by At

When reading a dataset which contains zero records the data step doesn't run any put statements.

data test;
    set SASHELP.class end=eof;
    where 1=0;
    *output;
    if _N_=1 then put '[';
    put '"' name '",';
    if eof then put name ']';
    *output;
    run;

Is it possible to force some of them?

The reason I'm doing this is that I'm creating json files with put statements
(I'm using SAS9.3 and don't have access to proc json yet.)

The json files are completely empty when the datasets are empty. I would need them to contain at least an empty array [].

This means I would need at least these two lines to run :

if _N_=1 then put '[';
if eof then put name ']';

For the moment I'm forced to split this in 3 data steps, one whith the head (_N_=1), one with the records, and the last with the tail (eof=1). Maybe there is a shorter solution?

1

There are 1 answers

0
Joe On BEST ANSWER

What happens here is that the moment SAS hits a set or input with no record to read in, it terminates the data step at that point. So since set is at the top of your loop, it terminates immediately.

If you move the two conditions that you want to the top, this will work, for example:

data test;
  if _n_=1 then put '[';
  if eof then put ']';
  set SASHELP.class end=eof;
  where 1=0;
  *output;
  put '"' name '",';
  *output;
run;

Then they'll operate whether or not there is data there.

Another option that I like is looping the dataset reads, but in this case it would probably be more work than is useful.