sas - 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;
您有更简单/更简洁的解决方案吗?
谢谢!
肖恩
解决方案
您可以使用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;
推荐阅读
- linux - 在 shell 脚本中将 4 位年份转换为 2 位数字
- android - com.stripe.android.exception.invalidrequestexception 没有这样的付款意图
- wordpress - Wordpress 阻止了我的主题的重要 API
- javascript - 如何从提交的 Laravel 数据中填充 Vue.js Vue-Autoextra 动态表单?
- c# - 在 ASP.NET Core 中添加脚手架登录寄存器破坏了我的项目
- javascript - 如何优化 for 循环以供所有人使用?
- android - 以编程方式安装 .apk 的回调
- git - 如何从 bitbucket 中提取特定的标签代码更改?
- java - 是否可以仅使用 Java 流从集合中获取特定数据?
- mysql - Laravel mongodb 查询原始数据