- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!!!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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?
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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?
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
perfect, thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Paige Miller