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"
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.