New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sk1_SAS
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

%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

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

%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
Sk1_SAS
Obsidian | Level 7

perfect, thank you

data_null__
Jade | Level 19

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;

Capture.PNG

PaigeMiller
Diamond | Level 26

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