How to stop SAS not to Execute any Furthur statements outside Macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

How to stop SAS not to Execute any Furthur statements outside Macro

[ Edited ]

Hi All,

 

I have a master program which executes multiple sas programs based on different conditions between each other. So here based on a particular confition, I want sas not to process any furthur datasets/statements (SAS CODE OUTSIDE MACRO ALSO) .

 

please check sample code below and code below should not execute code for dataset Class2.  Please suggest me how to achieve this. (I tried with "Return" statement but it still executes 'class2' and if we use GOTO statemnt, we should write lable with in Macro only). Your help is appreciated.

 

data class;

set sashelp.class;

where sex='n';

run;

 

%macro chk;

proc sql;

select count(*) into :cnt from class;

quit;

%if &cnt. Le 0 %then %do;

%put 'data is not Available';

%put 'SAS will exit and not process any furthur datasets';

%end;

%else %put 'Data is Available';

 

%mend;

%chk;

data class2;

set sashelp.class;

run;

 

Thanks,

Hari.


Accepted Solutions
Solution
‎07-31-2017 11:01 AM
Occasional Contributor
Posts: 19

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Hi,

 

 

sorry for late reply.

"endsas" works in local pc sas. however , I want to end it remote session and unfortunately its not working when I submit between RSUBMIT and ENDRSUBMIT.  I want this to work between rsubmit and endrsubmit, becaue our process runs remotely and any condition that I chekc will be in remote only. so based on condition, SAS sessin should end.

 

 

View solution in original post


All Replies
Super User
Super User
Posts: 7,988

Re: How to stop SAS not to Execute any Furthur statements outside Macro

You can do it in a number of ways.  Are you using EnterriseGuide for intance as that has conditionals on program execution.  You can do it in code in a number of ways also:

data class;
  set sashelp.class;
  where sex='n';
run;

data _null_;
  set sashelp.vtable (where=(libname="WORK" and memname="CLASS"));
  if nobs=0 then call execute('%put "No obs found, nothing further done";');
  else call execute('%put "Obs found, doing something";');
run;

Using code generation.  You could also call macros or other code in those blocks, or include other programs

Occasional Contributor
Posts: 19

Re: How to stop SAS not to Execute any Furthur statements outside Macro

[ Edited ]

Hi,

 

Thanks.

 I don't use EG rather I use PC SAS and will submit in remote session.

 

could you please use code which I have posted, run and make it not to execute the code which is written for Classe 2 datset creation.

 

I should stop SAS not to process any thing furthur at all when Number o frecords in class is <= 0 (As per code which I posted earlier).

 

Thank,

Hari.

Super User
Posts: 10,044

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Try %ABORT;

 

data class;

set sashelp.class;

where sex='n';

run;

 

%macro chk;

proc sql;

select count(*) into :cnt from class;

quit;

%if &cnt. Le 0 %then %do;

%put 'data is not Available';

%put 'SAS will exit and not process any furthur datasets';

%end;

%else %put 'Data is Available';

 %abort;

%mend;

%chk

data class2;

set sashelp.class;

run;
Occasional Contributor
Posts: 19

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Hi,

 

Even %Abort will work same as %return. In our case, the given code still creates Class2 dataset. pleaes gove any other alternative.

Trusted Advisor
Posts: 1,584

Re: How to stop SAS not to Execute any Furthur statements outside Macro

If you want to run last step when there are more than 0 observations then

enter your conditional code to run inside the macro:

%macro chk;
   proc sql;
      select count(*) into :cnt from class;
   quit;
  %if &cnt. Le 0 %then %do;
       %put 'data is not Available';
       %put 'SAS will exit and not process any furthur datasets';
  %end;
  %else %do;
      %put 'Data is Available';
      data class2;
       set sashelp.class;
      run;
  %end; 
%mend;
%chk;
Occasional Contributor
Posts: 19

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Thanks you Sir.

 

but this is not what I am expecting. In my actual code, I will have many other data steps outside of Macro and I should not execute any of them when  a certain conditon with in that macro is satisfied. please check again and revert.

Trusted Advisor
Posts: 1,584

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Then save the conditional code to run as a separate file and do:

 

%macro chk;
   proc sql;
      select count(*) into :cnt from class;
   quit;
  %if &cnt. Le 0 %then %do;
       %put 'data is not Available';
       %put 'SAS will exit and not process any furthur datasets';
  %end;
  %else %do;
      %put 'Data is Available';
      
       %include '...path_and_name_of_code_to_run.sas';

  %end; 
%mend;
%chk;
Super User
Posts: 10,044

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Use

 

ENDSAS;

 

instead of

 

%abort;

?

Solution
‎07-31-2017 11:01 AM
Occasional Contributor
Posts: 19

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Hi,

 

 

sorry for late reply.

"endsas" works in local pc sas. however , I want to end it remote session and unfortunately its not working when I submit between RSUBMIT and ENDRSUBMIT.  I want this to work between rsubmit and endrsubmit, becaue our process runs remotely and any condition that I chekc will be in remote only. so based on condition, SAS sessin should end.

 

 

Trusted Advisor
Posts: 1,584

Re: How to stop SAS not to Execute any Furthur statements outside Macro

[ Edited ]

Hvae you tried to use the macro program I have posted ?

The logic of it is:

 

 rsubmit;

%macro macro_name;

     %if <condition> is true

           %then %do;

                  <run code-1> ;

            %end;

     %else do;

               <run code-2>;

     %end;

%mend macro_name;

%macro_name;

 endrsubmit;

 

where <run_code> is any sas code or program included.

Occasional Contributor
Posts: 19

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Hi Sir,

 

I tried this too. It's a direct code or through #include, the challenge is sas was executing rest of the code after macro. I dont want that to happen because once I find any error in any step sas should not execute any statements furthur.

 

Now finally some how I am able to do it using endsas itself.

 

Thanks for your suggetions.

Frequent Contributor
Posts: 119

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Hari,

 

I would suggest you look at the following article to see if it can play into your scenario (vs just the mockup code below):

 

https://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#win-stmt-waitfor....

 

This is the direction I would look at facing similar issues in the past. YMMV.

SAS Super FREQ
Posts: 304

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Add this to your %THEN %DO loop to see if it provides the desired result:

 

data _null_;  abort cancel; run;

 

Vince DelGobbo

SAS R&D

Occasional Contributor
Posts: 19

Re: How to stop SAS not to Execute any Furthur statements outside Macro

Posted in reply to Vince_SAS

Hi, This works but the challenge is sas will still execute statements outside macro/step which are written after. This should not happen. 

 

endsas option is working for me. thanks for your suggetion.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 363 views
  • 1 like
  • 6 in conversation