如何比较SAS中的表结构

时间:2010-09-29 06:21:08

标签: data-structures sas compare

我是测试人员,我需要比较SAS中的两个数据集结构(不是表格数据)。 我尝试使用'proc compare'但它会比较数据。我想比较数据集/表结构(列名,数据类型,空约束等)

任何人都可以帮忙吗?

5 个答案:

答案 0 :(得分:6)

您可以在SASHELP(vtable,vcolumn等)中查询视图来执行此操作。一种快速的方法是从sashelp.vcolumn为要比较的两个表中的每一个表创建一个临时表,然后使用PROC SQL连接来比较它们。然后,您将比较结构,这些结构在vcolumn的数据中表示。

要开始使用它,请查看SASHELP.vcolumn中的内容。

以下是使用此方法比较2个数据集中的变量的基本示例。

* provide names of the two data sets here ;
%let ds1=TheFirstDataSet;
%let ds2=TheOtherDataSet;

* upcase the data set names ;
%let ds1=%sysfunc(upcase(&ds1));
%let ds2=%sysfunc(upcase(&ds2));

proc sql;
* retrieve info on these tables from sashelp.vcolumn;
  create table first as select * from sashelp.vcolumn where upcase(memname)="&ds1";
  create table second as select * from sashelp.vcolumn where upcase(memname)="&ds2";
* join these data sets and report on differences for var names;
  select coalescec(f.name,s.name) as varName
        ,case
          when f.name is null then "This var is in &ds2 only"
          when s.name is null then "This var is in &ds1 only"
          else 'This var is in both data sets'
          end as DiffDescription
  from 
    first as f
    full outer join 
      second as s 
      on f.name=s.name
  ;
quit;

您可以从中推广其他属性,例如数据类型,长度,标签等,所有这些属性都可以在vcolumn中找到。

  • 请注意,您可能需要更改此代码以适应数据集可能具有的libref。

答案 1 :(得分:3)

您可以使用proc contents将描述符部分写入数据集,然后使用proc compare查看其结构的不同之处。 out2选项将写出完整性约束(如果存在)。如果不是,则数据集将为空。某些列(如CRDATE(创建日期)或LIBNAME或MEMNAME)可能会有所不同,因此您可能希望从比较中排除这些列。

/* create some fake data similar to an existing one */
proc sql;
create table myclass as 
  select *, "foo" as newcol from sashelp.class
;
/* modify it */
  insert into myclass
     values ("George", "M", 17, 72, 169,"foo");
/* add an index */
  create index names on
     work.myclass(name, age);
quit;

/* write out descriptor portions to data sets */
proc contents data=myclass out=ds1 out2=ds2;run;
/* sashelp.class doesn't have an index so ds2a will not exist */
proc contents data=sashelp.class out=ds1a out2=ds2a;run;

/* compare data set structures */
proc compare data=ds1 compare=ds1a;run;

答案 2 :(得分:1)

1-使用PROC CONTENTS获取数据集描述(数据集名称,变量名称,变量标签,变量类型....)

2- PROC SORT所有内容输出

3-使用PROC COMPARE。

***********************************************;
proc content data=table1 out=cont1 noprint;
run;

proc content data=table2 out=cont2 noprint;
run;

proc sort data=cont1; 
  by memname name;
run;

proc sort data=cont2; 
  by memname name;
run;

proc compare listvar
  base=cont1
  compare=cont2;
  id memname name;
run;
******************* END ************;

答案 3 :(得分:0)

您还可以使用checklist tables并排比较SAS表结构。

在我最近的博客文章How to compare SAS data tables for common/uncommon columns中,我使用以下代码示例演示如何做到这一点:

data WORK.NEWCARS (drop=temp:);
   set SASHELP.CARS (rename=(Origin=Region EngineSize=temp1 Make=temp2));
   length EngineSize $3 Make $20;
   EngineSize = put(temp1,3.1);
   Make = temp2; 
   label Type='New Car Type';
run;

然后我们可以比较两个数据集,如下所示:

proc contents data=SASHELP.CARS noprint out=DS1(keep=Name Type Length Label);
run;

proc contents data=WORK.NEWCARS noprint out=DS2(keep=Name Type Length Label);
run;

data comparison_matrix;
   merge
      DS1(in=in1 rename=(Type=Typ1 Length=Len1 Label=Lab1))
      DS2(in=in2 rename=(Type=Typ2 Length=Len2 Label=Lab2));
   by Name;

   /* set symbol shape: 1=V; 0=X */
   ds1 = 1; ds2 = 1;
   if in1 and not in2 then ds2 = 0; else
   if in2 and not in1 then ds1 = 0;

   /* add background color */
   if ds1=ds2=1 then
   select;
      when(Typ1^=Typ2) do; ds1=2; ds2=2; end;
      when(Len1^=Len2) do; ds1=3; ds2=3; end;
      when(Lab1^=Lab2) do; ds1=4; ds2=4; end;
      otherwise; 
   end;

   label
      Name = 'Column Name'
      ds1 = 'SASHELP.CARS'
      ds2 = 'WORK.NEWCARS'
      ;
run;

proc format;
   value chmark
      0   = '(*ESC*){unicode "2718"x}'
      1-4 = '(*ESC*){unicode "2714"x}'
      ;
   value chcolor
      0   = red
      1-4 = green
      ;
   value bgcolor
      2 = 'cxffccbb'
      3 = 'cxffe177'
      4 = 'cxd4f8d4' 
      ;
run;

ods html path='c:\temp' file='comp_marix.html' style=Seaside;
ods escapechar='^';
title 'Data set columns comparison matrix';

proc odstext;
   p '<div align="center">Mismatch Legend:'||
     '<span style="background-color:#ffccbb;margin-left:17px">^_^_^_^_</span> Type'||
     '<span style="background-color:#ffe177;margin-left:17px">^_^_^_^_</span> Length'||
     '<span style="background-color:#d4f8d4;margin-left:17px">^_^_^_^_</span> Label</div>'
   / style=[fontsize=9pt];
run;

title; 
proc print data=comparison_matrix label noobs;
   var Name / style={fontweight=bold width=100px};
   var ds1 ds2 / style={color=chcolor. backgroundcolor=bgcolor. just=center fontweight=bold width=120px};
   format ds1 ds2 chmark.;
run;

ods html close;

运行此代码的结果将是以下清单表: enter image description here

有关其他详细信息,请参见博客文章How to compare SAS data tables for common/uncommon columns

答案 4 :(得分:0)

您可以使用PROC COMPARE,只需在每个输入数据集上使用OBS = 0数据集选项,这样就没有要比较的数据。

proc compare data=old(obs=0) compare=new(obs=0);
run;