您的位置:首页 > 其它

SAS  hash 树与多维临时数组的比较

2015-09-18 15:47 141 查看
代码来自SAS 官网

 

data bteam;

  infile datalines;

  input lname : $10. gender $ height weight
type;

datalines;

Adams M 67 160 2

Alexander M 69 115 1

Apple M 69 139 1

Arthur M 66 125 2

Avery M 66 152 2

Barefoot M 68 158 2

Baucom M 70 170 3

Blair M 69 133 1

Blalock M 68 148 2

Bostic M 74 170 3

;;;

proc print data=bteam;

  title "BTEAM";

run;

data ideal;

  infile datalines;

  input height small medium large;

datalines;

66 126 138 149

67 130 141 154

68 134 145 158

69 138 149 162

70 142 153 167

71 146 157 172

72 150 161 177

73 154 165 181

74 158 169 185

75 162 173 189

;;;

proc print data=ideal;

  title "IDEAL";

run;

data inshape outofshape;

  keep lname height weight type;

  array wt(66:75,3) _temporary_;

  if _n_=1 then do;

    do i=1 to
all;

     
set ideal nobs=all;

     
wt(height,1)=small;

     
wt(height,2)=medium;

     
wt(height,3)=large;

    end;

  end;

  set bteam;

  if gender='M' and (1 le type le 3) and (66 le
height le 75) then do;

    if
wt(height,type)-5 le weight le wt(height,type)+5 then output
inshape;

    else output
outofshape;

  end;

  else putlog 'WARNING: Observation out of lookup
range: ' _all_;

run;

proc print data=inshape;

  title "Example 4.2 INSHAPE Data Set Created with
DATA Step";

run;

proc print data=outofshape;

  title "Example 4.2 OUTOFSHAPE Data Set Created
with DATA Step";

run;

/***************************************************************************data
inshape outofshape;

  attrib lname length=$10

        
gender length=$1

        
height length=8

        
weight length=8;

  keep lname gender height weight;

  if _n_=1 then do;

    declare hash
wt(dataset:'ideal');

   
wt.defineKey('height');

   
wt.defineData('small','medium','large');

   
wt.defineDone();

    call
missing(small,medium,large);

  end;

  set bteam;

  if 66 le height le 75 and gender='M' and type
in (1,2,3) then do;

   
rc=wt.find();

    if rc=0 then
do;

     
if type=1 then ideal=small;

     
else if type=2 then ideal=medium;

     
else if type=3 then ideal=large;

     
if ideal-5 le weight le ideal+5 then output inshape;

     
else output outofshape;

    end;

    else putlog
'WARNING: Height not found in hash object: ' _all_;

  end;

  else putlog 'WARNING: Observation out of range:
' _all_;

run;

proc print data=inshape;

  title "Example 4.2 Related Technique INSHAPE
Data Set Created with DATA Step";

run;

proc print data=outofshape;

  title "Example 4.2 Related Technique OUTOFSHAPE
Data Set Created with DATA Step";

run;

最后实现的结果都是



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: