【发布时间】:2015-07-02 13:25:58
【问题描述】:
我找到了一个宏,并一直使用它来导入以 csv 格式提供给我的数据集。现在我需要对其进行编辑,因为我的数据集中有一个 id 号,并且我想要具有相同名称的 sas 数据集。
这些 csv 被命名为 IDSTUDY233_first.csv 这样的东西所以我希望 sas 数据集是 IDSTUDY233_first。它应该出现在我的工作文件夹中。
我认为它只会为每个名为 IDSTUDY233_first 或类似名称的 csv 创建一个 sas 数据集。 (对于每个额外的研究,依此类推)。然而它是这样命名的。 IDSTUDY_FIRST 并为每个 ID 拥有自己的权利。我对宏比较陌生,并且一直在试图弄清楚它为什么这样做以及如何解决它。有什么建议吗?
%let subdir=Y:\filepath\; *MACRO VARIABLE FOR FILEPATH;
filename dir "&subdir.*.csv "; *give the file the name from the path that your at whatever the csv is named;
data new; *create the dataset new it has all those filepath names csv names;
length filename fname $ 200;
infile dir eof=last filename=fname;
input ;
last: filename=fname;
run;
proc sort data=new nodupkey; *sort but don't keep duplicate files;
by filename;
run;
data null; *create the dataset null;
set new;
call symputx(cats('filename',_n_),filename); *call the file name for this observation n;
call symputx(cats('dsn',_n_),compress(scan(filename,-2,'\.'), ,'ka')); *call the dataset for this file compress then read the file;
call symputx('nobs',_n_); *call for the number of observations;
run;
%put &nobs.; *but each observation in;
%macro import; *start the macro import;
%do i=1 %to &nobs; *Do for each fie to number of observations;
proc import datafile="&&filename&i" out=&&dsn&i dbms=csv replace;
getnames=yes;
run;
%end;
%mend import;
%import
*调用导入宏;
如您所见,我添加了我的理解。就像我说的那样,宏对我来说是新的。我的理解可能不正确。我猜问题出在
call symputx(cats('dsn',_n_),compress(scan(filename,-2,'\.'), ,'ka'));
或者它可能在 import 语句中 out=&&dsn&i 因为它会快速覆盖以前的 SAS 文件,直到它完成所有文件。只是我需要所有的 sas 文件,而不仅仅是最后一个。
【问题讨论】: