您可以使用SAS proc json编写超过2个级别的json吗?

wwwo4jvm  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(104)

我必须从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?

xcitsw88

xcitsw881#

如果你想使用PROC JSON以这种方式编写文件,那么你将需要使用一些代码生成。您可以使用宏代码,但由于ORIGIN值列表已经在数据中,因此您可以仅使用数据步骤来生成代码。
因此,将重复代码生成到文件中:

filename code temp;

proc sort data=sashelp.cars(keep=origin) out=origins nodupkey;
  by origin;
run;

data _null_;
  set origins;
  file code;
  put 'write open object;'
    / 'write values ' origin :$quote. ';'
    / 'write open array;'
    / 'export sashelp.cars(keep=make model type origin MSRP'
    / ' where=((' origin = :$quote. ')'
    / '   and (type   = "&vehicleType")'
    / '   and (MSRP   > &minCost)'
    / ' ));' 
    / 'write close;'
    / 'write close;'
  ;
run;

字符串
然后%INCLUDE文件在你想要它运行的地方。

proc json .... ;
 ....
%include code / source2;
 ....
run;

相关问题