我正在编写SAS程序以与API交互。我正在尝试使用SAS从API生成的文本文件中捕获特定字段。
生成的文本“ resp”如下所示:
{“ result”:{“ progressId”:“ ab12”,“ percentComplete”:0.0,“ status”:“ inProgress”},“ meta”:{“ requestId”:“ abcde123”,“ httpStatus”:“ 200-OK“}}
我要捕获的字段是“ progressID”。在这种情况下,它将是“ ab12”。 如果progressID的长度将更改,那么捕获该字段的最简单方式是什么?
我当前的方法如下:
/* The following section will import the text into a SAS table,
seperated by colon. The third column would be "ab12","percentCompelte"
*/
proc import out = resp_table
datafile= resp
dbms = dlm REPLACE;
delimiter = ':';
GETNAMES = NO;
run;
/* The following section will trim off the string ,"percentCompete"*/
data resp_table;
set resp_table;
Progress_ID = SUBSTR(VAR3,2,LENGTH(VAR3)-20);
run;
您有更简单/更简洁的解决方案吗?
谢谢!
肖恩
答案 0 :(得分:2)
您可以使用JSON
库引擎读取json文档,然后将内容复制到SAS数据集。处理引擎创建的数据项。
示例:
filename myjson "c:\temp\sandbox.json";
data _null_;
file myjson;
input;
put _infile_;
datalines;
{"result":{"progressId":"ab12","percentComplete":0.0,"status":"inProgress"},"meta":{"requestId":"abcde123","httpStatus":"200 - OK"}}
run;
libname jsondoc json "c:\temp\sandbox.json";
proc copy in=jsondoc out=work;
run;
proc print data=work.Alldata;
where P1='result' and P2='progressId';
run;