如何在SAS中导入包含重复列名的csv文件

时间:2013-08-24 11:46:57

标签: sas

我想在SAS中导入包含重复列名的CSV文件。实施例

命名abc abc def def

列名是否在我的文件中。

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

我想保留原始列名。我怎么能这样做?

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

docs看起来您会将变量名称放在第一行并使用GETNAMES=yes

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

proc print;
run;

答案 2 :(得分:0)

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

proc datasets;
    modify <dataset>;
    rename var1=name var2=abc1 var3=abc2 var4=def1 var5=def2;
quit;