本文介绍了在SAS中附加多个CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个SAS数据集中导入大量的csv文件。它们都具有相同的数据结构(相同的变量,第一行上的变量名称)。我通常在SQL中工作,但我被迫在SAS中的这个特定项目,我只有基本的知识。

I need to import a large amount of csv files in one SAS dataset. They all have the same data structure (same variables, variable names on the first line). I usually work in SQL, but I am forced to this particular project in SAS of which I have only basic knowledge.

现在,我的代码看起来像这样: / p>

For the moment, my code looks like this:

proc import out=work.data
   datafile = file1.csv
   DBMS=CSV REPLACE;
   GETNAMES=YES;
   DATAROW=2;

proc import out=work.newData
   datafile = file2.csv
   DBMS=CSV REPLACE;
   GETNAMES=YES;
   DATAROW=2;

proc append base=work.data
            data=work.newData force;
run;

等等。file3.csv ... file4.csv。

and so on for file3.csv ... file4.csv.

我相信,这是一个更优雅的方式,即,循环遍历一个文件夹上的所有csv文件,而不明确写入(有几千个文件)。

There is, I am sure, a more elegant way of doing this, that is, looping over all csv files on one folder without writing them explicitly (there are a few thousand files).

感谢您的帮助。

推荐答案

,而不是使用PROC IMPORT(虽然如果您使用PROC IMPORT一次,它会礼貌地将该输入代码写入您可以使用的日志),然后您可以使用通配符:

You need to figure out the input statement, rather than using PROC IMPORT (though if you use PROC IMPORT once, it will politely write that input code to the log which you can then use), and then you can use wildcards:

data mydata;
infile "c:\temp\*.csv" dlm=',' missover lrecl=32767;
input
myvar1
myvar2 $
myvar3
myvar4 :date9.
;
run;

请参见,例如其他方式来做。

Some other options exist; see https://communities.sas.com/message/182012#182012 for example for other ways to do it.

这篇关于在SAS中附加多个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:12