【问题标题】:Read JSON file and save it in SAS dataset读取 JSON 文件并将其保存在 SAS 数据集中
【发布时间】:2014-01-14 09:16:58
【问题描述】:

我正在尝试从 JSON 文件中读取数据并将值存储在 SAS 数据集中 这是我的 JSON 文件 test.json

[
  {
    "id":1,
    "name":"Johnson, Smith and Jones Co.",
    "amount":345.33,
    "Remark":"Pays on time"
  },
  {
    "id":2,
    "name":"Sam, Smith",
    "amount":993.44,
    "Remark":""
  },
  {
    "id":3,
    "name":"Barney & Company",
    "amount":0,
    "Remark":"Great to work with and always pays with cash."
  },
  {
    "id":4,
    "name":"Johnson's Automotive",
    "amount":2344,
    "Remark":""
  }
]

现在我想要像简单的 SAS 数据集这样的输出,我可以在其中以表格形式获取。 如果我使用 proc 打印数据集,它看起来像:

id  name    amount  Remark
1   Johnson, Smith and Jones Co.    345.33  Pays on time
2   Sam, Smith  993.44  
3   Barney & Company    0   Great to work with and always pays with cash.
4   Johnson's Automotive    2344

这是我的方法,但没有得到正确的输出。

LIBNAME src  '/home/user/read_JSON';
filename data '/home/user/read_JSON/test.json';
data src.testdata;
    infile data lrecl = 32000 truncover scanover;
    input @'"id": "' id $255. @'"name": "' name $255. @'"amount": "' amount @'"Remark": "' Remark $255.;
    id = substr(id,1,index(id,'",')-2);
    name = substr( name,1,index( name,'",')-2);
    amount = substr(amount,1,index(amount,'",')-2);
    Remark = substr(Remark,1,index(Remark,'",')-2);
run;

proc print data=src.testdata;
RUN;

知道我是怎么做到的吗???

【问题讨论】:

标签: json sas sas-macro datastep


【解决方案1】:

设置 firstobs=3 忽略前两行,然后使用/ 输入修饰符读取下一行。尾随的两个 / 忽略后面的 JSON 分隔符。

数据测试数据; infile 数据 lrecl=32000 trunco​​ver dsd firstobs=3 ; 输入@'"id":' id $255。 /@'"name":' 名字 $255。 /@'"金额":'金额 /@'"备注":'备注$255。 / / ; 跑;

【讨论】:

  • 谢谢.....我有一些记录......但不是全部......我有输出 lst 文件......我怎么能附加?
猜你喜欢
  • 2017-10-17
  • 2016-07-26
  • 2013-11-29
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多