SAS 查找表以匹配数据
SAS lookup tables to match data
我正在尝试通过查找评分系统 table 创建评分 table。三个老师给所有的学生打分,他们有自己的打分方式。我正在尝试通过映射到查找 table 来标准化学生的分数。我的 table 看起来像这样:
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
两次查找 table 具有相同的内容,我可以使用 table 中的任何一个作为映射 table.
我希望我的新 table 看起来像这样
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
我知道我可以通过 multiple join 来做到这一点,这会使代码很长,并且当在查找中添加更多教师时我需要很长时间来修改代码 table。因此我想找到一种不使用连接的更自动化的方式。我正在考虑使用散列对象,但查找 table1 中的评级不是唯一的,除非它与教师列结合使用。也许我可以使用 proc IML 来解决这个问题?有没有简单的方法来创建这样的 table?
就用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;
我正在尝试通过查找评分系统 table 创建评分 table。三个老师给所有的学生打分,他们有自己的打分方式。我正在尝试通过映射到查找 table 来标准化学生的分数。我的 table 看起来像这样:
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
两次查找 table 具有相同的内容,我可以使用 table 中的任何一个作为映射 table.
我希望我的新 table 看起来像这样
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
我知道我可以通过 multiple join 来做到这一点,这会使代码很长,并且当在查找中添加更多教师时我需要很长时间来修改代码 table。因此我想找到一种不使用连接的更自动化的方式。我正在考虑使用散列对象,但查找 table1 中的评级不是唯一的,除非它与教师列结合使用。也许我可以使用 proc IML 来解决这个问题?有没有简单的方法来创建这样的 table?
就用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;