在SAS宏中解析JSON对象-第2部分-使用输出函数处理嵌套值
这个问题涉及: 我有一个JSON文件,看起来像:在SAS宏中解析JSON对象-第2部分-使用输出函数处理嵌套值,json,sas,Json,Sas,这个问题涉及: 我有一个JSON文件,看起来像: [ { "rxnorm_id": "999999999", "drug_name": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "plans": [ { "plan_id_type": "xxxxxxxxxxxxx", "plan_id": "999999999999999",
[
{
"rxnorm_id": "999999999",
"drug_name": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"plans": [
{
"plan_id_type": "xxxxxxxxxxxxx",
"plan_id": "999999999999999",
"drug_tier": "xxxxxxxxxxxxxxx",
"prior_authorization": false,
"step_therapy": false,
"quantity_limit": false
},
我可以使用以下代码将具有“rxnorm_id和drug_名称”的每一行导入SAS:
>
filename data url 'http://stg-oh-medicaid.molinahealthcare.com/JSON/Drugs_Molina_Healthcare.json';
data formularies;
infile data lrecl = 32000 truncover scanover;
input @'"rxnorm_id": "' rxnorm_id $255.
@'"drug_name": "' drug_name $255.
@'"plan_id_type": "' plan_id_type $255.
@'"plan_id": "' plan_id $255.
@'"drug_tier": "' drug_tier $255.
@'"prior_authorization": ' prior_authorization $255.
@'"step_therapy": ' step_therapy $255.
@'"quantity_limit": ' quantity_limit $255.;
rxnorm_id = scan(rxnorm_id,1,'",');
drug_name = scan(drug_name,1,'",');
plan_id_type = scan(plan_id_type,1,'",');
plan_id = scan(plan_id,1,'",');
drug_tier = scan(drug_tier,1,'",');
prior_authorization = scan(prior_authorization,1,'",');
step_therapy = scan(step_therapy,1,'",');
quantity_limit = scan(quantity_limit,1,'",');
run;
但是,我想获取“计划”嵌套中介于rxnorm和drug name值之间的所有值。有人建议使用SAS中的输出选项查看缺少的行。有人对我的代码做了很好的修改吗
感谢从9.4开始,在SAS中解析JSON的最佳方法是。这就是我的建议。你也可以用它。如果你喜欢冒险,在9.4m3上,你也可以使用。这就是我要尝试的,因为它允许您轻松地操作SAS数据集 也就是说,如果您可以依赖示例的简单结构,那么您可以只选择包含字段的行,并在数据步骤中使用正则表达式以您想要的格式输出它们:
data want;
infile 'c:/tmp/json_snippet.txt';
length field $20 data $100;
keep field data;
retain re;
input;
if _n_ = 1 then do;
re = prxparse('/"(.*?)": "?(true|false|.*?(?="))/');
end;
if prxmatch(re,_infile_); /* grep only matching lines */
call prxposn(re,1,start,len);
field = substr(_infile_,start,len);
call prxposn(re,2,start,len);
data = substr(_infile_,start,len);
run;
注意:一位智者说,当你用正则表达式解决问题时,现在你有两个问题:)。在可能出错的事情中:
- 换行符
- 使用
而不是“
作为字符串分隔符”
- 长度
- 混合型
文件名URL
指向您问题中的json文件进行了尝试,效果很好,速度也很快。PROC Groovy可能需要一点设置。程序说要打开关闭NOXCMD
选项。我可以在我的9.4生产设备上运行它,而无需任何特殊设置。我明白了,这非常漂亮。是否有修改使其与此链接一起工作:我不确定这是否会导致您潜在的“换行符”问题。我在尝试使用您提供的代码时出现此错误:错误:Ca不加载SSL支持。不管怎样,我非常感谢您在这一点上的帮助。首先,您必须连接所有行,然后将它们拆分为不属于字符串的逗号。然后,其他一些事情可能会出错。因此,我当时的建议是只使用库。好的,我必须获得SAS 9.4,然后是JAVA,然后是Groovy。我有一些功能我一直在为此创建ential Groovy代码。请告诉我您的想法。