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