BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FelixAndrew
Calcite | Level 5

Hi- running SAS 9.4 on an IBM- z/OS 2.3 platform, I have run into a wall regarding the usage of  IF ELSE on a particular SAS program.

 

I have job A that scans my DASD environment for volumes that are eligible for a REORG. Once those DASD volumes have been identified, job A builds job B, which in turn executes to perform the actual compaction/REORG of the DASD volumes that were identified as candidates.

 

There will be times when no DASD volumes will be flagged as candidates, and this is normal. I have created SAS code for those instances when zero  volumes are identified as candidates and in those instances, job A should not create job B.

 

DATA DEFRAG;

SET ELIGIBLE NOBS=NUM_OBS;

FILE OUT;

/* MAXDEFRAG = NUMBER OF PROCS NEEDED */

RETAIN MAXDFRAG 200 I 0 N 0;

N + 1;

IF N > MAXDFRAG THEN STOP;

ELSE SELECTED = 'Y';

 

/* GENERATE JOB CARDS */

IF N = 1 THEN DO;

PUT @1 '//PSM4FDR2 JOB (B266,P116),''DASD COMPACT'',' @;

PUT 'MSGCLASS=F,CLASS=K';

PUT @1 '/*XEQ PYAPROD';

PUT @1 '/*ROUTE PRINT LOCAL';

PUT @1 '//*';

PUT @1 '//* THIS JOB WAS GENERATED BY PSM4FDR1';

 

The above represents part of SAS code that interrogates the availability of candidate volumes residing in file ELIGIBLE.

 

If there is data in file ELIGIBLE (candidate volumes) then proceed creating job B. If there is no data in file ELIGIBLE (no volumes flagged) then stop and do not create job B.

 

The me running into a wall is that as previously mentioned, there will be times when no candidate volumes will be flagged for a REORG, as stated with message:

 

NOTE: No observations in data set WORK.ELIGIBLE.

NOTE: The PROCEDURE PRINT used 0.00 CPU seconds and 32414K.

 

This is normal so job A should not create job B. Unfortunately for me job B is being created and after looking at my SAS code over and over again, I don't understand the reason why or what is wrong with my code above.

 

Basically, this is the code that questions and determines if job B should be created or not: 

 

DATA DEFRAG;

SET ELIGIBLE NOBS=NUM_OBS;

FILE OUT;

/* MAXDEFRAG = NUMBER OF PROCS NEEDED */

RETAIN MAXDFRAG 200 I 0 N 0;

N + 1;

IF N > MAXDFRAG THEN STOP;

ELSE SELECTED = 'Y';

 

What do you see wrong with my code above, if anything?

 

thanks to all for even looking at my question..

 

regards;

 

Felix Andrew

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Assuming that data set ELIGIBLE always exists, even when it has zero observations.  Then you could put this code in

DATA DEFRAG;
  IF NUM_OBS=0 then do;
    /* any relevant code you want for empty data sets here */
   stop;
  END;
  SET ELIGIBLE NOBS=NUM_OBS;
  … the rest of your code …;
run;

 

You might find it counter-intuitive to do an IF test on NUM_OBS prior to the SET statement that declares "NOBS=NUM_OBS".   However, that is just a result of the difference between compile-time (when SAS construct the computer code) and execution time (when SAS executes the generated code).

 

The "set eligible nobs=num_obs" tells the compiler to create a variable NUM_OBS with the nobs value from the metadata about dataset ELIGIBLE, and to have that value available at the start of execution.  As a result the IF NUM_OBS test will perform properly.

 

BTW, if dataset ELIGIBLE has zero observations, then the IF NUM_OBS=0 is true and your log will show a message like

    NOTE: The data set WORK.DEFRAG has 0 observations and xxx variables.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

Assuming that data set ELIGIBLE always exists, even when it has zero observations.  Then you could put this code in

DATA DEFRAG;
  IF NUM_OBS=0 then do;
    /* any relevant code you want for empty data sets here */
   stop;
  END;
  SET ELIGIBLE NOBS=NUM_OBS;
  … the rest of your code …;
run;

 

You might find it counter-intuitive to do an IF test on NUM_OBS prior to the SET statement that declares "NOBS=NUM_OBS".   However, that is just a result of the difference between compile-time (when SAS construct the computer code) and execution time (when SAS executes the generated code).

 

The "set eligible nobs=num_obs" tells the compiler to create a variable NUM_OBS with the nobs value from the metadata about dataset ELIGIBLE, and to have that value available at the start of execution.  As a result the IF NUM_OBS test will perform properly.

 

BTW, if dataset ELIGIBLE has zero observations, then the IF NUM_OBS=0 is true and your log will show a message like

    NOTE: The data set WORK.DEFRAG has 0 observations and xxx variables.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
FelixAndrew
Calcite | Level 5

thank you for the good feedback. Much appreciated.

 

regards..

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 426 views
  • 0 likes
  • 2 in conversation