【问题标题】:Set a Variable from a filename to read all the files from a directory从文件名设置变量以读取目录中的所有文件
【发布时间】:2015-08-06 00:36:01
【问题描述】:

我想在 SAS 中设置一个带有文件名的新变量。我在一个文件夹中有一堆 txt 文件,我有一个可以读取所有文本文件的宏,此外我想创建另一个变量来读取所有文件名。

我正在尝试做的与之前在这里提出的问题非常相似

SAS set a variable from a filename

这里提供的解决方案对我有用,但它只读取我的一个文件。我如何在宏中使用它,甚至单独使用它来输入我所有的文件名。

reg1=prxparse("/\\(\w+\.csv)/");
if prxmatch(reg1, filename) then filename=prxposn(reg1,1,filename);

我想要的是输入我所有的文本文件名以及文本文件具有的其他变量。 例如:

filename var1 var2 var3
text1    xxx  xxx  xxx
text2    yyy  yyy  yyy

我还尝试了链接中提供的第二种解决方案。我正在使用 SAS EG,它不允许我使用管道符号。对不起,如果我的问题太基本了。我是使用 perl 表达式的新手。

【问题讨论】:

  • 这个 Perl 有什么关系?它使用类似于 Perl 的正则表达式?我建议改为使用正则表达式进行标记。
  • 您不需要正则表达式或宏。

标签: regex sas filenames


【解决方案1】:

只有一行代码很难解释为什么该代码不适合您。

这是我的解决方案 - 没有正则表达式或宏。

data import_all;

*make sure variables to store file name are long enough;
length filename txt_file_name $256;

*keep file name from record to record;
retain txt_file_name;

*Use wildcard in input;
infile "Path\*.txt" eov=eov filename=filename truncover;

*Input first record and hold line;
input@;

*Check if this is the first record or the first record in a new file;
*If it is, replace the filename with the new file name and move to next line;
if _n_ eq 1 or eov then do;
txt_file_name = scan(filename, -1, "\");
eov=0;
end;

*Otherwise  go to the import step and read the files;
else input

 *Place input code here;

;
run;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2010-10-11
    • 2013-12-02
    • 1970-01-01
    相关资源
    最近更新 更多