解析SAS宏中的JSON对象
这是输入的JSON文件。它必须在SAS数据集中解析。
"results":[
{
"acct_nbr": 1234,
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
}
}
,
{
"acct_nbr": 3456,
"firstName": "Sam",
"lastName": "Jones",
"age": 32,
"address": {
"streetAddress": "25 2nd Street",
"city": "New Jersy",
"state": "NJ",
"postalCode": "10081"
}
}
]
我想要这样的SAS数据集中仅Address字段的输出:
ACCT_NBR FIELD_NAME FIELD_VALUE1234 streetAddress 21 2nd Street
1234 city New York
1234 state NY
1234 postalCode 10021
3456 streetAddress 25 2nd Street
3456 city New Jersy
3456 state NJ
3456 postalCode 10081
我尝试了单独的方式,但没有类似的输出。甚至尝试过从PDF进行扫描…但无法获得所需的输出…
这是我的代码…和输出…
LIBNAME src '/home/user/read_JSON';filename data '/home/user/read_JSON/test2.json';
data src.testdata2;
infile data lrecl = 32000 truncover scanover;
input @'"streetAddress": "' streetAddress $255. @'"city": "' city $255. @'"state": "' state $2. @'"postalCode": "' postalCode $255.;
streetAddress = substr(streetAddress,1,index(streetAddress,'",')-2);
city = substr( city,1,index( city,'",')-2);
state = substr(state,1,index(state,'",')-2);
postalCode = substr(postalCode,1,index(postalCode,'",')-2);
run;
proc print data=src.testdata2;
RUN;
在.lst
文件中的
The SAS System 09:44 Tuesday, January 14, 2014 1 street postal
Obs Address city state Code
1 21 2nd Stree New Yor NY 10021"
2 25 2nd Stree New Jers NJ 10081"
回答:
要使用仅SAS的解决方案来回答您的问题,您有两个问题:
- 使用
SCAN
而不是substr
获取未逗号/引号部分 acct_nbr
是数字,因此您需要从输入中删除最后的引号。
这是正确的代码(我更改了目录,您需要将其改回):
filename data 'c:\temp\json.txt';data testdata2;
infile data lrecl = 32000 truncover scanover;
input
@'"acct_nbr": ' acct_nbr $255.
@'"streetAddress": "' streetAddress $255.
@'"city": "' city $255.
@'"state": "' state $2.
@'"postalCode": "' postalCode $255.;
acct_nbr=scan(acct_nbr,1,',"');
streetAddress = scan(streetAddress,1,',"');
city = scan(city,1,',"');
state = scan(state,1,',"');
postalCode = scan(postalCode,1,',"');
run;
proc print data=testdata2;
RUN;
以上是 解析SAS宏中的JSON对象 的全部内容, 来源链接: utcz.com/qa/418375.html