<?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: Libname error trapping in a macro when the connection is to a database. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807444#M81750</link>
    <description>&lt;P&gt;The same way you would put any macro variable value into a dataset variable.&lt;/P&gt;
&lt;P&gt;For example just expanding the value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;numvar = &amp;amp;digitstring;
charvar= "&amp;amp;charstring";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or using function call.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;numvar=symgetn('digitstring');
charvar=symget('charstring');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have the list of values in a data set you might be better off ditching the macro a see if you can just the LIBNAME() function in a single data step to test all of the different values are once. That would be a lot simpler than trying get macro code and macro variables to work.&amp;nbsp; Something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  rc = libname(......);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Check the documentation on the LIBNAME() function to work out the details.&amp;nbsp; You could probable use the SYMGET() function to return the values of the automatic macro variables after each LIBNAME() function call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 12 Apr 2022 18:19:47 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-04-12T18:19:47Z</dc:date>
    <item>
      <title>Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807417#M81746</link>
      <description>&lt;P&gt;Hello Forum -&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to trap any potential libname errors that occur inside a macro, and make them available to the remainder of my SAS program (so that I can output results of connectivity test.).&amp;nbsp; Below is my code.&amp;nbsp; How can I modify it to accomplish this?&amp;nbsp; I have seen this post, but it did not help:&amp;nbsp;&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/LIBNAME-error-trapping/m-p/238132" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Procedures/LIBNAME-error-trapping/m-p/238132&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please, and Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO test_connection();

%DO I = 1 %TO &amp;amp;IDS;

%IF "&amp;amp;&amp;amp;type&amp;amp;I."  EQ "DB2" /* success*/
%THEN
     %DO;

LIBNAME DB2TST DB2 DATAsrc="&amp;amp;&amp;amp;datasrc&amp;amp;i." USER="&amp;amp;&amp;amp;user&amp;amp;i." PASSWORD="&amp;amp;&amp;amp;pwd&amp;amp;i." SCHEMA="&amp;amp;&amp;amp;schema&amp;amp;i." access=readonly;

/* Trap error here.
What code can trap an error, if it occurs, and make it available to the rest of the SAS program?  */

     %END;

%END;

%RUN;
%MEND test_connection;

%test_connection();&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Apr 2022 15:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807417#M81746</guid>
      <dc:creator>Smurfy</dc:creator>
      <dc:date>2022-04-12T15:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807430#M81747</link>
      <description>&lt;P&gt;Set macro variable &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p11nt7mv7k9hl4n1x9zwkralgq1b.htm" target="_blank" rel="noopener"&gt;SYSCC&lt;/A&gt; to 0 before you issue the libname, and check it after. A non-zero value indicates a problem.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 16:27:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807430#M81747</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-12T16:27:51Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807433#M81748</link>
      <description>&lt;P&gt;Check the automatic macro variable SYSLIBRC.&lt;/P&gt;
&lt;P&gt;And for relational databases SYSDBMSG and SYSDBRC.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;429   libname xx  oracle '||||';
ERROR: Incorrect syntax for this LIBNAME statement. This engine does not accept a physical name.
ERROR: Error in the LIBNAME statement.
430   %put &amp;amp;=syslibrc ;
SYSLIBRC=5780013
431   %put &amp;amp;=sysdbmsg;
SYSDBMSG=ORACLE: ERROR: Incorrect syntax for this LIBNAME statement. This engine does not accept a physical name.
432   %put &amp;amp;sysdbrc;
-1
&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Apr 2022 17:19:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807433#M81748</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-12T17:19:35Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807441#M81749</link>
      <description>Thank you! But how to I get it into a variable and available to the rest of the SAS program (not just a put to the log)? I ultimately need to output failures to an excel sheet...</description>
      <pubDate>Tue, 12 Apr 2022 18:11:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807441#M81749</guid>
      <dc:creator>Smurfy</dc:creator>
      <dc:date>2022-04-12T18:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807444#M81750</link>
      <description>&lt;P&gt;The same way you would put any macro variable value into a dataset variable.&lt;/P&gt;
&lt;P&gt;For example just expanding the value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;numvar = &amp;amp;digitstring;
charvar= "&amp;amp;charstring";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or using function call.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;numvar=symgetn('digitstring');
charvar=symget('charstring');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have the list of values in a data set you might be better off ditching the macro a see if you can just the LIBNAME() function in a single data step to test all of the different values are once. That would be a lot simpler than trying get macro code and macro variables to work.&amp;nbsp; Something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  rc = libname(......);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Check the documentation on the LIBNAME() function to work out the details.&amp;nbsp; You could probable use the SYMGET() function to return the values of the automatic macro variables after each LIBNAME() function call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 18:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807444#M81750</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-12T18:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807469#M81751</link>
      <description>&lt;P&gt;These are the coding changes I made, but I my libstat&amp;amp;I isn't being assigned anything...what could possible sytax corrections be for this line:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=""&gt;%let libstat&amp;amp;I = syslibrc;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=""&gt;x=symget("libstat"|| cat(I)); -&amp;nbsp;x&amp;nbsp;has&amp;nbsp;nothing&amp;nbsp;in&amp;nbsp;it.&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO test_connection();

%global libstat&amp;amp;I;

%DO I = 1 %TO &amp;amp;IDS;

%IF "&amp;amp;&amp;amp;type&amp;amp;I."  EQ "DB2" /* success*/
%THEN
     %DO;

LIBNAME DB2TST DB2 DATAsrc="&amp;amp;&amp;amp;datasrc&amp;amp;i." USER="&amp;amp;&amp;amp;user&amp;amp;i." PASSWORD="&amp;amp;&amp;amp;pwd&amp;amp;i." SCHEMA="&amp;amp;&amp;amp;schema&amp;amp;i." access=readonly;

/* Trap error here.
What code can trap an error, if it occurs, and make it available to the rest of the SAS program?  */

%put &amp;amp;=syslibrc;
%let libstat&amp;amp;I = syslibrc;


     %END;

%END;

%RUN;
%MEND test_connection;

%test_connection();

data work.fail;
  do i=1 to &amp;amp;ids;
     x=symget("libstat"|| cat(I));
     if x ^=0 then output;
  end;
run;

Proc export data=work.fail;
  outfile="/mydir/fail.xlsx"
  dbms=XLSX replace;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Apr 2022 19:25:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807469#M81751</guid>
      <dc:creator>Smurfy</dc:creator>
      <dc:date>2022-04-12T19:25:07Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807471#M81752</link>
      <description>&lt;P&gt;You made LOCAL macro variables and then tried to read them after they were already gone because the macro had finished running.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 19:31:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807471#M81752</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-12T19:31:25Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807472#M81753</link>
      <description>&lt;P&gt;Ok - So I moved my global var definition downwhere I am doing the libstat&amp;amp;I assignment, and now it works.&amp;nbsp; Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO test_connection();



%DO I = 1 %TO &amp;amp;IDS;

%IF "&amp;amp;&amp;amp;type&amp;amp;I."  EQ "DB2" /* success*/
%THEN
     %DO;

LIBNAME DB2TST DB2 DATAsrc="&amp;amp;&amp;amp;datasrc&amp;amp;i." USER="&amp;amp;&amp;amp;user&amp;amp;i." PASSWORD="&amp;amp;&amp;amp;pwd&amp;amp;i." SCHEMA="&amp;amp;&amp;amp;schema&amp;amp;i." access=readonly;

/* Trap error here.
What code can trap an error, if it occurs, and make it available to the rest of the SAS program?  */

%put &amp;amp;=syslibrc;
%global libstat&amp;amp;I;
%let libstat&amp;amp;I = syslibrc;


     %END;

%END;

%RUN;
%MEND test_connection;

%test_connection();

data work.fail;
  do i=1 to &amp;amp;ids;
     x=symget("libstat"|| cat(I));
     if x ^=0 then output;
  end;
run;

Proc export data=work.fail;
  outfile="/mydir/fail.xlsx"
  dbms=XLSX replace;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Apr 2022 19:40:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807472#M81753</guid>
      <dc:creator>Smurfy</dc:creator>
      <dc:date>2022-04-12T19:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807474#M81754</link>
      <description>&lt;P&gt;Sounds good.&lt;/P&gt;
&lt;P&gt;It is still going to be a LOOOOOOOT easier to skip the macro code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;LIBNAME DB2TST DB2 DATAsrc="&amp;amp;&amp;amp;datasrc&amp;amp;i." USER="&amp;amp;&amp;amp;user&amp;amp;i." PASSWORD="&amp;amp;&amp;amp;pwd&amp;amp;i." SCHEMA="&amp;amp;&amp;amp;schema&amp;amp;i." access=readonly;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From the names of the macro variables it looks like you started with a DATASET that had variables name DATASRC, USER , PWD and SCHEMA.&amp;nbsp; So just use that dataset directly to generate the LIBNAME() function call and then save the return code.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 19:48:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807474#M81754</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-12T19:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807482#M81755</link>
      <description>Can't - I am doing a mass connection test. I need the macro to loop thru the input file that contains many database types, userids, passwords, schemas, etc.</description>
      <pubDate>Tue, 12 Apr 2022 20:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807482#M81755</guid>
      <dc:creator>Smurfy</dc:creator>
      <dc:date>2022-04-12T20:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Libname error trapping in a macro when the connection is to a database.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807490#M81756</link>
      <description>&lt;P&gt;You can do all of that, including the looping, in a DATA step using the LIBNAME function and avoid a macro loop altogether.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 20:14:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Libname-error-trapping-in-a-macro-when-the-connection-is-to-a/m-p/807490#M81756</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-04-12T20:14:48Z</dc:date>
    </item>
  </channel>
</rss>

