2012-06-18 29 views
0

誰能告訴我如何提取此類型的數據:提取XML數組值SAS

[{「數字」:「8457215152」,「類型」:「細胞」,「狀態」: 「LA」, 「國」: 「美國」, 「TZ」: 「CT」, 「拉鍊」: 「70546」, 「MSA」: 「0」},{ 「號」: 「4363685555」, 「類型」: 「Cell」,「state」,「LA」,「country」:「US」,「tz」:「CT」,「zip」:「70546」,「msa」:「0」}]

I希望能有結果有這樣的事情該ID

ID號型國家的國家TX郵編MSA 1 845 ... 1 436 ...

我的問題是一些id有兩個以上的數字(這個id只有2個數字) 我通常能夠在mysql中使用extractvalue函數,但在這種情況下,我在我的繩索末尾。

感謝

+1

[你嘗試過什麼?](http://www.whathaveyoutried.com/) –

+0

SUBSTRING_INDEX,定位,extract_value和SAS陣列一些,但還是沒有得到我想要的解決方案: - ( – JPC

+0

雖然我想有人可以想出一種方法來用SAS來實現,但使用諸如Perl或Python之類的東西來處理數據並以易於讀入SAS的方式進行格式化會容易得多。 – itzy

回答

0
data work.parsed; 
    infile cards; 
    input; 

    length line_str $32000 rec_str $800 number type state country tx zip msa $100 elemname $32; 

    line_str = compress(_infile_, '"'); /* remove quotes */ 
    line_str = translate(line_str, ':', ','); /* make : a key:value separator */ 

    keep id number type state country tx zip msa; 
    id = _N_; 
    rec_count=countc(line_str, '{'); 

    array elem {*} $ number type state country tx zip msa;/* order is important */ 

    put rec_count=; 
    do r=1 to rec_count; 
     if r = 1 then rec_start=3; 
      else rec_start = rec_end + 4; 
     rec_end = findc(line_str, '}', rec_start) - 1; 

     rec_str=substr(line_str, rec_start, rec_end - rec_start + 1); 

     do i=1 to dim(elem); 
      elemname = vname(elem(i)); 
      elem(i)= scan(rec_str, i * 2, ':');/* this way relying on all elements provided in record in expected order */ 
      if findc(elem(i), '}') > 0 then elem(i) = substr(elem(i), 1, findc(elem(i), '}') - 1); 
     end; 
     output; 
    end; 
    cards; 
    [{"number":"8457215152","type":"Cell","state":"LA","country":"US","tz":"CT","zip":"70546","msa":"0"},{"number":"4363685555","type":"Cell","state":"LA","country":"US","tz":"CT","zip":"70546","msa":"2"},{"number":"33333","type":"Cell","state":"CA","country":"US","tz":"CT","zip":"33333","msa":"3"}] 
    ; 
    run; 

當然這有一定的假設條件是什麼數據的模樣。 HTH Vasja