gpt4 book ai didi

database - 在 SAS 中导入许多文件 .txt

转载 作者:搜寻专家 更新时间:2023-10-30 23:28:59 29 4
gpt4 key购买 nike

我编写了一个代码来同时将多个数据导入 SAS,但我想改进它,有人有什么建议吗?

filename indata pipe 'dir E:\Desafio_SAS\Dados /B'; 

data file_list;
length arquivos$20.;
infile indata truncover ;
input arquivos $20.;
call symput('num_files',_n_);
arquivos=compress(arquivos,',.txt');
run;

CRIANDO UMA MACRO POR PROC SQL PARA GUARDAR O NOME DOS ARQUIVOS

proc sql;
select arquivos into :lista separated by ' ' from file_list;
quit;
%let &lista;
%macro importar(arquivo=);
filename data "E:\Desafio_SAS\Dados\&arquivo..txt";
data &arquivo;
infile data dlm=" " missover dsd firstobs=2;
input v0 (v1 - v8) ($);
format v0 F16.;
run;
%mend importar;
%macro fileout;
%do i=1 %to &num_files;
%importar(arquivo=df&i);
data df&i;
set var_names df&i;
run;
%end;
%mend fileout;
%fileout;
%macro excluiv0;
%do i=1 %to &num_files;
data _null_;
data df&i(drop = v0);
set df&i;
run;
%end;
run;
%mend excluiv0;
%excluiv0;

这只是部分代码。

最佳答案

您可以在 infile 文件规范中使用通配符。只要满足通配符的所有文件都是相同的布局,您就可以使用单个输入来读取所有文件。

例子

* create three text files having same fields;

data _null_;
file '%temp%\1.txt';
put '1 2 3 abc';
put '3 4 5 def';

file '%temp%\2.txt';
put '6 7 8 ghi';
put '9 10 11 jkl';

file '%temp%\3.txt';
put '12 13 14 xyz';
put '15 16 17 tuv';
run;

* read all three using wildcard in infile. Save name of file whence
* data cometh frometh;

data want;
length _filename_ $250;
infile '%temp%\?.txt' filename=_filename_;
length source $250;
length a b c 8 s $20;

source = _filename_;
input a b c s;
run;

通配符是

  • ?, 0 或 1 个任意字符
  • *,任意数量的任意字符

关于database - 在 SAS 中导入许多文件 .txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51996150/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com