2014-11-06 142 views
3

我有Excel文件(.xlsx),它的第四行有列名,第五行有數據。我不知道用什麼來提取SAS中的Proc Import中的數據。請幫忙。 謝謝如何將excel數據導入sas

+2

缺少截斷空格,您可能能夠使用範圍參數在https://communities.sas.com/thread/12293?tstart=0 – NoChance 2014-11-06 01:11:50

+0

問題是範圍不會是恆定的,我試圖自動從SAS數據集中的Excel中導入數據並根據我的要求處理它。 – user2694624 2014-11-06 01:21:35

+0

看起來有人寫了一篇關於如何做到這一點的文章(http://www.ciser.cornell.edu/faq/sas/excel2sas.shtml)。我認爲你必須將你的Excel文件格式化爲XLS而不是XLSX – davidcondrey 2014-11-06 01:56:13

回答

0

無論您的數據前面有多少行,提供了以下數據的行都完全是空白。

libname xl excel 'C:\somefile.xlsx'; 

data sheet; 
    set xl.'Sheet1$'n; 
run; 

libname xl clear; 

這將設置您的Excel工作簿,就像數據庫一樣,工作表直接像表一樣引用。我應該注意到我的設置是使用64位Excel的64位SAS 9.4;這是我的理解,例如,如果您有64位SAS和32位Excel,則此方法可能無法按預期工作。

1

我解決了一個類似的問題,在SAS 9.2 導入兩個筆劃,一個探索表和一個提取數據。

這是我在那裏做的一般化,但是請原諒我張貼的來源我沒有測試:我的電腦沒有安裝SAS。 讓我們asume您的數據可能看起來像這樣(當保存爲製表符分隔的文件):

  Some title that does not interust us   
Author Dirk Horsten     
Date 01-Jan-15    
Other Irrelevant thing     

     Bar Foo  Val Remark 
     A Alfa 1 This is the first line 
     B Beta 2 This is the second line 
     C Gamma 3 This is the last line 

所以實際數據在與列標題「酒吧」細胞C6開始。讓我們假設我們知道以未知的順序查找列「Foo」,「Bar」和「Val」以及其他可能不感興趣的列,並且我們並不知道有多少數據行。

現在,我們天真地導入工作表並查詢sasHelp以查明所讀的內容:;

/** First stroke import, to explore the content of the sheet **/ 
proc import datafile="&file_name" out=temp_out dbms=excelcs replace; 
    sheet="&sheet_name"; 
run; 

/** Find out what SAS read in **/ 
proc sql; 
    select couint(*) into :nrColstempCos separ by ' ' 
    from sashelp.vcolumn where libName = 'WORK' and memName = 'TEMP_OUT'; 

    select name into :tempCos separated by ' ' 
    from sashelp.vcolumn where libName = 'WORK' and memName = 'TEMP_OUT'; 
quit; 

接下來我們來看看這些接頭連接和數據,所以我們知道如何正確地讀它。; 如果所有列均被解釋爲字符值,則此方法有效,但不幸的是,Excel不能強制這樣做。

data _null_; 
    set temp_out end=last; 
    array temp {*} &tempCols.; 

    retain foo_col bar_col val_col range_bottom 0; 
    if not (foo_col and bar_col and val_col) then do; 
     range_left = 0; 
     range_right = 0; 

     /* Find out if we finally found the headers */ 
     do col = 1 to &nrCols.; 
      select (upcase(temp(col)); 
       when ('FOO') do; 
        foo_col = col; 
        if not range_left then range_left = col; 
        rang_right = col; 
       end; 
       when ('BAR') do; 
        bar_col = col; 
        if not range_left then range_left = col; 
        rang_right = col; 
       end; 
       when ('VALUE') do; 
        val_col = col; 
        if not range_left then range_left = col; 
        rang_right = col; 
       end; 
       otherwise; 
      end; 
     end; 
     if (foo_col and bar_col and val_col) then do; 
      /** remember where the headers were found **/ 
      range_top = _N_ + 1; 
      call symput ('rangeTop', range_top); 

      rangeLeft = byte(rank('A') + range_left - 1); 
      call symput ('rangeLeft', rangeLeft); 

      rangeRight = byte(rank('A') + range_right - 1); 
      call symput ('rangeRight', rangeRight); 
     end; 
    end; 
    else do; 
     /** find out if there is data on this line **/ 
     if (temp(foo_col) ne '' and temp(bar_col) ne '' and temp(val_col) ne '') 
      then range_bottom = _N_ + 1; 
    end; 

    /** remember where the last data was found **/ 
    if last then call symput ('rangeBottom', range_bottom); 
run; 

爲了計算rangeTop和rangeBottom,我們考慮到,在SAS的_N_th觀測來自在Excel中Ñ + 1條線,因爲第一個Excel行被解釋爲標題。

要計算rangeLeft和rangeRight,我們必須找到相對位置範圍左columen我們將閱讀和翻譯成字母

現在我們在相關資料只讀;

/** Second stroke import, to read in the actual data **/ 
proc import datafile="&file_name" out=&out_ds dbms=excelcs replace; 
    sheet="&heet_name"; 
    range="&rangeLeft.&rangeTop.&rangeRight.&rangeBottom."; 
run; 

成功。如果您的計算機上安裝有SAS並進行更正,請隨時測試此代碼。