BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hari2
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Hari2
Quartz | Level 8

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

14 REPLIES 14
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Hari2
Quartz | Level 8

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.

Ksharp
Super User

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;
Hari2
Quartz | Level 8

Hi,

 

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

Shmuel
Garnet | Level 18

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;
Hari2
Quartz | Level 8

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.

Shmuel
Garnet | Level 18

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;
Ksharp
Super User

Use

 

ENDSAS;

 

instead of

 

%abort;

?

Hari2
Quartz | Level 8

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.

 

 

Shmuel
Garnet | Level 18

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.

Hari2
Quartz | Level 8

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.

AlanC
Barite | Level 11

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.

https://github.com/savian-net
Vince_SAS
Rhodochrosite | Level 12

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

Hari2
Quartz | Level 8

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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