BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

data montreal baltimore jersey;

set sashelp.baseball(keep= name team nhits);

if nhits >=20 and nhits <= 60;

if team = 'Montreal' then output Montreal;

if team = 'Baltimore' then output Baltimore;

if team = 'Jersey' then output Jersey;

run;

%macro eof(report);

data &report.;

 

if eof then output;

modify &report. end=eof;

 

run;

%mend;

 

%eof(montreal);

%eof(baltimore);

%eof(jersey);

 

In this case the only dataset that has no records is Jersey.  How would I insert a statement in the "Team" variable that says "No Records"

1 REPLY 1
mkeintz
PROC Star

Your EOF macro reading in the data set could check for zero observations and report that status to the log, otherwise leaving the data set unmodified.  Is that what you are looking for?

 

Here's an example using sashelp.class data set:

 

data m f uncoded;
  set sashelp.class;
  if sex='M' then output m;
  else if sex='F' then output f;
  else output uncoded;
run;

%macro eof(sex);
  data &sex;
  if nrecs=0 then do;
     put "No Observations in &sex";
     stop;
  end;
  if eof then output;
  modify &sex nobs=nrecs end=eof;
  run;
%mend eof;

options mprint;
%eof(m);
%eof(f);
%eof(uncoded);

 

It might seem counterintuitive to have an if-test for NRECS=0  preceding the apparent assignment of a value to nrecs in the MODIFY statement.  But that's because the assignment of a value to nrecs is done at the data step compiler stage, not the execution stage.

 

 

--------------------------
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

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 818 views
  • 1 like
  • 2 in conversation