gpt4 book ai didi

sas - 创建特定的 PROC JSON 层次结构

转载 作者:行者123 更新时间:2023-12-04 15:42:25 26 4
gpt4 key购买 nike

我正在尝试使用 SAS Enterprise Guide (EG) 创建以下格式的 JSON 文件:

{
"schema": "EMAIL_SHA26",
"data": ["1516e67afa2d9c3874c3e9874bdb41c4", "1a7e5f59b3f0dfe6ea152cb65aedb0d2"]
}

我非常接近,但我生成的 JSON 文件中的括号太多了。这是我目前得到的:

{
"schema": "EMAIL_SHA26",
"data": [
[
"1516e67afa2d9c3874c3e9874bdb41c4"
],
[
"1a7e5f59b3f0dfe6ea152cb65aedb0d2"
]
]
}

这是我使用的代码:

PROC JSON 
OUT = users NOSASTAGS PRETTY;
WRITE VALUES "schema";
WRITE VALUES "EMAIL_SHA26";
WRITE VALUES "data";
WRITE OPEN ARRAY;
EXPORT users_ds / NOKEYS;
WRITE CLOSE;
RUN;

“users_ds”数据集有一列,其中有 2 条数据记录。有什么办法可以防止它在数据集中的每个值周围加上括号?此外,如果知道散列电子邮件列表可能多达 10,000 条记录,是否可以实现我期望的输出?

如有任何帮助,我们将不胜感激。

最佳答案

Export 默认将数据集输出为名称:值对对象数组。对于 NOKEYS,构造是一个数组数组,内部数组是一个值数组。

为了获取列的单个值数组,您可以将该列转置为单行数据集并将其导出。您不必在 EXPORT 之前OPEN ARRAY

data have;
do row = 1 to 10;
userid = uuidgen();
age = 19 + row;
output;
end;
run;

* transpose a single column into a single row data set;
proc transpose data=have out=have_1_row(drop=_name_);
var userid;
run;

filename users "C:\temp\users.json" ;

proc json out = users nosastags pretty;
WRITE VALUES "schema";
WRITE VALUES "EMAIL_SHA26";
WRITE VALUES "data";
EXPORT have_1_row / NOKEYS;
RUN;

生成 json

{
"schema": "EMAIL_SHA26",
"data": [
"6ebd89fa-b6bc-4c14-b094-43792d202ad7",
"ec53dd59-1290-47d7-b437-0c754349434c",
"17332882-58ca-4c09-a599-2048d58460d0",
"d5b57a19-ff73-4deb-bfc7-62ebc19d719e",
"9d2758b2-e128-45df-8589-99cd7204c1ab",
"a13bcba7-742f-4a01-bd56-dc12f4190d3e",
"5f853bf3-9597-4c94-9b57-a54d3de190c3",
"0edbd2d8-bd5d-46be-aaa7-ac208df4ba62",
"07347e73-7efa-4e9c-8242-5a9c85f07b56",
"03976b1b-513f-41ee-92d5-d23c8d3d4918"
]
}

对于想要将多个列导出为值数组的情况,请考虑使用DOSUBL 来调用一个宏,该宏会旁运行转置并生成宏代码中使用的单行数据集生成的 EXPORT 语句:

%macro transpose_column(data=, column=, out=);
%* generate code that will transpose a single column into a single row data set;
proc transpose data=&data out=&out(keep=col:);
var &column;
run;
%mend;

%macro export_column_as_array (data=, column=);
%local rc out;
%let out = _%sysfunc(monotonic());

%* Invoke DOSUBL to side-run macro generated proc transpose code;
%let rc = %sysfunc(
DOSUBL(
%transpose_column(data=&data, column=&column, out=&out)
)
);

%* use the output data set created by the side-run code;
WRITE VALUES "&column";
EXPORT &out / NOKEYS;
%mend;

data have;
do row = 1 to 10;
userid = uuidgen();
age = 19 + row;
date = today() - row;
output;
end;
format date yymmdd10.;
run;

filename users "C:\temp\users.json" ;

options mprint mtrace;

proc json out = users nosastags pretty;
WRITE VALUES "schema";
WRITE VALUES "EMAIL_SHA26";
%export_column_as_array(data=have,column=userid);
%export_column_as_array(data=have,column=age);
%export_column_as_array(data=have,column=date);
run;
quit;

关于sas - 创建特定的 PROC JSON 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57332658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com