通过其余api结果分页吗?

时间:2019-03-28 11:19:48

标签: sas

我正在使用以下代码从网址中提取json文件:

options NOQUOTELENMAX;

filename usage "/folders/myfolders/sasuser.v94/usage.json";

%let AccessKey = reallylongstring;

proc http
 url="https://a.url"
 method="GET" out=usage;
 headers 
   "Authorization"="Bearer &AccessKey.";
run;

libname usage json "/folders/myfolders/sasuser.v94/usage.json";

data usage;
 set usage.data;
run;

proc print data=usage noobs;
run;   

但是现在结果返回的结果超过1000个,而我需要以某种方式检查nextLink属性?

在.net中,我可以这样使用:

$usagerest = Invoke-Restmethod -url $usageurl -header $authheaders -method get

while ($null -ne $usageRest.nextLink) {
$usageRest = Invoke-Restmethod -uri $usagerest.nextlink -headers $authheaders -method get
}

在SAS中使用proc http可能会发生这种情况吗?

这里是实际json的树状视图,如果有帮助的话?

enter image description here

到目前为止,我已经尝试了一个快速的肮脏版本:

libname usage1 JSON fileref=resp1;

data x;
set usage1.root;
call symputx('nextLink',nextLink);
run;

proc http
url="%superq(nextLink)"
method="GET" out=resp2;
headers 
   "Authorization"="Bearer &AccessKey.";
run;

libname usage2 JSON fileref=resp2;

data y;
set usage2.root;
call symputx('nextLink',nextLink);
run;

proc http
url="%superq(nextLink)"
method="GET" out=resp3;
headers 
   "Authorization"="Bearer &AccessKey.";
run;

libname usage3 JSON fileref=resp3;

data z;
    set usage3.root;
    call symputx('nextLink',nextLink);
run;

库概述:

enter image description here

usage1.data示例:

enter image description here

work.x示例:

enter image description here

谢谢

2 个答案:

答案 0 :(得分:2)

对于需要用双引号引起的解析宏变量的上下文,例如url=选项值,请将%superq放在双引号内-"%superq(<macro-var-name>)"

尝试

… 
url = "%superq(nextLink)"

如果链接仍然很麻烦,您可以尝试

url = "%qsysfunc(urlencode(%superq(nextLink)))"

在控制数据集中每行调用一次宏

有很多方法,例如

  • 通过data _null_;call execute(…堆叠1,000次调用
  • 执行SQL select nextLink into :link1-创建1,000个宏变量
  • 使用%let ds=%sysfunc(open(…打开数据集,并遍历1,000行— %do %while (%sysfunc(fetch(&ds) = 0))

第二个项目符号的示例代码:

此示例将宏变量从“循环”宏传递到“工人”宏。传递宏变量名称时,不必解析宏var并将其引用以传递给工作程序。相反,该作品获得了宏var(又称符号名称),并让superq以引用方式对其进行了解析,以用于源代码生成。本质上,传递宏var类似于传统语言的按引用传递概念。

data have;
  do linknum = 1 to 25;
    link = cats("place=", byte(64+linknum),'&extra="zoom=',linknum,'"&key=MYAPIKEY');
    output;
  end;
run;


%macro processLink(link_mvar=);
  %put url="%sysfunc(urlencode(%superq(&link_mvar)))";
%mend;

%macro processLinks (data=);
  proc sql;
    reset noprint;
    select link into :link1- from &data;
  quit;

  %local i;
  %do i = 1 %to &sqlobs;

    %* pass name of macro variable to macro;
    %processLink (link_mvar=link&i);

  %end;
%mend;

%processLinks(data=have)

答案 1 :(得分:1)

问题似乎更清楚了。该过程可以被宏化以在Proc HTTP循环内重复调用%do。可以将每页数据附加到一个数据集,该数据集将随着检索到的每一页而增长。

未经测试

%macro getThatPagedJsonData (url=, accesskey=, out=);

  %local page guard;

  %let page = 1;
  %Let guard = 50;  * just in case - prevent infinite/excessive looping during development/testing;

  filename response temp;

  %do %until (%length(%superq(url)) eq 0 or &page > &guard);

    * clear libname, releasing any locks on json repsonse file;
    libname page;

    * prior response will be over written;
    proc http url="%superq(url)" method="GET" out=response;
      headers "Authorization"="Bearer &AccessKey.";
    run;

    * magic json engine;
    libname page JSON fileref=response;

    if &page = 1 %then %do;
      * first page starts the output data set;
      data &out;
        set page.data;
      run;
    %end;
    %else %do;
      * append subsequent pages of data;
      proc append base=&out data=page.data;
      run;
    %end;

    * track number of pages processed;
    %let page = %eval (&page + 1);

    * reset url for %until test;
    %let url=;
    * fetch the nextlink as the url for next iteration of %do %until; 
    * might need error handling here when last page has no nextlink;
    data _null_;
      set page.root;
      call symput('url', trim(nextlink));
    run;
  %end;

%mend;

%getThatPagedJsonData (url=...., accesskey=...., out=serviceAggreements);