BookmarkSubscribeRSS Feed
LesLim58
Calcite | Level 5

I'm trying to send RC=255 to JES when all 3 files are empty. The program sends RC=0 to JES.

 

DATA _NULL_;

            SET OT.XXXX OT.YYYY OT.ZZZZ;

            IF _N_ = 0 THEN ABORT 255;

RUN;

 

Thanks! Les

2 REPLIES 2
Quentin
Super User

I would try (untested):

 

DATA _NULL_;
  IF _N_ = 1 and last THEN ABORT 255;
  SET OT.XXXX OT.YYYY OT.ZZZZ end=last;
RUN;

When the step compiles, it will set the variable last to 1 if all datasets on the SET statement have 0 obs. 

 

Note: _N_ is not a counter of the number of records, it's a counter of the number of DATA step iterations.  _N_ can never be 0.

 

You need the PUT statement before the SET statement, because the step will stop when the SET statement executes if there are 0 records to read.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Lisa Mendez & Richann Watson present Get Tipsy with Debugging Tips for SAS® Code: The After Party on Wednesday Jul 16.
Register now at https://www.basug.org/events.
Tom
Super User Tom
Super User

Pretend you are SAS and you have to run your data step.

 

DATA _NULL_;
  SET OT.XXXX OT.YYYY OT.ZZZZ;
  IF _N_ = 0 THEN ABORT 255;
RUN;

First instruction you get to is the SET statement. When there are zero observations in all three of those input datasets what do you think SAS is going to do in that case?

 

 

Try adding some PUT statements to see what is happening.

DATA _NULL_;
  put 'BEFORE SET STATEMENT. ' _n_= ;
  SET OT.XXXX OT.YYYY OT.ZZZZ;
  put 'AFTER SET STATEMENT.' _n_=;
  IF _N_ = 0 THEN ABORT 255;
  put 'AFTER IF STATEMENT. ' _n_=;
RUN;

If you want to use a data step to detect number of observations then you need to use either END= or NOBS= options of the SET statement to specify the name of the temporary variable to use for that information.  You can then test that variable's value BEFORE the SET statement stops the data step.

You could use the boolean flag created by the END= option.

DATA _NULL_;
  IF EOF THEN ABORT 255;
  STOP;
  SET OT.XXXX OT.YYYY OT.ZZZZ END=EOF;
RUN;

Or the integer value created by the NOBS= option.

DATA _NULL_;
  IF NOBS=0 THEN ABORT 255;
  STOP;
  SET OT.XXXX OT.YYYY OT.ZZZZ NOBS=NOBS;
RUN;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 422 views
  • 2 likes
  • 3 in conversation