<?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: Census API uses '&amp;amp;' parameter - how to work around in a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791343#M253456</link>
    <description>&lt;P&gt;Tom:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&amp;nbsp; as always for your insight.&amp;nbsp; I am using your suggestion and working with the following code, but there are two issues, which hopefully have simple resolutions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, I am manually changing the value for I_COMMODITY from '12*' to '13*' to '14*', etc. I want to get a macro to do the work for me.&amp;nbsp; Let's say the I_Commodity values is a list from '12*' to '13*' and up to 98* (by 1).&amp;nbsp; I then want to repeat those I_COMMODITY values again for the next CTY_CODE value (which is also a list) '1220', to '1221' to 1222' etc... Then, of course run those all again for a series of 7 time values (i.e., 2021-11, then 2020-11, then 2020-12, 2019-12, etc)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, the root file has the variable names as ordinal_root, element1, element2, etc.&amp;nbsp; The first row has the actual variable names I want to use, "YEAR", "MONTH", "RP", "CTY_CODE", etc.... which are the field names I am "GET"ting.&amp;nbsp; How do I get the 1st row values to be the variable names rather than an observation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DM 'CLEAR LOG; CLEAR OUTPUT'; RESETLINE;
options ExtendObsCounter=no;

LIBNAME COMPANY2 'W:\SAS Data\';        

data _null_;
  time='2021-11';
  CTY_CODE='1220';
  I_COMMODITY='16*';
  length url $500;
  url = cats('https://api.census.gov/data/timeseries/intltrade/imports/hs'
            ,'?get=YEAR,MONTH,RP,CTY_CODE,CTY_SUBCODE,DISTRICT,DIST_NAME'
            ,',CON_QY1_MO,CON_QY1_YR,I_COMMODITY,I_COMMODITY_SDESC'
            ,',I_COMMODITY_LDESC,CON_VAL_MO,CON_VAL_YR,CON_CHA_MO,CON_CHA_YR'
            ,',GEN_VAL_MO,CAL_DUT_MO,CAL_DUT_YR'
            ,'&amp;amp;SUMMARY_LVL2=HSCYCSDTRP'
            ,'&amp;amp;COMM_LVL=HS10'
            ,'&amp;amp;time=',time
            ,'&amp;amp;CTY_CODE=',cty_code
            ,'&amp;amp;I_COMMODITY=',i_commodity
            );
   rc=filename('in',url,'url');
run;

filename json temp;
%let rc=%sysfunc(fcopy(in,json));
libname json json;
&lt;BR /&gt;proc copy inlib=json out=work; 
run;

proc append base=company2.impdetl data=ROOT force;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 21 Jan 2022 04:54:21 GMT</pubDate>
    <dc:creator>texasmfp</dc:creator>
    <dc:date>2022-01-21T04:54:21Z</dc:date>
    <item>
      <title>Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791336#M253452</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to develop code to download the full set of Census import data each month.&amp;nbsp; Unfortunately, Census limits the volume of data that can be called via the API.&amp;nbsp; As a result, that means I will need to run the code a few thousand times, substituting a few of the parameters with each iteration.&amp;nbsp; However, the Census API uses the '&amp;amp;' to define the parameters.&amp;nbsp; I am hoping to create a macro that will minimize the required code by doing the substituting for me.&amp;nbsp; Obviously, SAS's macro '&amp;amp;' conflicts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DM 'CLEAR LOG; CLEAR OUTPUT'; RESETLINE;
options ExtendObsCounter=no
NOQUOTELENMAX;

LIBNAME COMPANY2 'W:\SAS Data\';        

filename in url 'https://api.census.gov/data/timeseries/intltrade/imports/hs?get=YEAR,MONTH,RP,CTY_CODE,CTY_SUBCODE,DISTRICT,DIST_NAME,CON_QY1_MO,CON_QY1_YR,I_COMMODITY,I_COMMODITY_SDESC,I_COMMODITY_LDESC,CON_VAL_MO,CON_VAL_YR,CON_CHA_MO,CON_CHA_YR,
GEN_VAL_MO,CAL_DUT_MO,CAL_DUT_YR
&amp;amp;SUMMARY_LVL2=HSCYCSDTRP&amp;amp;COMM_LVL=HS10&amp;amp;time=2021-11&amp;amp;CTY_CODE=1220&amp;amp;I_COMMODITY=12*';

libname in json;

data COMPANY2.bingo_out;
set in.root;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I will need to change the saved filename (currently &lt;FONT color="#FF0000"&gt;bingo_out&lt;/FONT&gt;) as well as the values for three variables:&lt;/P&gt;
&lt;P&gt;&amp;amp;time=&lt;FONT color="#FF0000"&gt;2021-11&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;amp;CTY_CODE=&lt;FONT color="#FF0000"&gt;1220&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;amp;I_COMMODITY=&lt;FONT color="#FF0000"&gt;12*&lt;/FONT&gt;';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 03:10:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791336#M253452</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2022-01-21T03:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791341#M253455</link>
      <description>&lt;P&gt;Since it looks like you are already generating some SAS code (FILENAME statement) then do it with a DATA step where you can work with the &amp;amp; in an actual variable instead of a macro variable. Protecting macro triggers will be much&amp;nbsp;easier in SAS code than macro code.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So build the string using data step code, like CATS() for example.&amp;nbsp; And then either use it directly to make the FILEREF.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  time='2021-11';
  CTY_CODE='1220';
  I_COMMODITY='12*';
  length url $500;
  url = cats('https://api.census.gov/data/timeseries/intltrade/imports/hs'
            ,'?get=YEAR,MONTH,RP,CTY_CODE,CTY_SUBCODE,DISTRICT,DIST_NAME'
            ,',CON_QY1_MO,CON_QY1_YR,I_COMMODITY,I_COMMODITY_SDESC'
            ,',I_COMMODITY_LDESC,CON_VAL_MO,CON_VAL_YR,CON_CHA_MO,CON_CHA_YR'
            ,',GEN_VAL_MO,CAL_DUT_MO,CAL_DUT_YR'
            ,'&amp;amp;SUMMARY_LVL2=HSCYCSDTRP'
            ,'&amp;amp;COMM_LVL=HS10'
            ,'&amp;amp;time=',time
            ,'&amp;amp;CTY_CODE=',cty_code
            ,'&amp;amp;I_COMMODITY=',i_commodity
            );
   rc=filename('in',url,'url');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or create a macro variable with value inside of single quotes where the macro processor will ignore the &amp;amp; and % characters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;....
 call symputx('url',quote(trim(url),"'"));
run;
libname in url &amp;amp;url ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you will find that you will need to copy the result of your URL to an actual physical file to get the JSON libref engine to work with it.&amp;nbsp; Something like this might work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename json temp;
%let rc=%sysfunc(fcopy(in,json));
libname json json;
proc copy inlib=json out=work; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it might be easier to use PROC HTTP to copy the file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 04:12:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791341#M253455</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-21T04:12:32Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791343#M253456</link>
      <description>&lt;P&gt;Tom:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&amp;nbsp; as always for your insight.&amp;nbsp; I am using your suggestion and working with the following code, but there are two issues, which hopefully have simple resolutions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, I am manually changing the value for I_COMMODITY from '12*' to '13*' to '14*', etc. I want to get a macro to do the work for me.&amp;nbsp; Let's say the I_Commodity values is a list from '12*' to '13*' and up to 98* (by 1).&amp;nbsp; I then want to repeat those I_COMMODITY values again for the next CTY_CODE value (which is also a list) '1220', to '1221' to 1222' etc... Then, of course run those all again for a series of 7 time values (i.e., 2021-11, then 2020-11, then 2020-12, 2019-12, etc)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, the root file has the variable names as ordinal_root, element1, element2, etc.&amp;nbsp; The first row has the actual variable names I want to use, "YEAR", "MONTH", "RP", "CTY_CODE", etc.... which are the field names I am "GET"ting.&amp;nbsp; How do I get the 1st row values to be the variable names rather than an observation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DM 'CLEAR LOG; CLEAR OUTPUT'; RESETLINE;
options ExtendObsCounter=no;

LIBNAME COMPANY2 'W:\SAS Data\';        

data _null_;
  time='2021-11';
  CTY_CODE='1220';
  I_COMMODITY='16*';
  length url $500;
  url = cats('https://api.census.gov/data/timeseries/intltrade/imports/hs'
            ,'?get=YEAR,MONTH,RP,CTY_CODE,CTY_SUBCODE,DISTRICT,DIST_NAME'
            ,',CON_QY1_MO,CON_QY1_YR,I_COMMODITY,I_COMMODITY_SDESC'
            ,',I_COMMODITY_LDESC,CON_VAL_MO,CON_VAL_YR,CON_CHA_MO,CON_CHA_YR'
            ,',GEN_VAL_MO,CAL_DUT_MO,CAL_DUT_YR'
            ,'&amp;amp;SUMMARY_LVL2=HSCYCSDTRP'
            ,'&amp;amp;COMM_LVL=HS10'
            ,'&amp;amp;time=',time
            ,'&amp;amp;CTY_CODE=',cty_code
            ,'&amp;amp;I_COMMODITY=',i_commodity
            );
   rc=filename('in',url,'url');
run;

filename json temp;
%let rc=%sysfunc(fcopy(in,json));
libname json json;
&lt;BR /&gt;proc copy inlib=json out=work; 
run;

proc append base=company2.impdetl data=ROOT force;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 04:54:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791343#M253456</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2022-01-21T04:54:21Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791350#M253460</link>
      <description>&lt;P&gt;I suggest to use Proc HTTP with the QUERY option to download what you need. See the code below for an example. Using the query option it is quite easy to provide name=value pairs in the url.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let time = 2021-11;
%let cty_code = 1220;
%let I_commodity = 12*;
filename resp temp;

proc http
  method="get"
  url='https://api.census.gov/data/timeseries/intltrade/imports/hs'
  query=(
    "get" = "YEAR,MONTH,RP,CTY_CODE,CTY_SUBCODE,DISTRICT,DIST_NAME,CON_QY1_MO,CON_QY1_YR,I_COMMODITY,I_COMMODITY_SDESC,I_COMMODITY_LDESC,CON_VAL_MO,CON_VAL_YR,CON_CHA_MO,CON_CHA_YR,GEN_VAL_MO,CAL_DUT_MO,CAL_DUT_YR"
    "SUMMARY_LVL2" = "HSCYCSDTRP"
    "COMM_LVL" = "HS10"
    "time"="&amp;amp;time"
    "CTY_CODE" = "&amp;amp;CTY_CODE"
    "I_COMMODITY" = "&amp;amp;I_COMMODITY"
  )
  verbose
  out=resp
;
run;

libname resp json;

proc copy in=resp out=work;
run;

libname resp clear;
filename resp clear;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Jan 2022 07:45:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791350#M253460</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2022-01-21T07:45:55Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791412#M253482</link>
      <description>&lt;P&gt;Bruno:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks but I get a Syntax Error using (my mod) of your code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_examples(period,country,product);

%let time = &amp;amp;period;
%let cty_code = &amp;amp;country;
%let I_commodity = &amp;amp;product*;
filename resp temp;

proc http
  method="get"
  url='https://api.census.gov/data/timeseries/intltrade/imports/hs'
  query=(
    "get" = "YEAR,MONTH,RP,CTY_CODE,CTY_SUBCODE,DISTRICT,DIST_NAME,CON_QY1_MO,CON_QY1_YR,I_COMMODITY,I_COMMODITY_SDESC,I_COMMODITY_LDESC,CON_VAL_MO,CON_VAL_YR,CON_CHA_MO,CON_CHA_YR,GEN_VAL_MO,CAL_DUT_MO,CAL_DUT_YR"
    "SUMMARY_LVL2" = "HSCYCSDTRP"
    "COMM_LVL" = "HS10"
    "time"="&amp;amp;time"
    "CTY_CODE" = "&amp;amp;CTY_CODE"
    "I_COMMODITY" = "&amp;amp;I_COMMODITY"
  )
  verbose
  out=resp
;
run;

libname resp json;

proc copy in=resp out=work;
run;

libname resp clear;
filename resp clear;

proc append force base=impdetl2 data=ROOT;
run;

%mend get_examples;
%get_examples(2021-11,*,0101)
%get_examples(2021-11,*,0102)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;44   %get_examples(2021-11,*,0101)
NOTE: Line generated by the invoked macro "GET_EXAMPLES".
1                          proc http   method="get"   url='https://api.census.gov/data/timeseries/intltrade/imports/hs'
1  ! query=(     "get" =
     -----
     22
WARNING: Apparent symbolic reference KEY not resolved.
NOTE: Line generated by the invoked macro "GET_EXAMPLES".
1     filename resp temp;  proc http   method="get"   url='https://api.census.gov/data/timeseries/intltrade/imports/hs'
1  ! query=(     "get" =
     -----
     76
ERROR 22-322: Syntax error, expecting one of the following: ;, AUTH_ANY, AUTH_BASIC, AUTH_NEGOTIATE, AUTH_NONE, AUTH_NTLM,
              CLEAR_CACHE, CLEAR_CONN_CACHE, CLEAR_COOKIES, CT, EXPECT_100_CONTINUE, FOLLOWLOC, HEADERIN, HEADEROUT,
              HEADEROUT_OVERWRITE, HTTP_TOKENAUTH, IN, METHOD, NOFOLLOW, NOFOLLOWLOC, NO_CONN_CACHE, NO_COOKIES, OAUTH_BEARER,
              OUT, PASSWORD, PROXYHOST, PROXYPASSWORD, PROXYPORT, PROXYUSERNAME, PROXY_AUTH_BASIC, PROXY_AUTH_NEGOTIATE,
              PROXY_AUTH_NONE, PROXY_AUTH_NTLM, TIMEOUT, URL, USERNAME, VERBOSE, WEBAUTHDOMAIN, WEBPASSWORD, WEBUSERNAME.

ERROR 76-322: Syntax error, statement will be ignored.


WARNING: RUN statement ignored due to previous errors. Submit QUIT; to terminate the procedure.
NOTE: PROCEDURE HTTP used (Total process time):
      real time           0.04 seconds
      cpu time            0.03 seconds

NOTE: The SAS System stopped processing this step because of errors.
NOTE: JSON data is only read once.  To read the JSON again, reassign the JSON LIBNAME.
ERROR: Physical file does not exist, e:\SAS Temporary Files\_TD19132_DESKTOP-575EP3T_\#LN00054.
ERROR: Error in the LIBNAME statement.



ERROR: Libref RESP is not assigned.
NOTE: Statements not processed because of errors noted above.
NOTE: PROCEDURE COPY used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

NOTE: The SAS System stopped processing this step because of errors.
WARNING: Libref RESP is not assigned.
NOTE: Fileref RESP has been deassigned.


ERROR: File WORK.ROOT.DATA does not exist.

NOTE: Statements not processed because of errors noted above.
NOTE: The data set WORK.IMPDETL2 has 0 observations and 0 variables.
NOTE: PROCEDURE APPEND used (Total process time):
      real time           0.02 seconds
      cpu time            0.03 seconds

NOTE: The SAS System stopped processing this step because of errors.
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Jan 2022 13:13:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791412#M253482</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2022-01-21T13:13:11Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791422#M253489</link>
      <description>&lt;P&gt;What version of SAS are you using?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The doc says this about the QUERY option:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The QUERY= option is available beginning with&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="xisDoc-codeFocus"&gt;SAS Viya 3.5&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;and the&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="xisDoc-codeFocus"&gt;November 2019 release of SAS 9.4M6&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="xisDoc-codeFocus"&gt;So might have to do with this.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 13:41:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791422#M253489</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2022-01-21T13:41:51Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791445#M253504</link>
      <description>&lt;P&gt;Again using SAS code is much easier than macro code.&lt;/P&gt;
&lt;P&gt;Since you appear to want to transfer many of these files setup a macro that allows you to transfer one of them.&lt;/P&gt;
&lt;P&gt;Perhaps something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_census(url,out);
...
filename in url &amp;amp;url;
....
data &amp;amp;out;
 ....
run;
%mend ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use a data step to do your looping and generate the URL needed and then use CALL EXECUTE() or just PUT statement into a text file to generate a series of macro calls.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data census;
  start='01NOV2020'd;
  format start date9.;
  do offset=0 to 6;
     time=put(intnx('month',start,offset),yymm7.);
     do COMMODITY=12 to 16;
       I_COMMODITY=cats(COMMODITY,'*');
       do cty_code='1220','1221','1245' ;
          dsn=catx('_','census',time,commodity,cty_code);
          output;
       end;
     end;
   end;
run;

data _null_;
  set census;
  length url $500;
  url = cats('https://api.census.gov/data/timeseries/intltrade/imports/hs'
            ,'?get=YEAR,MONTH,RP,CTY_CODE,CTY_SUBCODE,DISTRICT,DIST_NAME'
            ,',CON_QY1_MO,CON_QY1_YR,I_COMMODITY,I_COMMODITY_SDESC'
            ,',I_COMMODITY_LDESC,CON_VAL_MO,CON_VAL_YR,CON_CHA_MO,CON_CHA_YR'
            ,',GEN_VAL_MO,CAL_DUT_MO,CAL_DUT_YR'
            ,'&amp;amp;SUMMARY_LVL2=HSCYCSDTRP'
            ,'&amp;amp;COMM_LVL=HS10'
            ,'&amp;amp;time=',time
            ,'&amp;amp;CTY_CODE=',cty_code
            ,'&amp;amp;I_COMMODITY=',i_commodity
            );
   call execute(cats('%nrstr(%get_census)(',url,',',dsn,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Jan 2022 15:00:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791445#M253504</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-21T15:00:49Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791446#M253505</link>
      <description>&lt;P&gt;If you want help parsing the JSON the the URL generates then provide some examples of what it looks like.&amp;nbsp; The little bit I see on the website makes it kind of look like they have just stuffed a CSV file into the middle of a JSON wrapper.&amp;nbsp; So you might want to extract the text from the JSON and write to an actual text file where it will be easier to read.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 15:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791446#M253505</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-21T15:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791608#M253595</link>
      <description>&lt;P&gt;Bruno:&amp;nbsp; I am using &lt;SPAN class="xisDoc-codeFocus"&gt;SAS 9.4M6&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 23:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791608#M253595</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2022-01-21T23:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791687#M253626</link>
      <description>&lt;P&gt;Thanks Tom.&amp;nbsp; I have found that all fields are being read in as characters and, for some fields each API call can yield varying lengths.&amp;nbsp; When appending subsequent API calls to my database, the varying lengths results in a truncated warning and, the data shows that character strings are being cut off.&amp;nbsp; It looks like I will need to create something called a map to change a few character fields to numeric and to set a length.&amp;nbsp; I think I will create a separate post for that.&lt;/P&gt;</description>
      <pubDate>Sun, 23 Jan 2022 03:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791687#M253626</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2022-01-23T03:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791714#M253637</link>
      <description>&lt;P&gt;Also long as you can write the data out as a delimited text file that might be the simplest way to handle those types of inconsistencies.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take your exsiting inconsistent datasets and write them to CSV files.&amp;nbsp; Use one file if they all have the same variables in the same order.&amp;nbsp; Or perhaps add a RETAIN statement to force them to all have the same variables in the same order (even if the types and/or lengths are different).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename csv temp;
data _null_;
  retain id var1 var2 ... ;
  set file1;
  file csv dsd mod;
  put (_all_) (+0);
run;
data _null_;
  retain id var1 var2 ... ;
  set file2;
  file csv dsd mod;
  put (_all_) (+0);
run;
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just write your own data step to read the CSV file and define the variable how you want them defined.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile csv dsd truncover ;
  length id 8 var1 $10 var2 $40  ... ;
  input (_all_) (+0);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 23 Jan 2022 16:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791714#M253637</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-23T16:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791744#M253658</link>
      <description>&lt;P&gt;Thanks Tom.&amp;nbsp; I found an even simpler solution.&amp;nbsp; The issue was that the first macro iteration set the lengths, since there was no previously existing BASE file.&amp;nbsp; So I created the BASE file before the macro runs, setting the lengths at the max.&amp;nbsp; Now, nothing gets truncated when it is appended.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am down to my last issue.&amp;nbsp; I am running over 1,000 API calls through the macro iterations.&amp;nbsp; Invariably, for whatever reason, there is a glitch with a few of the API calls.&amp;nbsp; Maybe it is the interwebs, maybe it is the Census server, whatever the cause, it fails.&amp;nbsp; Ideally, I would like the macro to recognize the failure and run it again, until it succeeds.&amp;nbsp; But if push comes to shove, I'd at least like the log to print a list of the failed runs at the end so that I don't have to scroll through 10000's of log lines to figure out which ones failed.&amp;nbsp; Then, armed with the list, I could at least manually run them.&amp;nbsp; Thanks in advance for any suggestions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MPRINT(GET_EXAMPLES):   filename json temp;
MLOGIC(GET_EXAMPLES):  %LET (variable name is RC)
MPRINT(GET_EXAMPLES):   libname json json;
NOTE: JSON data is only read once.  To read the JSON again, reassign the JSON LIBNAME.
ERROR: Physical file does not exist, e:\SAS Temporary Files\_TD12064_DESKTOP-575EP3T_\#LN02903.
ERROR: Error in the LIBNAME statement.
MPRINT(GET_EXAMPLES):   proc copy inlib=json out=work;
MPRINT(GET_EXAMPLES):   run;

ERROR: Libref JSON is not assigned.
NOTE: Statements not processed because of errors noted above.
NOTE: PROCEDURE COPY used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

NOTE: The SAS System stopped processing this step because of errors.

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jan 2022 00:11:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791744#M253658</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2022-01-24T00:11:27Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791752#M253664</link>
      <description>&lt;P&gt;If the issue is the LIBNAME statement then capture and/or print the value of &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0vnvc6k3fo349n1aszgd2ls6oxk.htm" target="_self"&gt;SYSLIBRC&lt;/A&gt; after running the LIBNAME statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other thing to do is to check the resulting dataset for missing time periods.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Say your macro is currently appending NEXT to BASE by running something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc append base=base data=next force;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just make sure that NEXT and BASE have variables to store you API settings that matter.&amp;nbsp;&amp;nbsp;If the data does not already have variables that can identify which API call generated those records then add them yourself.&amp;nbsp; &amp;nbsp;Say your macro/loop is using two macro variables named PARM1 and PARM2 to determine which API call to make. Just add dataset variables named PARM1 and PARM2 and populate them with the values of the macro varaibles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data next;
  set next;
  parm1=symget('parm1');
  parm2=symget('parm2');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 03:55:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791752#M253664</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-24T03:55:51Z</dc:date>
    </item>
    <item>
      <title>Re: Census API uses '&amp;' parameter - how to work around in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791772#M253675</link>
      <description>&lt;P&gt;Thanks Tom:&amp;nbsp; the issue is not that some variables are missing data, the issue is nothing results from the API call - its a failed call.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Taking your suggestion, I can use the &lt;CODE class=" language-sas"&gt;syslibrc&lt;/CODE&gt; to print in the log whether it failed or not (see code box).&amp;nbsp; Rather than print something to the log, what I really want is to have the macro not go on to the next step if the value for &lt;CODE class=" language-sas"&gt;syslibrc&lt;/CODE&gt; ne 0, but to loop back to retry the filename statement and keep looping back until the API call works (i.e., the &lt;CODE class=" language-sas"&gt;syslibrc&lt;/CODE&gt; =0).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename json temp;
%let rc=%sysfunc(fcopy(in,json));
libname json json;

%if &amp;amp;syslibrc ne 0
      %then %put "Could not connect to &amp;amp;product";
      %else %put "Successfully connected to &amp;amp;product";

proc copy inlib=json out=work; 
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jan 2022 07:13:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Census-API-uses-amp-parameter-how-to-work-around-in-a-macro/m-p/791772#M253675</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2022-01-24T07:13:15Z</dc:date>
    </item>
  </channel>
</rss>

