【问题标题】:How to compare table structure in SAS如何比较SAS中的表结构
【发布时间】:2010-09-29 06:21:07
【问题描述】:

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

有人可以帮忙吗?

【问题讨论】:

    标签: data-structures sas compare


    【解决方案1】:

    您可以查询 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。

    【讨论】:

      【解决方案2】:

      您可以使用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;
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          您还可以使用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;
          

          然后我们可以比较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;
          

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

          更多详情请参阅博文How to compare SAS data tables for common/uncommon columns

          【讨论】:

            【解决方案5】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-12-23
              • 1970-01-01
              • 1970-01-01
              • 2020-05-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多