我必须从SAS数据创建一个有点复杂的json文件,与SAS在documentation on the JSON Procedure中创建的文件非常相似
[
"Vehicles",
[
"Truck",
[
"Greater than $26000",
{
"Asia": [
{
"Make": "Nissan",
"Model": " Titan King Cab XE",
"Type": "Truck",
"Origin": "Asia",
"MSRP": 26650
}
]
},
{
"Europe": [
]
},
{
"USA": [
{
"Make": "Cadillac",
"Model": " Escalade EXT",
"Type": "Truck",
"Origin": "USA",
"MSRP": 52975
},
{
"Make": "Chevrolet",
"Model": " Avalanche 1500",
"Type": "Truck",
"Origin": "USA",
"MSRP": 36100
},
{
"Make": "Chevrolet",
"Model": " Silverado SS",
"Type": "Truck",
"Origin": "USA",
"MSRP": 40340
},
{
"Make": "Chevrolet",
"Model": " SSR",
"Type": "Truck",
"Origin": "USA",
"MSRP": 41995
},
{
"Make": "Ford",
"Model": " F-150 Supercab Lariat",
"Type": "Truck",
"Origin": "USA",
"MSRP": 33540
},
{
"Make": "GMC",
"Model": " Sierra HD 2500",
"Type": "Truck",
"Origin": "USA",
"MSRP": 29322
}
]
}
]
]
]
字符串
然而,这个解决方案要求你知道'origin'的可能值,这使得我无法复制和修改示例代码
%let vehicleType=Truck;
%let minCost=26000;
proc json out="C:\Users\sasabc\JSON\WriteOpenArrayOutput.json" nosastags pretty;
write open array;
write values "Vehicles";
write open array;
write values "&vehicleType";
write open array;
write values "Greater than $&minCost";
/*********** Asian ***********************/
%let originator=Asia;
write open object;
write values "&originator";
write open array;
export sashelp.cars(where=((origin = "&originator") and
(type = "&vehicleType") and
(MSRP > &minCost) )
keep=make model type origin MSRP);
write close; /* data values */
write close; /* Asia */
/*********** European ***********************/
%let originator=Europe;
write open object;
write values "&originator";
write open array;
export sashelp.cars(where=((origin = "&originator") and
(type = "&vehicleType") and
(MSRP > &minCost) )
keep=make model type origin MSRP);
write close; /* data values */
write close; /* Europe */
/*********** American ***********************/
%let originator=USA;
write open object;
write values "&originator";
write open array;
export sashelp.cars(where=((origin = "&originator") and
(type = "&vehicleType") and
(MSRP > &minCost) )
keep=make model type origin MSRP);
write close; /* data values */
write close; /* USA */
write close; /* expensive */
write close; /* vehicleType */
write close; /* cars */
run;
型
是否有人有一个更通用的解决方案/提供更大的灵活性,但它提供了与proc json
相同的保证来创建有效的json?
1条答案
按热度按时间xcitsw881#
如果你想使用PROC JSON以这种方式编写文件,那么你将需要使用一些代码生成。您可以使用宏代码,但由于ORIGIN值列表已经在数据中,因此您可以仅使用数据步骤来生成代码。
因此,将重复代码生成到文件中:
字符串
然后%INCLUDE文件在你想要它运行的地方。
型