SAS导入JSON

时间:2015-04-12 21:54:53

标签: json sas

我需要将一些JSON转换为机器友好格式(例如CSV,Excel,Stata,SAS),并使用SAS,因为我的文件很大。

观察的一个例子:

{"business_id": "vcNAWiLM4dR7D2nwwJ7nCA", "full_address": "4840 E Indian School Rd\nSte 101\nPhoenix, AZ 85018", "hours": {"Tuesday": {"close": "17:00", "open": "08:00"}, "Friday": {"close": "17:00", "open": "08:00"}, "Monday": {"close": "17:00", "open": "08:00"}, "Wednesday": {"close": "17:00", "open": "08:00"}, "Thursday": {"close": "17:00", "open": "08:00"}}, "open": true, "categories": ["Doctors", "Health & Medical"], "city": "Phoenix", "review_count": 9, "name": "Eric Goldberg, MD", "neighborhoods": [], "longitude": -111.98375799999999, "state": "AZ", "stars": 3.5, "latitude": 33.499313000000001, "attributes": {"By Appointment Only": true}, "type": "business"}

我一直在使用http://support.sas.com/resources/papers/proceedings13/296-2013.pdf推荐的方法。

问题是并非所有观察都有相同的条目。例如,某些观察结果可能会丢失" full_address"。

因此我的示例代码现在是

filename data '(filename)';
data datatest; * defines dataset;
infile data lrecl = 32000 truncover scanover;
input 
    @'"business_id": "' business_id $255. 
    ;

    business_id = substr(business_id,1,index(business_id,'",')-1);


    IF INDEX(_INFILE_,'"full_address":') > 0
    THEN DO;    
        input @'"full_address": "' full_address $255.;      
        full_address = substr(full_address,1,index(full_address,'",')-1);
    END; 
run;
proc print data = work.datatest;
run;

问题是代码似乎跳过了其他所有观察。我怎样才能防止这种情况发生?

1 个答案:

答案 0 :(得分:1)

您的问题是您的初始输入超过了full_address(因为它占用了255个字符)。你可以解决这个问题,如下:

input 
    @'"business_id": "' business_id $255. +(-254) @
    ;

基本上将指针重置回字段的开头,并允许您查找下一部分。

你也可以用不同的方式阅读;如果你有SAS 9.3(我相信),PROC GROOVY可以用来以更简单的方式读入JSON文件。有关详细信息,请参阅我对this question的回答。