我有Excel文件(.xlsx),它的第四行有列名,第五行有數據。我不知道用什麼來提取SAS中的Proc Import中的數據。請幫忙。 謝謝如何將excel數據導入sas
3
A
回答
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並進行更正,請隨時測試此代碼。
相關問題
- 1. IGNORE SAS數據導入EXCEL
- 2. 將Excel文件讀入SAS數據集
- 3. 如何將數據導入Excel模板
- 4. 將數據透視表從Excel導入到SAS EG中
- 5. 將SAS數據文件導入R
- 6. 將XML數據導入excel
- 7. 將Excel數據導入F#
- 8. 將Excel數據導入Access
- 9. 將Excel數據導入GridView
- 10. 將SQLite數據導入Excel?
- 11. 將SAS導出到Excel中
- 12. 如何使用getnames = no將excel導入到sas中?
- 13. 將數據從Excel導入數據庫
- 14. 如何將SAS7BDAT數據庫導入到沒有SAS的Stata中
- 15. 如何將數據從sql導入excel並更新sql數據
- 16. 如何將數據庫中的數據導入Excel表格?
- 17. 如何將Stata文件導入SAS 9.0?
- 18. 如何將SharePoint表導入SAS?
- 19. 從Excel導入Excel數據
- 20. 如何導出超過350MB的SAS數據集到excel表格
- 21. 如何從Excel導入數據到MYSQL?
- 22. 如何導入數據到excel表
- 23. 如何根據條件將數據從excel導入到UFT
- 24. 將Excel數據導入SAS,同時電子表格仍然打開
- 25. 如何將數據導出到excel中
- 26. 如何將大數據導出到Excel
- 27. SAS導入excel日期格式更改
- 28. 直接將數據表導入Excel C#
- 29. PHP - 將數據導入excel模板
- 30. 將數據從dataGrid導入excel
缺少截斷空格,您可能能夠使用範圍參數在https://communities.sas.com/thread/12293?tstart=0 – NoChance 2014-11-06 01:11:50
問題是範圍不會是恆定的,我試圖自動從SAS數據集中的Excel中導入數據並根據我的要求處理它。 – user2694624 2014-11-06 01:21:35
看起來有人寫了一篇關於如何做到這一點的文章(http://www.ciser.cornell.edu/faq/sas/excel2sas.shtml)。我認爲你必須將你的Excel文件格式化爲XLS而不是XLSX – davidcondrey 2014-11-06 01:56:13