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

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

我必须从SAS数据创建一个有点复杂的json文件,与SAS在documentation on the JSON Procedure中创建的文件非常相似

  1. [
  2. "Vehicles",
  3. [
  4. "Truck",
  5. [
  6. "Greater than $26000",
  7. {
  8. "Asia": [
  9. {
  10. "Make": "Nissan",
  11. "Model": " Titan King Cab XE",
  12. "Type": "Truck",
  13. "Origin": "Asia",
  14. "MSRP": 26650
  15. }
  16. ]
  17. },
  18. {
  19. "Europe": [
  20. ]
  21. },
  22. {
  23. "USA": [
  24. {
  25. "Make": "Cadillac",
  26. "Model": " Escalade EXT",
  27. "Type": "Truck",
  28. "Origin": "USA",
  29. "MSRP": 52975
  30. },
  31. {
  32. "Make": "Chevrolet",
  33. "Model": " Avalanche 1500",
  34. "Type": "Truck",
  35. "Origin": "USA",
  36. "MSRP": 36100
  37. },
  38. {
  39. "Make": "Chevrolet",
  40. "Model": " Silverado SS",
  41. "Type": "Truck",
  42. "Origin": "USA",
  43. "MSRP": 40340
  44. },
  45. {
  46. "Make": "Chevrolet",
  47. "Model": " SSR",
  48. "Type": "Truck",
  49. "Origin": "USA",
  50. "MSRP": 41995
  51. },
  52. {
  53. "Make": "Ford",
  54. "Model": " F-150 Supercab Lariat",
  55. "Type": "Truck",
  56. "Origin": "USA",
  57. "MSRP": 33540
  58. },
  59. {
  60. "Make": "GMC",
  61. "Model": " Sierra HD 2500",
  62. "Type": "Truck",
  63. "Origin": "USA",
  64. "MSRP": 29322
  65. }
  66. ]
  67. }
  68. ]
  69. ]
  70. ]

字符串
然而,这个解决方案要求你知道'origin'的可能值,这使得我无法复制和修改示例代码

  1. %let vehicleType=Truck;
  2. %let minCost=26000;
  3. proc json out="C:\Users\sasabc\JSON\WriteOpenArrayOutput.json" nosastags pretty;
  4. write open array;
  5. write values "Vehicles";
  6. write open array;
  7. write values "&vehicleType";
  8. write open array;
  9. write values "Greater than $&minCost";
  10. /*********** Asian ***********************/
  11. %let originator=Asia;
  12. write open object;
  13. write values "&originator";
  14. write open array;
  15. export sashelp.cars(where=((origin = "&originator") and
  16. (type = "&vehicleType") and
  17. (MSRP > &minCost) )
  18. keep=make model type origin MSRP);
  19. write close; /* data values */
  20. write close; /* Asia */
  21. /*********** European ***********************/
  22. %let originator=Europe;
  23. write open object;
  24. write values "&originator";
  25. write open array;
  26. export sashelp.cars(where=((origin = "&originator") and
  27. (type = "&vehicleType") and
  28. (MSRP > &minCost) )
  29. keep=make model type origin MSRP);
  30. write close; /* data values */
  31. write close; /* Europe */
  32. /*********** American ***********************/
  33. %let originator=USA;
  34. write open object;
  35. write values "&originator";
  36. write open array;
  37. export sashelp.cars(where=((origin = "&originator") and
  38. (type = "&vehicleType") and
  39. (MSRP > &minCost) )
  40. keep=make model type origin MSRP);
  41. write close; /* data values */
  42. write close; /* USA */
  43. write close; /* expensive */
  44. write close; /* vehicleType */
  45. write close; /* cars */
  46. run;


是否有人有一个更通用的解决方案/提供更大的灵活性,但它提供了与proc json相同的保证来创建有效的json?

xcitsw88

xcitsw881#

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

  1. filename code temp;
  2. proc sort data=sashelp.cars(keep=origin) out=origins nodupkey;
  3. by origin;
  4. run;
  5. data _null_;
  6. set origins;
  7. file code;
  8. put 'write open object;'
  9. / 'write values ' origin :$quote. ';'
  10. / 'write open array;'
  11. / 'export sashelp.cars(keep=make model type origin MSRP'
  12. / ' where=((' origin = :$quote. ')'
  13. / ' and (type = "&vehicleType")'
  14. / ' and (MSRP > &minCost)'
  15. / ' ));'
  16. / 'write close;'
  17. / 'write close;'
  18. ;
  19. run;

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

  1. proc json .... ;
  2. ....
  3. %include code / source2;
  4. ....
  5. run;

展开查看全部

相关问题