首页 > 解决方案 > SAS:捕获文本文件中的特定字段

问题描述

我正在编写一个 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;

您有更简单/更简洁的解决方案吗?

谢谢!

肖恩

标签: sas

解决方案


您可以使用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;

推荐阅读