我有以下csv文件,我需要将它们读入SAS数据集。 另外,我需要分配列名称。 此外,我需要列是数字,但一些列同时具有数字和字符值。将多个csv文件读取到SAS数据集中
夹AA:abc1.csv,abc2.csv,abc3.csv,...... 夹BB:abc1.csv,abc2.csv,abc3.csv,...... 夹立方厘米:abc1.csv,abc2.csv,abc3.csv,......
我有以下csv文件,我需要将它们读入SAS数据集。 另外,我需要分配列名称。 此外,我需要列是数字,但一些列同时具有数字和字符值。将多个csv文件读取到SAS数据集中
夹AA:abc1.csv,abc2.csv,abc3.csv,...... 夹BB:abc1.csv,abc2.csv,abc3.csv,...... 夹立方厘米:abc1.csv,abc2.csv,abc3.csv,......
这不是一个完整的答案,但它会让你开始。你将不得不添加一个外部循环来遍历你想从中获取文件的不同目录。
/*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;
非常感谢,itzy!但我仍在试图弄清楚。 SAS真的很新鲜。 – user1106772 2011-12-20 21:29:13
你也可以用下面的方法做到这一点。
创建一个宏用“into”子句保留它们的名称。
proc sql;
select name_list into :name separated by '*' from work.name;
%let count2 = &sqlobs;
quit;
创建一个如下所示的宏。
%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;
你是否有包含CSV文件的n个文件夹的n个?你在Unix,Windows,Mainframe上吗? – 2011-12-19 23:56:47
是的,我有n个包含n个csv文件的文件夹。我在Windows上。我是SAS的新手,真的很担心这个问题。非常感谢! – user1106772 2011-12-20 00:03:42
“你需要列是数字”是什么意思?字符值不能存储为数字。 – itzy 2011-12-20 01:26:09