SAS查找表以匹配数据

时间:2018-12-28 05:08:17

标签: sas lookup-tables

我正在尝试通过查找评分系统表来创建评分表。共有三位老师为所有学生评分,他们有自己的评分方式。我正在尝试通过映射到查找表来标准化学生的评分。我的表如下所示:

 old grades table:
              prof_grade      TA_grade      chair_grade
  Anne           A+               A             AAA
  Peter          B+               B+            AA

  Look up table1:
             Score   Rating  Teacher
              10       A+      prof
              10       A        TA
              10      AAA      chair
               9       A       prof
               9       A-       TA
               9      AA       chair
               8      B+       prof
               8      B+        TA
               8      A        chair

  Look up table2:
             Prof    TA    chair
    10        A+      A     AAA
     9        A       A-     AA
     8        B+      B+      A

两个查找表具有相同的内容,我可以将其中任何一个表用作映射表。

我希望我的新桌子看起来像这样

  new grades table:
         prof_grade   TA_grade   chair_grade   prof_score   TA_score chair_score
  Anne         A+       A           AAA          10           10          10
  Peter        B+       B+          AA            8            8           9

我知道我可以通过多次联接来做到这一点,这将使代码变长,并且当在查找表中添加更多教师时,我需要很长时间来修改代码。因此,我想找到一种不使用联接的更自动化的方法。我正在考虑使用哈希对象,但查找表1中的Rating并非唯一,除非将其与Teacher列结合使用。也许我可以使用proc IML解决此问题?有没有简单的方法来创建这样的表?

1 个答案:

答案 0 :(得分:0)

仅使用proc格式,就非常简单直接。

    data have;
input  name   $        prof_grade   $   TA_grade   $   chair_grade $;
datalines;
Anne           A+               A             A+
Peter          B+               B+            AAA
Pete           A+              A-            AA
;



 /* your lookup table for creating informats*/
  data lookup;
  input  Score   Rating  $  Teacher $;
 datalines;
      10       A+      prof
      10       A        TA
      10       AAA      chair
       9       A       prof
       9       A-       TA
       9       AA       chair
       8       B+       prof
       8       B+        TA
       8       A+       chair
   ;

  /* creating informat*/
 proc sql ;                               
 create table crfmt as                        
 select distinct 
     Teacher as fmtname, 
   strip(Rating) as start,          
   score as label,                   
   "J" as type
   from lookup;                        
 quit;    

proc format library=work cntlin=crfmt fmtlib;
run;


/* using the informat created in the table above in first 2 cases score are 
character values you need to use one more input change to number as shown below*/

 data want;
 set have;
 Prof_score = input(trim(prof_grade),$prof.);
 TA_score = input(trim(TA_grade),$TA.);
  /* to make it numeric value*/
  chair_score = input(input(trim(chair_grade),$chair.),best32.);
  run;

Edit1:如果要寻址其他值。请使用以下代码

   data have;
  input  name   $        prof_grade   $   TA_grade   $   chair_grade $;
  datalines;
  Anne           A+               A             A+
 Peter           B+               B+            AAA
 Pete            A+              A-            AA
 Smith          A+              A-            AAA1A
;



 /* your lookup table for creating informats*/
  data lookup;
 infile datalines missover;
  input  Score $  Rating  $  Teacher $;
 datalines;
  10       A+      prof
  10       A        TA
  10       AAA      chair
   9       A       prof
   9       A-       TA
   9       AA       chair
   8       B+       prof
   8       B+        TA
   8       A+       chair
   ;

  /* insert rows in lookup to address other values*/
    proc sql;
   insert into lookup   
  values(" ", "Unknown" ,   "chair");
  insert into lookup  
 values(" ", "Unknown" ,   "TA");
  insert into lookup
   values(" ", "Unknown" ,   "prof");

 /* creating informat*/
  proc sql ;                               
    create table crfmt as                        
   select distinct 
   Teacher as fmtname, 
   strip(Rating) as start,          
  score as label,                   
  "J" as type
  from lookup;                        
quit;    

 proc format library=work cntlin=crfmt fmtlib;
 run;


/* using the informat created in the table above in first 2 cases score are 
 character values you need to use one more input change to number as shown below*/

  data want;
    set have;
  if input(trim(prof_grade),$prof.) eq prof_grade
   then prod_score = ' ';
   else  prod_score = input(trim(prof_grade),$prof.);
  ;
   if input(trim(TA_grade),$TA.) eq TA_grade
   then TA_score = ' ';
    else  TA_score = input(trim(TA_grade),$TA.);

   if input(trim(Chair_grade),$chair.) eq Chair_grade
   then chair_score = ' ';
    else  chair_score = input(trim(chair_grade),$chair.);
   run;