SAS:跨多个列对ID进行比较

SAS:跨多个列对ID进行比较,sas,comparison,multiple-columns,Sas,Comparison,Multiple Columns,我处理的数据来自于“指出所有适用的”问题。两名评分员被要求填写一份独特的主题列表。数据看起来像这样 ID| Rater|Q1A|Q1B|Q1C|Q1D ------------------------ 1 | 1 | A | F | E | B 1 | 2 | E | G | 2 | 1 | D | C | A 2 | 2 | C | D | A 我想比较两位评分员对每个ID的答案,并确定Q1A-Q1D的答案是否相同。我对Q1A、Q1B等的每个评级机构之间的ID直接比

我处理的数据来自于“指出所有适用的”问题。两名评分员被要求填写一份独特的主题列表。数据看起来像这样

ID| Rater|Q1A|Q1B|Q1C|Q1D
------------------------
1 | 1    | A | F | E | B
1 | 2    | E | G | 
2 | 1    | D | C | A
2 | 2    | C | D | A

我想比较两位评分员对每个ID的答案,并确定Q1A-Q1D的答案是否相同。我对Q1A、Q1B等的每个评级机构之间的ID直接比较不感兴趣。我想知道Q1A-Q1D中作为一个集合的所有值是否相同。(例如,在上面的示例数据中,ID 2的评分员是相同的)。我假设我会用数组来做这件事。谢谢

这看起来像是呼叫sortc的作业:

data have;
infile cards missover;
input ID  Rater (Q1A Q1B Q1C Q1D) ($);
cards;
1   1      A   F   E   B
1   2      E   G   
2   1      D   C   A
2   2      C   D   A
3   1      A   B   C
3   2      A   B   D
;
run;

/*You can use an array if you like, but this works fine too*/
data temp /view = temp;
  set have;
  call sortc(of q:);
run;

data want;
  set temp;
  /*If you have more questions, extend the double-dash list to cover all of them*/
  by ID Q1A--Q1D notsorted;
  /*Replace Q1D with the name of the variable for the last question*/
  IDENTICAL_RATERS = not(first.Q1D and last.Q1D);
run;

这里有一个类似的解决方案,也使用
调用sortc
,但使用向量和保留变量

创建示例数据集 做比较 结果
排序、连接,然后比较

data want ;
  set ratings;
  by id;
  call sortc(of Q1A -- Q1D);
  rating = cats(of Q1A -- Q1D);
  retain rater1 rating1 ;
  if first.id then rater1=rater;
  if first.id then rating1=rating;
  if not first.id ;
  rater2 = rater ;
  rating2 = rating;
  match = rating1=rating2 ;
  keep id rater1 rater2 rating1 rating2 match;
run;

谢谢,但这并不能告诉我这两个评分员是否相同。它将值按顺序排列-但是如果一个评分员的值是A、B、D、E、G,而第二个评分员的值是A、B、D、E、F、G-这段代码不能帮助我系统地确定它们是否不同。我对第三个数据步骤做了一些调整-而不是仅仅过滤掉任何不同的对,现在,它使用一个变量来标记相同的评分者对(如果我理解了您对“相同”的定义的话)。
data compare(keep=ID EQUAL);
  set ratings;
  by ID;
  format PREV_1A PREV_Q1B PREV_Q1C PREV_Q1D $1.
         EQUAL 1.;
  retain PREV_:;
  call sortc(of Q1:);
  array Q(4) Q1:;
  array PREV(4) PREV_:;
  if first.ID then do;
    do _i = 1 to 4;
      PREV(_i) = Q(_i);
    end;
  end;
  else do;
    EQUAL = 1;
    do _i = 1 to 4;
      if Q(_i) NE PREV(_i) then EQUAL = 0;
    end;
    output;
  end;
run;
ID EQUAL 
1  0 
2  1 
3  0 
data want ;
  set ratings;
  by id;
  call sortc(of Q1A -- Q1D);
  rating = cats(of Q1A -- Q1D);
  retain rater1 rating1 ;
  if first.id then rater1=rater;
  if first.id then rating1=rating;
  if not first.id ;
  rater2 = rater ;
  rating2 = rating;
  match = rating1=rating2 ;
  keep id rater1 rater2 rating1 rating2 match;
run;