SAS中文论坛

标题: sas中的哈希表 [打印本页]

作者: shiyiming    时间: 2011-1-15 14:19
标题: sas中的哈希表
用sas中的hash表可以实现两个数据集的匹配,例如:
data Treatment;
  input pscoreT idT@@;
datalines;
0.110 1
0.130 6
0.110 2
;
data Control;
  input pscoreC idC @@;
datalines;
0.334 3
0.110 5
0.131 4
;

data Matched(keep= IdSelectedControl MatchedToTreatID);
    length pscoreC 8;
    length idC 8;
     if _N_= 1 then do;
        declare hash h(dataset: "Control", ordered: 'no');
        declare hiter iter('h');
        h.defineKey('idC');
        h.defineData('pscoreC', 'idC');
        h.defineDone();     
       call missing(idC, pscoreC);
    end;
    set Treatment;
    retain BestDistance 99;
    rc= iter.first();
    if (rc=0) then BestDistance= 99;
    do while (rc= 0);
       ScoreDistance= abs(pscoreT - pscoreC);
       if ScoreDistance < BestDistance then do;
         BestDistance= ScoreDistance;
          IdSelectedControl= idC;  
          MatchedToTreatID= idT;
       end;
       rc= iter.next();
      if (rc~= 0) then do;
          output;
          rc1= h.remove(key: IdSelectedControl);
      end;
    end;
run;
如果两个数据集各分了k类,怎样实现按类匹配呢?(相当于做了k次匹配,每次k值相同的做匹配)例如,k为两类的数据集如下:data Treatment;
  input pscoreT idT k@@;
datalines;
0.110 1 1
0.130 6 1
0.110 2 1
0.112 9 2
0.113 11 2
0.115 12 2
;
data Control;
  input pscoreC idC k@@;
datalines;
0.334 3 1
0.110 5 1
0.131 4 1
0.120 7 2
0.132 8 2
0.125 10 2
;
匹配代码该怎么写?
作者: shiyiming    时间: 2011-1-25 17:43
标题: Re: sas中的哈希表
data Treatment;
input pscoreT idT k1@@;
datalines;
0.110 1 1
0.130 6 1
0.110 2 1
0.112 9 2
0.113 11 2
0.115 12 2
;
data Control;
input pscoreC idC k2@@;
datalines;
0.334 3 1
0.110 5 1
0.131 4 1
0.120 7 2
0.132 8 2
0.125 10 2
;

data Matched(keep= IdSelectedControl MatchedToTreatID);
        if _N_= 1 then do;
                if 0 then set Control;
                declare hash h(dataset: "Control", ordered: 'no');
                declare hiter iter('h');
                h.defineKey('idC','k2');
                h.defineData(all:"yes");
                h.defineDone();
                call missing(idC, pscoreC);
        end;
        set Treatment;

        retain BestDistance 99;
        rc= iter.first();

        if (rc=0) then BestDistance= 99;

        do while (rc= 0);
                if k1=k2 then do;
                        ScoreDistance= abs(pscoreT - pscoreC);
                        if ScoreDistance < BestDistance then do;
                                BestDistance= ScoreDistance;
                                IdSelectedControl= idC;
                                MatchedToTreatID= idT;
                                MatchedK=k2;
                        end;
                end;
                rc= iter.next();
                if (rc~= 0) then do;
                        output;
                        rc1= h.remove(key: IdSelectedControl,key:MatchedK);
                end;
        end;
run;
作者: shiyiming    时间: 2011-1-25 19:48
标题: Re: sas中的哈希表
thanks!
作者: shiyiming    时间: 2011-3-7 09:50
标题: Re: sas中的哈希表
hash 可以实现两组间的匹配,那三组和三组以上的匹配该怎样实现?




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2