Arrays 将数组传递到JSON文件中的每个对象中。(过程JSON SAS)
我正在尝试将数据集导出到JSON文件。使用procjson,数据集中的每一行都可以很好地导出。 我要做的是向每个导出的对象添加一个数组,其中包含来自特定列的数据 我的数据集具有如下结构:Arrays 将数组传递到JSON文件中的每个对象中。(过程JSON SAS),arrays,json,sas,Arrays,Json,Sas,我正在尝试将数据集导出到JSON文件。使用procjson,数据集中的每一行都可以很好地导出。 我要做的是向每个导出的对象添加一个数组,其中包含来自特定列的数据 我的数据集具有如下结构: data test; input id $ amount $ dimension $; datalines; 1 x A 1 x B 1 x C 2 y A 2 y X 3 z C 3 z K 3 z X ; run; proc json out='/MYPATH/jsontest.json' pretty
data test;
input id $ amount $ dimension $;
datalines;
1 x A
1 x B
1 x C
2 y A
2 y X
3 z C
3 z K
3 z X
;
run;
proc json out='/MYPATH/jsontest.json' pretty nosastags;
export test;
run;
[
{
"id": "1",
"amount": "x",
"dimension": "A"
},
{
"id": "1",
"amount": "x",
"dimension": "B"
},
{
"id": "1",
"amount": "x",
"dimension": "C"
},
...]
导出的JSON对象显然如下所示:
data test;
input id $ amount $ dimension $;
datalines;
1 x A
1 x B
1 x C
2 y A
2 y X
3 z C
3 z K
3 z X
;
run;
proc json out='/MYPATH/jsontest.json' pretty nosastags;
export test;
run;
[
{
"id": "1",
"amount": "x",
"dimension": "A"
},
{
"id": "1",
"amount": "x",
"dimension": "B"
},
{
"id": "1",
"amount": "x",
"dimension": "C"
},
...]
我想要的结果:
对于每个id,我希望将维度列中的所有数据插入一个数组,以便我的输出如下所示:
[
{
"id": "1",
"amount": "x",
"dimensions": [
"A",
"B",
"C"
]
},
{
"id": "2",
"amount": "y",
"dimensions": [
"A",
"X"
]
},
{
"id": "3",
"amount": "z",
"dimensions": [
"C",
"K",
"X"
]
}
]
我还没有找到像这样的场景或一些解决问题的指导方针。我希望有人能帮忙
/Crellee您可以在调用
proc json
之前尝试连接/分组文本
我的SAS环境中没有proc json
,但请尝试此步骤,看看它是否适合您:
data want;
set test (rename=(dimension=old_dimension));
Length dimension $200. ;
retain dimension ;
by id amount notsorted;
if first.amount = 1 then do; dimension=''; end;
if last.amount = 1 then do; dimension=catx(',',dimension,old_dimension); output; end;
else do; dimension=catx(',',dimension,old_dimension); end;
drop old_dimension;
run;
输出:
id=1 amount=x dimension=A,B,C
id=2 amount=y dimension=A,X
id=3 amount=z dimension=C,K,X
json输出还有其他方法,包括
- 数据步中的手动编码发射器
- 进程DS2中的JSON包
data _null_;
file 'c:\temp\test.json';
put '[';
do group_counter = 1 by 1 while (not end_of_data);
if group_counter > 1 then put @2 ',';
put @2 '{';
do dimension_counter = 1 by 1 until (last.amount);
set test end=end_of_data;
by id amount;
if dimension_counter = 1 then do;
q1 = quote(trim(id));
q2 = quote(trim(amount));
put
@4 '"id":' q1 ","
/ @4 '"amount":' q1 ","
;
put @4 '"dimensions":' / @4 '[';
end;
else do;
put @6 ',' @;
end;
q3 = quote(trim(dimension));
put @8 q3;
end;
if dimension_counter > 1 then put @4 '}';
put @2 ']';
end;
put ']';
stop;
run;
这样的发射器可以进行宏化和泛化,以处理data=、by=和arrayify=的规范。不建议朋友使用此路径。不幸的是,这无法解决我的问题,因为我的JSON对象中仍然没有数组,而是有一个值为..“a,B,C”的键。但是非常感谢你的回答!非常感谢你。这是一个很好的解决方案!它不像我所希望的那样充满活力。但也许proc-json无法解决我所面临的问题。