How to stop a macro if there is a empty data set?

Reply
Occasional Contributor
Posts: 7

How to stop a macro if there is a empty data set?

Hello I have a macro loop and has two steps, and the second step is transpose, but if the 1st step gets the empty data set (0 observation) then the 2nd step will have error. How can I have a step to test if the data set is empty, if its will jump to next loop. please see example below

 

%do m=1 %to 12;

          %do n=1 %to 6;         
                            /*1ST STEP*/      %Collect-DATA-SET-Step;

                            /*2ND STEP*/      %TRANSPOSE-Step;
          %end;    
%end;

 

What I would like is to have a test step after the 1st step, if the data set is empty, them move to next round loop.

 

Any thoughts are appreciated. Thank you. 

Super User
Posts: 10,283

Re: How to stop a macro if there is a empty data set?

proc sql noprint;
select nobs into :nobs from dictionary.tables
where libname = "XXXXX" and memname = "YYYYY";
quit;

%if &nobs ne 0 %then %do;

/* transpose step */

%end;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 7

Re: How to stop a macro if there is a empty data set?

Posted in reply to KurtBremser

Thank you. I am little confused with "where libname = "xxxxxx" and memname = "YYYYY"  ". What am I supposed to fillup for the xxxxxx and YYYYY?

 

 

I have posted a sample of my program, and can you please help me to correct it?  I have struggled for few hours on this....Smiley Sad

 

 

%do i= 1 %to 2;
%let PRIN1 = %scan(%bquote(&PRINLST), &i, ' ');
%put &prin1;

        %do j = 1 %to 2;
        %let POS1 = %scan(%bquote(&POS),&j, ' ');
        %put &POS1;

            %do m=1 %to 2;
            %let CLSYRMTH1 = %scan(%bquote(&CLSYRMTH),&m, ' ');
            %put &CLSYRMTH1;

 

                    %Basetable(&PRIN1, &POS1, &CLSYRMTH1); /*Gather base table*/

                    proc sql noprint;
                    select nobs into :nobs
                    from WORK.T_&PRIN1._&POS1._&CLSYRMTH1
                    where libname = "WORK" and memname = "YYYYY"
                    ;quit;

 

                    %if &nobs ne 0 %then %do;

                    %TRANSPOSE(&PRIN1, &POS1, &CLSYRMTH1); /*Transpose step*/

                    %end;

            %end;
      %end;
%end;

Super User
Posts: 10,283

Re: How to stop a macro if there is a empty data set?

memname is the dataset name (in uppercase) for which you want to get the observation count..

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 7

Re: How to stop a macro if there is a empty data set?

Posted in reply to KurtBremser

I figured it out. Thank you. 

Ask a Question
Discussion stats
  • 4 replies
  • 91 views
  • 0 likes
  • 2 in conversation