1

我想在 SAS 中导入具有重复列名的 CSV 文件。例子

名称 abc abc def def

是我文件中的列名。

当我尝试在 SAS 中导入文件时,它会自动将变量重命名为 var1、var2 等。

我想保留原始列名。我怎样才能做到这一点?

4

3 回答 3

1

你可以用几种不同的方式来做。执行示例的最简单方法如下所示:

data want;
infile datalines dlm=',';
input name $ abc1-abc2 def1-def2;
datalines;
John,1,2,3,4
Jack,4,5,6,7
;;;;
run;

proc print data=want;
run;

显然 1-2 可以变成 1-35 或其他。

如果您的数据比这更复杂,使用交错变量,我建议只阅读标题行,转置它,然后从中构造一个输入语句。

data varnames; *this dataset reads the header in only - use infile with obs option for actual csv;
input varname $ @@;
order+1; *so we can get back to proper order;
datalines;
Name abc abc def def
;;;;
run;

proc sort data=varnames; *sort by varname;
by varname;
run;

data input_statement;
set varnames;
by varname;
if first.varname then counter=0;
if not (first.varname and last.varname) then do; *if this is not the only varname with same varname;
 counter+1;                          *increment counter;
 varname=cats(varname,counter);      *append counter to varname;
end;
run;

proc sort data=input_statement;   *back to correct order;
by order;
run;

proc sql;   *select the new names into a macro variable;
select varname into :namelist separated by ' ' from input_statement;
quit;

data want;  *input them;
infile datalines dlm=',';
input (&namelist) ($);  *here I just make them all CHAR.  If you have all NUM except the name var, 
                         you could leave name out of your datasets and include it separately.;
datalines;
John,1,2,3,4
Jack,4,5,6,7
;;;;
run;
于 2013-08-24T19:25:14.447 回答
0

文档看来,您会将变量名放在第一行并使用GETNAMES=yes.

proc import datafile="C:\temp\test.csv"
   out=shoes
   dbms=csv
   replace;
   getnames=yes;
run;

proc print;
run;
于 2013-08-24T12:04:49.830 回答
0

SAS 数据集中不能有重复的列名。您必须将它们重命名为;如果你不喜欢proc import正在做的事情,你可以通过手动重命名它们proc datasets

proc datasets;
    modify <dataset>;
    rename var1=name var2=abc1 var3=abc2 var4=def1 var5=def2;
quit;
于 2013-08-24T13:20:51.587 回答