如何比较SAS中的表结构

发布于 2024-09-25 01:09:59 字数 113 浏览 12 评论 0 原文

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

有人可以帮忙吗?

I am a tester and I need to compare two data sets structure (not table data) in SAS.
I tried to use 'proc compare' but it compares the data. I want to compare dataset/table structure (column name, data type, null constraints etc.)

Can any one please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晒暮凉 2024-10-02 01:09:59

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

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

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

* 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 中可用。

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

You can interrogate the views in SASHELP (vtable, vcolumn etc) to do this. A quick way would be to create a temporary table from sashelp.vcolumn for each of the two tables you want to compare, then use a PROC SQL join to compare them. Then you'll be comparing the structures, which is represented in the data from vcolumn.

To get started with this, have a look at what's in SASHELP.vcolumn.

Here is a basic example of employing this method, to compare variables in 2 datasets.

* 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;

You can generalise from this for other attributes such as data type, length, label etc., all of which are available in vcolumn.

  • Note that you may need to alter this code to accommodate librefs that your data sets may have.
愛放△進行李 2024-10-02 01:09:59

您可以使用proc content将描述符部分写入数据集,然后使用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;

You can write the descriptor portions to data sets using proc contents then use proc compare to see how their structures differ. The out2 option will write out integrity constraints if they exist. If not the data set will be empty. Some columns like CRDATE (creation date) or LIBNAME or MEMNAME, may be expected to differ and so you may wish to exclude those from the comparison.

/* 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;
草莓味的萝莉 2024-10-02 01:09:59

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

2- PROC 对所有内容输出进行排序

3- 使用 PROC COMPARE。

IE

***********************************************;
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 ************;

1- Use PROC CONTENTS to get your dataset description (dataset name, variable name, variable label, variable type....)

2- PROC SORT all of the content output

3- Use a PROC COMPARE.

i.e

***********************************************;
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 ************;
南风几经秋 2024-10-02 01:09:59

您还可以使用 清单表

在我最近的博客文章如何比较 SAS 数据表中的常见/不常见列,我使用以下代码示例展示了如何执行此操作:

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;

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

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;

运行此结果代码将是以下清单表:
输入图片此处描述

有关更多详细信息,请参阅博客文章 如何比较 SAS 数据表中常见/不常见列

You can also compare SAS tables structures side-by-side visually using checklist tables.

In my recent blog post How to compare SAS data tables for common/uncommon columns, I show how to do this using the following code example:

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;

Then we can compare 2 datasets as follows:

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;

The result of running this code will be the following checklist table:
enter image description here

For additional details see the blog post How to compare SAS data tables for common/uncommon columns

客…行舟 2024-10-02 01:09:59

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

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

You can use PROC COMPARE, just use the OBS=0 dataset option on each input dataset so there is no data to compare.

proc compare data=old(obs=0) compare=new(obs=0);
run;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文