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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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.

laiguanyu001
Fluorite | Level 6
sorry I am new in sas programming. 

I forgot to add in the line nobs = size;



And I'm sorry... I didn't understand any of your solution.... where to add the %let statement and what is it for?  and also if I call symputx before set, how do I specify nobs = size?

Tom
Super User Tom
Super User

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.

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

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 3 replies
  • 2238 views
  • 0 likes
  • 2 in conversation