<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Exception Handling in a construct like try/catch in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Exception-Handling-in-a-construct-like-try-catch/m-p/551575#M153262</link>
    <description>&lt;P&gt;I'm trying to do something similar to another post, but the accepted answer didn't meet my requirements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We're working on a SAS macro to produce control chart data tables using PROC SHEWHART.&amp;nbsp; We need to&amp;nbsp;capture exceptions/errors, take appropriate action, and then continue with subsequent data steps/procedures.&amp;nbsp; In the example I've included below, we're simply changing from a p-Chart to a c-Chart when something causes the p-Chart to fail.&amp;nbsp; This specific example is where the denominator value is larger than the numerator; that is one known failure mode, but we don't know what other p-Charts rules could be violated.&amp;nbsp; Pre-testing for a specific failure mode, or failure modes, is not a desirable solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As expected, an exception, caught while the SYNTAXCHECK option is set, causes SAS to enter the special syntax check mode, and subsequent procedure calls within the macro fail due to empty data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a workaround where I temporarily set the NOSYNTAXCHECK system option immediately before the error-prone procedure call, and then set SYNTAXCHECK immediately following the exception handling code.&amp;nbsp;&amp;nbsp;In our environment we are supposed to keep the SYNTAXCHECK option active for the duration of the SAS session, so this is also an undesirable solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it possible to recover from "syntax check mode" after an error has occured with the SYNTAXCHECK system option set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would rather include a few lines of "reset" code within my exception-handling block and continue on.&amp;nbsp; I have set the OBS and REPLACE system options in my exception-handling block, but that doesn't appear to completely reverse the syntax check mode changes to the environment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mprintnest mlogic mlogicnest symbolgen spool syntaxcheck;
options msglevel=i notes source serror fullstimer errorcheck=normal;

%put NOTE: obs option : %sysfunc(getoption(obs,keyword));
%put NOTE: replace option : %sysfunc(getoption(replace));
%put NOTE: syntaxcheck option : %sysfunc(getoption(syntaxcheck));

data WORK.shewhart_data;
  input interval_month mmddyy10. numerator denominator;
  datalines;
07/01/2018 5 11
08/01/2018 14 9
09/01/2018 12 24
10/01/2018 19 27
11/01/2018 15 13
12/01/2018 20 34
01/01/2019 25 15
02/01/2019 22 31
03/01/2019 34 28
;

%macro test_shewhart(
  in_data=,
  out_data=
);
  proc shewhart data=&amp;amp;in_data;
    pchart numerator*interval_month
      / subgroupn=denominator sigmas=3 nochart outtable=&amp;amp;out_data;
  run;

  %if &amp;amp;syserr ne 0 %then
    %do;
      options obs=max replace; run; quit;

      %put NOTE: obs option : %sysfunc(getoption(obs,keyword));
      %put NOTE: replace option : %sysfunc(getoption(replace));
      %put NOTE: syntaxcheck option : %sysfunc(getoption(syntaxcheck));

      proc shewhart data=&amp;amp;in_data;
        cchart numerator*interval_month
          / sigmas=3 nochart outtable=&amp;amp;out_data;
      run;
    %end;

%mend test_shewhart;

%test_shewhart(
  in_data = WORK.shewhart_data,
  out_data = WORK.chart_data
)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've also attached the log with this code run from a brand new SAS session.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;BR /&gt;David&lt;/P&gt;</description>
    <pubDate>Tue, 16 Apr 2019 22:19:30 GMT</pubDate>
    <dc:creator>DavidRice</dc:creator>
    <dc:date>2019-04-16T22:19:30Z</dc:date>
    <item>
      <title>Exception Handling in a construct like try/catch</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exception-Handling-in-a-construct-like-try-catch/m-p/551575#M153262</link>
      <description>&lt;P&gt;I'm trying to do something similar to another post, but the accepted answer didn't meet my requirements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We're working on a SAS macro to produce control chart data tables using PROC SHEWHART.&amp;nbsp; We need to&amp;nbsp;capture exceptions/errors, take appropriate action, and then continue with subsequent data steps/procedures.&amp;nbsp; In the example I've included below, we're simply changing from a p-Chart to a c-Chart when something causes the p-Chart to fail.&amp;nbsp; This specific example is where the denominator value is larger than the numerator; that is one known failure mode, but we don't know what other p-Charts rules could be violated.&amp;nbsp; Pre-testing for a specific failure mode, or failure modes, is not a desirable solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As expected, an exception, caught while the SYNTAXCHECK option is set, causes SAS to enter the special syntax check mode, and subsequent procedure calls within the macro fail due to empty data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a workaround where I temporarily set the NOSYNTAXCHECK system option immediately before the error-prone procedure call, and then set SYNTAXCHECK immediately following the exception handling code.&amp;nbsp;&amp;nbsp;In our environment we are supposed to keep the SYNTAXCHECK option active for the duration of the SAS session, so this is also an undesirable solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it possible to recover from "syntax check mode" after an error has occured with the SYNTAXCHECK system option set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would rather include a few lines of "reset" code within my exception-handling block and continue on.&amp;nbsp; I have set the OBS and REPLACE system options in my exception-handling block, but that doesn't appear to completely reverse the syntax check mode changes to the environment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mprintnest mlogic mlogicnest symbolgen spool syntaxcheck;
options msglevel=i notes source serror fullstimer errorcheck=normal;

%put NOTE: obs option : %sysfunc(getoption(obs,keyword));
%put NOTE: replace option : %sysfunc(getoption(replace));
%put NOTE: syntaxcheck option : %sysfunc(getoption(syntaxcheck));

data WORK.shewhart_data;
  input interval_month mmddyy10. numerator denominator;
  datalines;
07/01/2018 5 11
08/01/2018 14 9
09/01/2018 12 24
10/01/2018 19 27
11/01/2018 15 13
12/01/2018 20 34
01/01/2019 25 15
02/01/2019 22 31
03/01/2019 34 28
;

%macro test_shewhart(
  in_data=,
  out_data=
);
  proc shewhart data=&amp;amp;in_data;
    pchart numerator*interval_month
      / subgroupn=denominator sigmas=3 nochart outtable=&amp;amp;out_data;
  run;

  %if &amp;amp;syserr ne 0 %then
    %do;
      options obs=max replace; run; quit;

      %put NOTE: obs option : %sysfunc(getoption(obs,keyword));
      %put NOTE: replace option : %sysfunc(getoption(replace));
      %put NOTE: syntaxcheck option : %sysfunc(getoption(syntaxcheck));

      proc shewhart data=&amp;amp;in_data;
        cchart numerator*interval_month
          / sigmas=3 nochart outtable=&amp;amp;out_data;
      run;
    %end;

%mend test_shewhart;

%test_shewhart(
  in_data = WORK.shewhart_data,
  out_data = WORK.chart_data
)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've also attached the log with this code run from a brand new SAS session.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;BR /&gt;David&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 22:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exception-Handling-in-a-construct-like-try-catch/m-p/551575#M153262</guid>
      <dc:creator>DavidRice</dc:creator>
      <dc:date>2019-04-16T22:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Exception Handling in a construct like try/catch</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exception-Handling-in-a-construct-like-try-catch/m-p/551584#M153266</link>
      <description>&lt;P&gt;Doesn't look like you are resetting the SYNTAXCHECK option in your code that sets the OBS option.&lt;/P&gt;
&lt;P&gt;Or the ERRORCHECK option that it looks like EG (or what ever front end you are using) submitted in front of your code.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 23:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exception-Handling-in-a-construct-like-try-catch/m-p/551584#M153266</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-16T23:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: Exception Handling in a construct like try/catch</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exception-Handling-in-a-construct-like-try-catch/m-p/551587#M153268</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30742"&gt;@DavidRice&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that SYNTAXCHECK is for batch while for running in a Windowing mode (EG, SAS Studio, PC SAS) the option is&amp;nbsp;DMSSYNCHK.&lt;/P&gt;
&lt;P&gt;As for your code: Possibly also reset macro variable &amp;amp;SYSCC to zero to continue processing. That's what worked for me in the past.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The option statement is a global statement. You don't need a RUN;QUIT; after it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually: I'd be storing the value of &amp;amp;SYSCC in another macro var before your TRY block, set &amp;amp;syscc to zero in your CATCH logic but then reset it back to the saved value after the CATCH logic using the max value of the current &amp;amp;SYSCC value (=the max from the value you've saved away and the value you've got out of the CATCH block).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure what value &amp;amp;syserr will have if you've got already and error before your try/catch and you're already entering the TRY block in syntaxcheckmode. So eventually use &amp;amp;SYSCC instead of &amp;amp;SYSERR to decide if you'll enter the CATCH block at all.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 23:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exception-Handling-in-a-construct-like-try-catch/m-p/551587#M153268</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-04-16T23:34:33Z</dc:date>
    </item>
    <item>
      <title>Re: Exception Handling in a construct like try/catch</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exception-Handling-in-a-construct-like-try-catch/m-p/551590#M153270</link>
      <description>&lt;P&gt;In most production environments, a non-nil value for syserr is unacceptable.&lt;/P&gt;
&lt;P&gt;Since your&amp;nbsp;production environment seems to have strict rules in place, I would expect this to be true as well?&lt;/P&gt;
&lt;P&gt;In this case, the typical production program would:&lt;/P&gt;
&lt;P&gt;- Not alter error-checking options&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Pick the most appropriate plot type depending on the data&lt;/P&gt;
&lt;P&gt;- Fail if something unexpected (i.e. not catered for in the code) happens.&lt;BR /&gt;&amp;nbsp; This failure is desirable as it highlights an unexpected event that should be looked into.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; For example if there is no data, one wants to know about this abnormal event,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; or the program could be ready for this and display a "No data" report.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry, not much help about your specific question, just to bring a bit of (maybe irrelevant) context.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 23:29:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exception-Handling-in-a-construct-like-try-catch/m-p/551590#M153270</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-04-16T23:29:01Z</dc:date>
    </item>
  </channel>
</rss>

