<?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 Macro to Retrieve API Data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Retrieve-API-Data/m-p/567064#M159422</link>
    <description>&lt;P&gt;I am trying to write a macro that will potentially pull 2 datasets from an api, join them and append to an existing dataset. I am trying to use some conditional logic because the second dataset is not available for all values, so if the second one doesn't exist, just append the first without attempting a join.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data ndc;&lt;BR /&gt;input ndc $11.;&lt;BR /&gt;datalines;&lt;BR /&gt;00002140701&lt;BR /&gt;00002143361&lt;BR /&gt;00000000000&lt;BR /&gt;00000000005&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ndc;
input ndc $11.;
datalines;
00002140701
00002143361
00000000000
00000000005
;
run;

%macro Query_RxNav_API(ndc=);
filename resp 'location/demo.json';
options SSLCALISTLOC="locations/trustedcerts.pem";

proc http
method="GET"
url="https://rxnav.nlm.nih.gov/REST/ndcstatus.json?ndc=&amp;amp;ndc"
out=resp;
run;

libname myfiles json 'location/demo.json';

data ndcstatus;
set myfiles.ndcstatus;
run;

%if %sysfunc(exist(myfiles.ndcstatus_ndchistory))=1 %then %do;

data ndchistory;
set myfiles.ndcstatus_ndchistory;
run;

proc sql;
create table ndc_join as
	select a.*,
		   b.*
	from ndcstatus a left join ndchistory b on
		 (a.ordinal_root=b.ordinal_ndcStatus)
;
quit;

proc append base=support.ndc_to_rxcui data=ndc_join force;
run;


%end;

%else %do;
proc append base=support.ndc_to_rxcui data=ndcstatus force;
run;

%end;
%mend Query_RxNav_API;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data macro_call;
set ndc;

str=catt('%Query_RxNav_API(ndc=',ndc, ');');

call execute(str);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run this macro I get the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: File MYFILES.NDCSTATUS_NDCHISTORY.DATA does not exist.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My logic seems to be failing at the if statement, because if that file doesn't exist it shouldn't need to use it, right? I have also tried the below syntax and received a similar error.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %sysfunc(exist(myfiles.ndcstatus_ndchistory)) %then %do;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any idea what is causing my logic to to skip the check of the file that doesn't exist?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Jun 2019 20:40:17 GMT</pubDate>
    <dc:creator>A_SAS_Man</dc:creator>
    <dc:date>2019-06-18T20:40:17Z</dc:date>
    <item>
      <title>Macro to Retrieve API Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Retrieve-API-Data/m-p/567064#M159422</link>
      <description>&lt;P&gt;I am trying to write a macro that will potentially pull 2 datasets from an api, join them and append to an existing dataset. I am trying to use some conditional logic because the second dataset is not available for all values, so if the second one doesn't exist, just append the first without attempting a join.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data ndc;&lt;BR /&gt;input ndc $11.;&lt;BR /&gt;datalines;&lt;BR /&gt;00002140701&lt;BR /&gt;00002143361&lt;BR /&gt;00000000000&lt;BR /&gt;00000000005&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ndc;
input ndc $11.;
datalines;
00002140701
00002143361
00000000000
00000000005
;
run;

%macro Query_RxNav_API(ndc=);
filename resp 'location/demo.json';
options SSLCALISTLOC="locations/trustedcerts.pem";

proc http
method="GET"
url="https://rxnav.nlm.nih.gov/REST/ndcstatus.json?ndc=&amp;amp;ndc"
out=resp;
run;

libname myfiles json 'location/demo.json';

data ndcstatus;
set myfiles.ndcstatus;
run;

%if %sysfunc(exist(myfiles.ndcstatus_ndchistory))=1 %then %do;

data ndchistory;
set myfiles.ndcstatus_ndchistory;
run;

proc sql;
create table ndc_join as
	select a.*,
		   b.*
	from ndcstatus a left join ndchistory b on
		 (a.ordinal_root=b.ordinal_ndcStatus)
;
quit;

proc append base=support.ndc_to_rxcui data=ndc_join force;
run;


%end;

%else %do;
proc append base=support.ndc_to_rxcui data=ndcstatus force;
run;

%end;
%mend Query_RxNav_API;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data macro_call;
set ndc;

str=catt('%Query_RxNav_API(ndc=',ndc, ');');

call execute(str);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run this macro I get the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: File MYFILES.NDCSTATUS_NDCHISTORY.DATA does not exist.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My logic seems to be failing at the if statement, because if that file doesn't exist it shouldn't need to use it, right? I have also tried the below syntax and received a similar error.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %sysfunc(exist(myfiles.ndcstatus_ndchistory)) %then %do;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any idea what is causing my logic to to skip the check of the file that doesn't exist?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 20:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Retrieve-API-Data/m-p/567064#M159422</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2019-06-18T20:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Retrieve API Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Retrieve-API-Data/m-p/567068#M159423</link>
      <description>&lt;P&gt;You're falling victim to macro timing issues when using call execute. Expand your command for call execute:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;str=catt('%nrstr(%Query_RxNav_API(ndc=',ndc, '));');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Jun 2019 20:54:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Retrieve-API-Data/m-p/567068#M159423</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-18T20:54:20Z</dc:date>
    </item>
  </channel>
</rss>

