Proceeding to the next iteration after a recognizing my data set is empty

Reply
Regular Learner
Posts: 1

Proceeding to the next iteration after a recognizing my data set is empty

I am having trouble proceeding to the next iteration of my %do loop after recognizing that a dataset I am working with is empty. The code I am working on is structured below. I don't know an easy way to recognize the data set is empty and then skip to the next iterative step. I tried using %goto method but I couldn't get it to work. Also, since my %do is outside my data step, I can't use continue; to proceede either. 

 

%macro 1(varA);

...(code)....

%macro macro2(varC, varD);
      %do i = 1 %to &snum;
      data data1;
      set S_&i;

       ......

      run;

      %end;

      %mend;

      %macro2(1,2);

      %macro2(3,4);

      %mend;

%macro1(A);

 

Super User
Posts: 19,052

Re: Proceeding to the next iteration after a recognizing my data set is empty

Don't define a macro within a macro. You're asking for trouble.

 

How are you checking if your dataset is empty? It should be as simple as %if/%then-%do

Super User
Posts: 11,113

Re: Proceeding to the next iteration after a recognizing my data set is empty

One approach is to check if the data set you name has any observations you could do that with this code after the %do statement:

 

proc sql noprint;
   select nobs into setsize
   from dictionary.tables
   where libname='WORK' and memname=upcase("&S_i");
quit;

%if &setsize > 0 %then %do;
  <code to execute when the dataset has any observations>
%end;

You data step would go within the <> area. Since you are using a single level dataset name I am assuming the sets are in the WORK library. If that is not the case then put the approriate library in the comparison with Libname

 

You could also use an %else /%end to do something when the set is empty.

Untested for missing datasets.

Ask a Question
Discussion stats
  • 2 replies
  • 198 views
  • 0 likes
  • 3 in conversation