Hi, I found a macro that gets a table's size. I was wondering if there's a way to adjust it so it can output an empty table?
%macro get_table_size(inset,macvar);
data _null_;
set &inset ;
call symput("&macvar",size);
stop;
run;
%mend;
And here's how to use it:
%let count=;
%get_table_size(table, count);
%put &=count;
right now for empty table, %put statement will return count =
I want it to return count = 0 because then I will expand this to:
if &count= 0 then do;
data new;
message = 'table is empty';
...
how may I do this? Thanks
Most SAS data steps stop when they read past the end of the input. So if you are reading from an empty dataset then any statement in the data step that is after the SET statement will never execute since the step stops as soon as it tries to read a non-existing observation.
So it sounds like you want to know if you can use the NOBS= option of the SET statement to populate a macro variable that you can later test. Since SAS will set the NOBS= variable's value while building the data step you can reference its value before the SET statement even executes.
%macro get_table_size(inset,macvar);
%if not %symexist(&macvar) %then %global &macvar;
data _null_;
call symputx("&macvar",size);
stop;
set &inset nobs=size;
run;
%mend;
Adding the test will make sure that the caller can reference the macro variable, even if they did not create it before calling.
Use the newer (better?) CALL SYMPUTX() function will make sure that the value does not have leading spaces or get converted to scientific notation when larger than 999,999,999,999.
What is the SIZE variable in your CALL SYMPUT statement? (And why are you using the old CALL SYMPUT instead of CALL SYMPUTX?)
You can force the CALL SYMPUTX() statement to run even if the input dataset is empty by moving it to BEFORE the SET statement.
Or you could just add a %LET to your macro.
Or you could change the %LET before your macro call.
Most SAS data steps stop when they read past the end of the input. So if you are reading from an empty dataset then any statement in the data step that is after the SET statement will never execute since the step stops as soon as it tries to read a non-existing observation.
So it sounds like you want to know if you can use the NOBS= option of the SET statement to populate a macro variable that you can later test. Since SAS will set the NOBS= variable's value while building the data step you can reference its value before the SET statement even executes.
%macro get_table_size(inset,macvar);
%if not %symexist(&macvar) %then %global &macvar;
data _null_;
call symputx("&macvar",size);
stop;
set &inset nobs=size;
run;
%mend;
Adding the test will make sure that the caller can reference the macro variable, even if they did not create it before calling.
Use the newer (better?) CALL SYMPUTX() function will make sure that the value does not have leading spaces or get converted to scientific notation when larger than 999,999,999,999.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.