2016-07-27 6 views
2

Ich versuche zum ersten Mal eine JSON-Datei mit SAS analysieren. Ich habe einige Skripte im Internet gefunden und diesen Code auf SAS versucht, konnte aber nicht mit meiner JSON-Datei arbeiten. HierParse Json-Datei mit SAS

data parsed; 
infile data lrecl=32000 truncover scanover dlm=","; 
input @'"driver_id":{' driver_id $255. 
    @'"publication_site_id":{' publication_site_id $255. 
    @'"confirmed_seat_count":{' confirmed_seat_count $255. 
    @'"published_date":{' published_date $255. 
    @'"trip_date":{' trip_date $255. 
    @'"first_publication_date":{' first_publication_date $255. 
    @'"profile_picture_added_date":{' profile_picture_added_date $255.; 

driver_id=scan(driver_id, 1, '",'); 
published_date=scan(published_date, 1, '",'); 
trip_date =scan(trip_date, 1, '",'); 
publication_site_id=scan(publication_site_id, 1, '",'); 
confirmed_seat_count =scan(confirmed_seat_count, 1, '",'); 
first_publication_date =scan(first_publication_date, 1, '",'); 
profile_picture_added_date =scan(profile_picture_added_date, 1, '",'); 
run; 

ist eine Übersicht über meine Json-Datei

{ 
"driver_id": {"0":11450650,"1":4705453,"2":,"3":4892472,"4":47572650,"5":5242675}, 
"publication_site_id":{"0":1056,"1":1056,"2":1056,"3":5056,"4":1056,"5":1056}, 
"confirmed_seat_count":{"0":2.0,"1":0.0,"2":1.0,"3":0.0,"4":0.0,"5":0.0}, 
"published_date":{"0":"2015-06-16 00:00:00","1":"2015-06-16 00:00:00","2":"2015-06-16 00:00:00","3":"2015-06-16 00:00:00","4":"2015-06-16 00:00:00","5":"2015-06-16 00:00:00"}, 
"trip_date":{"0":"2015-06-21 19:00:00","1":"2015-06-21 12:00:00","2":"2015-06-19 17:00:00","3":"2015-06-17 15:30:23","4":"2015-06-21 15:00:00","5":"2015-06-21 17:00:00"}, 
"first_publication_date":{"0":"2015-01-10 00:00:00","1":"2014-01-25 00:00:00","2":"2014-12-22 00:00:00","3":"2014-10-22 00:00:00","4":"2015-06-16 00:00:00","5":"2014-11-20 00:00:00"}, 
"profile_picture_added_date":{"0":null,"1":null,"2":"2015-09-22 00:00:00","3":"2013-11-19 00:00:00","4":"2015-06-17 00:00:00","5":null} 
} 

Ich bin sehr dankbar für die Hilfe.

Grüße SM

+0

Welche Version von SAS verwenden Sie? – Altons

Antwort

1

Dies sollten Sie auf Ihrem Weg helfen:

options noquotelenmax; 
filename data temp lrecl=32000; 

data _null_; 
file data; 
put '{ 
"driver_id": {"0":11450650,"1":4705453,"2":,"3":4892472,"4":47572650,"5":5242675}, 
"publication_site_id":{"0":1056,"1":1056,"2":1056,"3":5056,"4":1056,"5":1056}, 
"confirmed_seat_count":{"0":2.0,"1":0.0,"2":1.0,"3":0.0,"4":0.0,"5":0.0}, 
"published_date":{"0":"2015-06-16 00:00:00","1":"2015-06-16 00:00:00","2":"2015-06-16 00:00:00","3":"2015-06-16 00:00:00","4":"2015-06-16 00:00:00","5":"2015-06-16 00:00:00"}, 
"trip_date":{"0":"2015-06-21 19:00:00","1":"2015-06-21 12:00:00","2":"2015-06-19 17:00:00","3":"2015-06-17 15:30:23","4":"2015-06-21 15:00:00","5":"2015-06-21 17:00:00"}, 
"first_publication_date":{"0":"2015-01-10 00:00:00","1":"2014-01-25 00:00:00","2":"2014-12-22 00:00:00","3":"2014-10-22 00:00:00","4":"2015-06-16 00:00:00","5":"2014-11-20 00:00:00"}, 
"profile_picture_added_date":{"0":null,"1":null,"2":"2015-09-22 00:00:00","3":"2013-11-19 00:00:00","4":"2015-06-17 00:00:00","5":null} 
}'; 
run; 

data parsed; 
infile data lrecl=32000 truncover scanover dlm='}' ; 
input @'"driver_id": {' driver_id: $255. 
    @'"publication_site_id":{' publication_site_id: $255. 
    @'"confirmed_seat_count":{' confirmed_seat_count: $255. 
    @'"published_date":{' published_date: $255. 
    @'"trip_date":{' trip_date: $255. 
    @'"first_publication_date":{' first_publication_date: $255. 
    @'"profile_picture_added_date":{' profile_picture_added_date: [email protected]@; 
run; 

ich die delimeter aktualisiert (so dass Sie die inneren Objekte zu halten, die sich abgegrenzt sind), hinzugefügt, um den Doppelpunkt Eingang Modifikator (erlaubt das Lesen von Nicht-Standard-Werten durch das Aussprechen von "Lesen in der nächsten nicht-leeren Spalte beginnen") und endet mit dem nacheilenden Spaltenzeiger, um den Datensatz für die Ausgabe nachfolgender Beobachtungen zu halten (die Wahrscheinlichkeit ist groß, dass Ihr json als ein langer Stream ankommt) . Oh, ich habe auch einen Platz für @'"driver_id": {' hinzugefügt - war das ein Tippfehler?

Ich habe den Scan-Teil weggelassen, dachte, Sie haben dieses Bit unter Kontrolle!

1

Nicht meine beste Arbeit, aber es wird geben, was Sie wollen !. Sie müssen Spalten und ein bisschen dq reorganisieren, aber Ihr JSON wird als SAS-Datensatz aussehen. Ich würde Ihnen empfehlen, Python pandas Paket zu betrachten. Mit pandas hätten Sie dies mit einer einzigen Codezeile sortiert.

data parsed; 
infile "/your_path/test.json" lrecl=32500 truncover scanover dlm="," firstobs=2; 
input @'"driver_id"' _driver_id $255. 
     @'"publication_site_id"' _publication_site_id $255. 
     @'"confirmed_seat_count"' _confirmed_seat_count $255. 
     @'"published_date"' _published_date $255. 
     @'"trip_date"' _trip_date $255. 
     @'"first_publication_date"' _first_publication_date $255. 
     @'"profile_picture_added_date"' _profile_picture_added_date $255.; 
array v{*} _:; 
do i=1 to dim(v); 
    v[i]=prxchange('s/"\d+"//',-1,v[i]); 
    v[i]=compress(v[i],'{}"'); 
    v[i]=substr(v[i],3); 
end; 
run; 

proc transpose data=parsed out=t_parsed ; 
var _:; 
run; 

data want(drop=i); 
set t_parsed; 
array split(6) $50.; 
do i=1 to dim(split); 
split[i] = scan(col1,i,','); 
end; 
run; 

proc sort data=want; by _name_;run; 

proc transpose data=want out=want_t(drop=_name_); 
var split:; 
id _name_; 
run;