Hi Team,
I have a table that sometimes is with records and sometimes no. Ex:
Table name Test:
NAME ID
A A1
Table name Test:
NAME ID
Im trying to do a Macro:
%MACRO TEST;
%IF NOBS > 0 AND %SYSFUNC(EXIST(TEST)) = 1 %THEN %DO;
CODE 1
%END;
%ELSE %DO;
CODE 2
%END;
%MEND;
The problem is that even the table Test is empty or with register, the macro will always execute the code 1.
Do you know why?
Tks a lot!!!
%IF NOBS > 0 AND %SYSFUNC(EXIST(TEST)) = 1 %THEN %DO;
This statement doesn't work as you think it will. You probably think NOBS is some number that the macro processor understands, but it is not. NOBS inside this %IF statement is a text string of four letters, the first letter is N, the second letter is O, and so on; and so NOBS is not a number. Macros evaluate text in a certain way, and the text string NOBS is always greater than 0, so code 1 always results.
Perhaps you mean
%IF &NOBS > 0 THEN ...
so if &NOBS contains a number then you could test if that number is > 0, but nowhere have you defined what the value of &NOBS is, so as shown, this wouldn't work either.
You didn't state your objective — is it trying to execute one set of code if data set TEST exists and has more than 0 observations, and execute other code if either TEST doesn't exist or has zero observations?
%IF NOBS > 0 AND %SYSFUNC(EXIST(TEST)) = 1 %THEN %DO;
This statement doesn't work as you think it will. You probably think NOBS is some number that the macro processor understands, but it is not. NOBS inside this %IF statement is a text string of four letters, the first letter is N, the second letter is O, and so on; and so NOBS is not a number. Macros evaluate text in a certain way, and the text string NOBS is always greater than 0, so code 1 always results.
Perhaps you mean
%IF &NOBS > 0 THEN ...
so if &NOBS contains a number then you could test if that number is > 0, but nowhere have you defined what the value of &NOBS is, so as shown, this wouldn't work either.
You didn't state your objective — is it trying to execute one set of code if data set TEST exists and has more than 0 observations, and execute other code if either TEST doesn't exist or has zero observations?
perfect, thank you
You don't need to know how may obs just some/none.
data _null_;
if eof then do;
put 'NOTE: data set or subset has 0 obs';
*Execute when data has 0 obs.;
end;
else do;
*has obs;
put 'NOTE: has obs';
end;
stop;
set sashelp.class end=eof; where sex eq 'f';
run;
It appears that the user also wants to test if the data set exists in the first place, hence the %SYSFUNC(EXIST( )), the data step code fails if the data set does not exist.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.