下面程序是此问题用 hash object 的做法, 其中包括了用hash做分组求和,填充缺失值,和指定闭区间求和。
- data a;
- input value x subjid $;
- cards;
- 1 1 001
- 2 1 001
- 3 2 001
- 4 2 001
- 5 2 001
- 6 3 001
- 7 3 001
- 8 5 001
- 1 1 002
- 2 1 002
- 3 2 002
- 4 2 002
- 5 2 002
- 6 3 002
- 7 3 002
- 8 5 002
- ; run;
- data b(keep = subjid xx sum_x);
- length subjid xx $3.;
- declare hash h(ordered: 'Y');
- h.definekey('subjid', 'x');
- h.definedata('subjid', 'x', 'vsum');
- h.definedone();
- declare hiter hi ('h');
- do until (eof); /* load the sum of values for each subjid_x */
- set a end = eof;
- rc = h.check();
- if rc ne 0 then do;
- vsum = value;
- rc1 = h.add(); end;
- else if rc eq 0 then do;
- vsum ++ value;
- rc1 = h.replace(); end;
- end;
- do subjid = '001', '002'; /* fill the missing x */
- do x = 1 to 5;
- rc = h.find();
- if rc ne 0 then do;
- vsum = 0;
- rc = h.replace(); end;
- end;
- end;
-
- do subjid = "001", "002"; /* calculating expected sum */
- do i = 1 to 4;
- x = i;
- rc = h.find();
- sum_x = vsum;
- x = x + 1;
- rc = h.find();
- sum_x = sum_x + vsum;
- xx = catx('_', i, x);
- output;
- end;
- sum_x = 0;
- do x = 1 to 5;
- rc = h.find();
- sum_x ++ vsum;
- end;
- xx = catx('_', '1', '5');
- output;
- end;
- run;


雷达卡




京公网安备 11010802022788号







