2011-12-19 210 views
1

我有以下csv文件,我需要將它們讀入SAS數據集。 另外,我需要分配列名稱。 此外,我需要列是數字,但一些列同時具有數字和字符值。將多個csv文件讀取到SAS數據集中

夾AA:abc1.csv,abc2.csv,abc3.csv,...... 夾BB:abc1.csv,abc2.csv,abc3.csv,...... 夾立方厘米:abc1.csv,abc2.csv,abc3.csv,......

+0

你是否有包含CSV文件的n個文件夾的n個?你在Unix,Windows,Mainframe上嗎? – 2011-12-19 23:56:47

+0

是的,我有n個包含n個csv文件的文件夾。我在Windows上。我是SAS的新手,真的很擔心這個問題。非常感謝! – user1106772 2011-12-20 00:03:42

+0

「你需要列是數字」是什麼意思?字符值不能存儲爲數字。 – itzy 2011-12-20 01:26:09

回答

1

這不是一個完整的答案,但它會讓你開始。你將不得不添加一個外部循環來遍歷你想從中獲取文件的不同目錄。

/*List the files in a directory for use in a data step. This is written for Windows. Other operating systems will be slightly different. */ 
filename fnames pipe 'dir c:\temp\* /b'; 

/* Create a data set with one observation for each file name */ 
data fnames; 
    infile fnames pad missover; 
    input @1 filename $255.; 
    n=_n_; 
run; 

/* Store the number of files in a macro variable "num" */ 
proc sql noprint; select count(filename) into :num; quit; 

/* Create a macro to iterate over the filenames, read them in, and append to a data set. */ 
%macro doit; 
    %do i=1 %to # 

     proc sql noprint; 
      select filename into :filename from fnames where n=&i; 
     quit; 

     data ds; 
      infile &filename; 
      input ...list of variable names...; 
      ...other statements...; 
     run; 

     proc append data=ds base=final; run; 
    %end; 
%mend; 

%doit; 
+0

非常感謝,itzy!但我仍在試圖弄清楚。 SAS真的很新鮮。 – user1106772 2011-12-20 21:29:13

2

你也可以用下面的方法做到這一點。

  1. 將所有文件保存在一個文件夾中。
  2. 將它們全部命名爲只有一列的csv文件。
  3. 將帶文件名的文件(csv)導入SAS。
  4. 創建一個宏用「into」子句保留它們的名稱。

    proc sql; 
    select name_list into :name separated by '*' from work.name; 
    %let count2 = &sqlobs; 
    quit; 
    
  5. 創建一個如下所示的宏。

    %macro yy; 
    %do i = 1 %to &count2; 
    %let j = %scan(&name,&i,*); 
    proc import out = &j datafile="folderwhereallcsvfilesarekept\&j..csv" 
    dbms=csv replace; 
    getnames = yes; 
    run; 
    %end; 
    %mend;