<?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 Re: Error Handling in a construct like try/catch in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/539164#M148503</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Here is an example:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc HTTP
		URL = &amp;amp;QAUrl.
		Method = "get"
	  	Out = Resp; 
Run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;* Assign Libname for parsing ; &lt;BR /&gt;Libname in json "&amp;amp;WorkDir./Test.txt";&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;In an ideal world, if everything goes well, the above would work fine and I would parse the data into a dataset using something like the following:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data TestDetrails;
		set in.root; 
Run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;However, when something goes wrong, the error occurs when I try to assign a libname. So, I don't know what is the logical condition to test for here ? For e.g. I don't have a way of figuring out if a 400 code got returned in response to my incorrect api call or if my Test.txt file is empty. I hope that adds a little more color.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;I definitely follow and understand how I could use a logical test for error handling, here is an example, but I don't know what an appropriate logical test would be when you can have different errors rather than a definitive logical condition like a dataset existence ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%checkds(Work.ErrorLog); *Check if Data set exists;
%If &amp;amp;ExistTest = 1 %Then
	%Do; 
		%Let OutputFilePath = &amp;amp;OutputFilePath&amp;amp;FileSeperator&amp;amp;MyDate&amp;amp;SpaceSeperator&amp;amp;MyTime&amp;amp;MyFile; 
		%Put &amp;amp;OutputFilePath; 
		Proc Export Data = Work.errorlog
			 Outfile = "&amp;amp;OutputFilePath."
			 Dbms = csv
			 Replace;
		Run;
	%End; 
	
Data _null_;
	Stop; 
Run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Feb 2019 22:32:02 GMT</pubDate>
    <dc:creator>UdayGuntupalli</dc:creator>
    <dc:date>2019-02-27T22:32:02Z</dc:date>
    <item>
      <title>Error Handling in a construct like try/catch</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/539151#M148496</link>
      <description>&lt;P&gt;All,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;I am trying to find an example of how to perform error handling in SAS with a construct similar to try/catch. I understand Base SAS does not offer the try/catch construct. However, I am wondering if there is a simple way to mimic this? I have an internal api that I call with a custom made macro - which works fine, however I would like to build some error handling capability where I can say some thing like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Try 
   %MyApiCall(); 
Catch 
    %writetolog(); &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;I am more familiar with JMP ( a SAS product) and here is how similar construct is achieved in JMP :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Try( 
	If( Random Uniform() &amp;lt; 0.5, 
		1, 
		Throw() 
	  ), 
	"thrown" 
   );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Feb 2019 22:00:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/539151#M148496</guid>
      <dc:creator>UdayGuntupalli</dc:creator>
      <dc:date>2019-02-27T22:00:54Z</dc:date>
    </item>
    <item>
      <title>Re: Error Handling in a construct like try/catch</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/539158#M148499</link>
      <description>&lt;P&gt;There isn't enough detail in your question to provide a specific answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'd handle it with SAS macro logic most likely.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a contrived example, where there's conditional execution of a procedure:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=p011imau3tm4jen1us2a45cyenz9.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=p011imau3tm4jen1us2a45cyenz9.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or it could be as simple as conditional logic within a data step.&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/27543"&gt;@UdayGuntupalli&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;All,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;I am trying to find an example of how to perform error handling in SAS with a construct similar to try/catch. I understand Base SAS does not offer the try/catch construct. However, I am wondering if there is a simple way to mimic this? I have an internal api that I call with a custom made macro - which works fine, however I would like to build some error handling capability where I can say some thing like this:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Try 
   %MyApiCall(); 
Catch 
    %writetolog(); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;I am more familiar with JMP ( a SAS product) and here is how similar construct is achieved in JMP :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Try( 
	If( Random Uniform() &amp;lt; 0.5, 
		1, 
		Throw() 
	  ), 
	"thrown" 
   );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 22:06:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/539158#M148499</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-27T22:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: Error Handling in a construct like try/catch</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/539164#M148503</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Here is an example:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc HTTP
		URL = &amp;amp;QAUrl.
		Method = "get"
	  	Out = Resp; 
Run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;* Assign Libname for parsing ; &lt;BR /&gt;Libname in json "&amp;amp;WorkDir./Test.txt";&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;In an ideal world, if everything goes well, the above would work fine and I would parse the data into a dataset using something like the following:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data TestDetrails;
		set in.root; 
Run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;However, when something goes wrong, the error occurs when I try to assign a libname. So, I don't know what is the logical condition to test for here ? For e.g. I don't have a way of figuring out if a 400 code got returned in response to my incorrect api call or if my Test.txt file is empty. I hope that adds a little more color.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;I definitely follow and understand how I could use a logical test for error handling, here is an example, but I don't know what an appropriate logical test would be when you can have different errors rather than a definitive logical condition like a dataset existence ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%checkds(Work.ErrorLog); *Check if Data set exists;
%If &amp;amp;ExistTest = 1 %Then
	%Do; 
		%Let OutputFilePath = &amp;amp;OutputFilePath&amp;amp;FileSeperator&amp;amp;MyDate&amp;amp;SpaceSeperator&amp;amp;MyTime&amp;amp;MyFile; 
		%Put &amp;amp;OutputFilePath; 
		Proc Export Data = Work.errorlog
			 Outfile = "&amp;amp;OutputFilePath."
			 Dbms = csv
			 Replace;
		Run;
	%End; 
	
Data _null_;
	Stop; 
Run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 22:32:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/539164#M148503</guid>
      <dc:creator>UdayGuntupalli</dc:creator>
      <dc:date>2019-02-27T22:32:02Z</dc:date>
    </item>
    <item>
      <title>Re: Error Handling in a construct like try/catch</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/539177#M148511</link>
      <description>&lt;P&gt;Ah, you want to use the automatic macro variables in some of these cases.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n1wrevo4roqsnxn1fbd9yezxvv9k.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n1wrevo4roqsnxn1fbd9yezxvv9k.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, SYSERR will contain a different value if a proc errors out, versus if it completes successfully. In the example below, you get 3000 if it fails and 0 if it is successful. Then you can use that macro variable in the conditional logic to execute your code conditionally based on the macro variables values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=sashelp.class;
class sex;
var wieght;
run;

%put &amp;amp;syserr;

proc means data=sashelp.class;
class sex;
var weight;
run;

%put &amp;amp;syserr;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;208  proc means data=sashelp.class;
209  class sex;
210  var wieght;
ERROR: Variable WIEGHT not found.
211  run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds

212
213  %put &amp;amp;syserr;
&lt;FONT size="4" color="#FF0000"&gt;&lt;STRONG&gt;3000&lt;/STRONG&gt;&lt;/FONT&gt;
214


215  proc means data=sashelp.class;
216  class sex;
217  var weight;
218  run;

NOTE: Writing HTML Body file: sashtml.htm
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.62 seconds
      cpu time            0.20 seconds


219
220  %put &amp;amp;syserr;
&lt;FONT size="4" color="#FF0000"&gt;&lt;STRONG&gt;0
&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Feb 2019 22:58:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/539177#M148511</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-27T22:58:50Z</dc:date>
    </item>
    <item>
      <title>Re: Error Handling in a construct like try/catch</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/670166#M201137</link>
      <description>&lt;P&gt;It's been months, but I figured I'd drop this here anyway.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you've narrowed the source of any problems down to the libname statement, use the libname function instead, and check the return code.&lt;/P&gt;&lt;P&gt;Documentation with examples:&lt;/P&gt;&lt;P&gt;&lt;A href="https://bit.ly/397oFMr" target="_blank"&gt;https://bit.ly/397oFMr&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH,&lt;/P&gt;&lt;P&gt;Rebekah&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jul 2020 17:06:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/m-p/670166#M201137</guid>
      <dc:creator>rlandbeck</dc:creator>
      <dc:date>2020-07-17T17:06:19Z</dc:date>
    </item>
  </channel>
</rss>

