SAS比较两个具有不同列顺序的表

时间:2018-11-08 20:52:08

标签: sas

嗨,我有两个表具有不同的列顺序,并且列名不是大写的。如果这两个表的内容相同,该如何比较?

例如,我有两个表的学生成绩

  table A:

        Math   English  History
      -------+--------+---------  
   Tim   98      95        90
  Helen 100      92        85


 table B:

        history   MATH    english
       --------+--------+---------  
   Tim   90       98        95
  Helen  85      100        92

2 个答案:

答案 0 :(得分:0)

您可以使用两种方法中的任何一种进行比较,而不考虑顺序或列名

/*1. Proc compare*/

proc sort data=A; by name; run;
proc sort data=B; by name; run;

proc compare base=A compare=B;
   id name;
run;

/*2. Proc SQL*/
proc sql;
select Math, English, History from A
<union/ intersect/ Except>
select MATH, english, history from B;
quit; 

答案 1 :(得分:0)

使用除corr(对应)以外的其他名称,它将按名称进行检查。如果一切都匹配,您将得到零记录。

 data have1;
 input  Math   English  History;
datalines;
1 2 3
;
run;

data have2;
input English math History;
datalines;
 2 1 3
;
run;



proc sql ;
select * from have1
 except corr
 select * from have2;

edit1

如果要检查哪一列不同,您可能需要进行转置和比较,如下例所示。

data have1;
 input name $ Math   English pyschology History;
 datalines;
  Tim 98      95     76   90
 Helen 100      92   55     85
  ;
 run;

data have2;
input name $ English Math pyschology History;
datalines;
Tim       95   98  76   90
 Helen    92  100 99    85
 ;
run;

 proc sort data = have1 out =hav1;
 by name;
run;

 proc sort data = have2 out =hav2;
 by name;
  run;

 proc transpose data =hav1 out=newhave1 (rename = (_name_= subject 
 col1=marks));
 by name;
  run;

  proc transpose data =hav2 out=newhave2 (rename = (_name_= subject 
 col1=marks));
 by name;
 run;


 proc sql;
create table want(drop=mark_dif) as
 select 
 a.name as name
,a.subject as subject
,a.marks as have1_marks
,b.marks as have2_marks
,a.marks -b.marks as mark_dif
from  newhave1 a inner join newhave2 b
on upcase(a.name)  = upcase(b.name)
and upcase(a.subject) =upcase(b.subject)
where calculated mark_dif ne 0;