好的,这是我的桌子(更大),并且具有不同的样式(a,B,C,D ...)。
+-------+-------+---------+
| Style | Range | Int |
+-------+-------+---------+
| A | 0-1 | . |
| A | 1-5 | . |
| A | 5-11 | . |
| A | 11-12 | . |
| A | 12-24 | -999999 |
| A | 24-36 | 0 |
| A | 36-48 | 0 |
| A | 48-60 | -999999 |
| A | 60+ | 0 |
+-------+-------+---------+
我想根据如下顺序更改每个-999999值:
if(int = -999999) and range ='12-24'
then get the first non null value in int from
11-12, 5-11, 1-5, 0-1 24-36, 36-48, 48-60, 60+ in that order
if(int = -999999) and range ='0-1' or '1-5' or '5-11' or '11-12'
then get the first non null value in int from
12-24, 11-12, 5-11, 1-5, 0-1 24-36, 36-48, 48-60, 60+ in that order without looking up self
if(int = -999999) and range ='24-36'
then get the first non null value in int from
36-48, 48-60, 60+, 12-24, 11-12, 5-11, 1-5, 0-1 in that order
if(int = -999999) and range ='36-48' or '48-60' or '60+'
then get the first non null value in int from
24-36, 36-48, 48-60, 60+, 12-24, 11-12, 5-11, 1-5, 0-1 in that order without looking up self
这会让我得到这张桌子:
+-------+-------+---------+
| Style | Range | Int |
+-------+-------+---------+
| A | 0-1 | . |
| A | 1-5 | . |
| A | 5-11 | . |
| A | 11-12 | . |
| A | 12-24 | 0 |
| A | 24-36 | 0 |
| A | 36-48 | 0 |
| A | 48-60 | 0 |
| A | 60+ | 0 |
+-------+-------+---------+
希望这很清楚。有更简单的方法可以做到这一点吗?
谢谢
答案 0 :(得分:1)
对于每种处理的样式,您都必须将所有int
值存储在一个数组(或哈希)中,并执行特殊的后退,然后转发集中在12-24
上的规则。
Q:假设您的样本数据具有0-1
-999999
。规则将从范围-999999
中选取12-24
。您是否搜索一次直到non-missing
,还是多次搜索直到non-missing
和non-<special-value>
。也许您认为这些数据将不会出现,但是数据不在乎您的想法:)
假定每种样式都具有全部九个范围。串行DOW循环将使您在分组的各个项目中排列int
组,然后根据查找规则处理每个项目。可以将这些规则编码在一个增量表中,该表指示下一个查找值在哪里。
data have; infile datalines dlm='|';
input
Style$ Range$ Int; datalines;
| A | 0-1 | . |
| A | 1-5 | . |
| A | 5-11 | . |
| A | 11-12 | . |
| A | 12-24 | -999999 |
| A | 24-36 | 0 |
| A | 36-48 | 0 |
| A | 48-60 | -999999 |
| A | 60+ | 0 |
run;
data want;
array ints(9) int_1 - int_9;
do _n_ = 1 by 1 until (last.style);
set have;
by style;
ints(_n_) = int;
end;
do _n_ = 1 to _n_;
set have;
if int = -999999 then do;
* traversal data for look up rules;
array rule[9,9] _temporary_ (
/* 0-1 */ +4 +3 +2 +1 +0 +5 +6 +7 +8
/* 1-5 */ +3 +2 +1 +0 -1 +4 +5 +6 +7
/* 5-11 */ +2 +1 +0 -1 -2 +3 +4 +5 +6
/* 11-12 */ +1 +0 -1 -2 -3 +2 +3 +4 +5
/* 12-24 */ -1 -2 -3 -4 +0 +1 +2 +3 +4
/* 24-36 */ +1 +2 +3 +0 -1 -2 -3 -4 -5
/* 36-48 */ -1 +0 +1 +2 -2 -3 -4 -5 -6
/* 48-60 */ -2 -1 +0 +1 -3 -4 -5 -6 -7
/* 60+ */ -3 -2 -1 +0 -4 -5 -6 -7 -8
);
do _m_ = 1 to 9 while (int = -999999);
step = rule(_n_,_m_);
if ints(_n_ + step) not in (., -999999) then
int = ints(_n_ + step);
end;
end;
output;
end;
drop int_: _m_ step;
run;
@Whymath询问如何制作rule
数组。
编码假定每个组都有每个范围,并且范围按问题中所述的顺序排序。
Array
Range Index
----- -----
0-1 1
1-5 2
5-11 3
11-12 4
12-24 5
24-36 6
36-48 7
48-60 8
60+ 9
范围数组索引用于rules
数组的第一维。
那么,对于范围0-1找到-999999怎么办?第二个如果告诉你
if(int = -999999) and range ='0-1' or '1-5' or '5-11' or '11-12' then get the first non null value in int from 12-24, 11-12, 5-11, 1-5, 0-1 24-36, 36-48, 48-60, 60+ in that order
第一个DO
循环使用该组的ints
值(假定每个范围都有一个值)填充int
数组(共9个值)。
因此,当-999999在0-1处找到时,if会按范围查找顺序对非null,非-999999进行查找:
0-1 range lookup index delta
index lookup index [0-1] index to lookup index
----- ------ ------ --------
1 12-24 5 +4
1 11-12 4 +3
1 5-11 3 +2
1 1-5 2 +1
1 0-1 1 0
1 24-36 6 +5
1 36-48 7 +6
1 48-60 8 +7
1 60+ 9 +8
向下查看索引增量,这些值将成为搜索非空非999999时要应用的步骤。索引增量是rules
表中的第二维,因此值将在二维数组初始化中使用。
对于其他8个范围,将逻辑转换重复为“步长”,您将获得9x9 rules
矩阵。
如果范围不一定完整,则可以使用哈希代替数组来完成每个组中的值和搜索规则。这是将Proc DS2
和多数据哈希用于规则的示例。
* implement loop over group_lookup[logic_lookup[range]];
* the inner lookup result becomes the key for the outer lookup;
proc ds2;
data want(overwrite=yes keep=(style range "int"));
declare char(8) style;
declare char(8) range key;
declare int "int" value;
declare package hash group_lookup([key], [value]);
declare package hash logic_lookup();
method init();
declare char(8) keys[9];
declare int index;
logic_lookup.keys([range]);
logic_lookup.data([key]);
logic_lookup.multiData('yes');
logic_lookup.defineDone();
keys := ('12-24' '11-12' '5-11' '1-5' '0-1' '24-36' '36-48' '48-60' '60+');
range = '0-1'; do index = 1 to dim(keys); key = keys[index]; if key ne range then logic_lookup.add(); end;
range = '1-5'; do index = 1 to dim(keys); key = keys[index]; if key ne range then logic_lookup.add(); end;
range = '5-11'; do index = 1 to dim(keys); key = keys[index]; if key ne range then logic_lookup.add(); end;
range = '11-12'; do index = 1 to dim(keys); key = keys[index]; if key ne range then logic_lookup.add(); end;
range = '12-24'; do index = 1 to dim(keys); key = keys[index]; if key ne range then logic_lookup.add(); end;
keys := ('24-36' '36-48' '48-60' '60+' '12-24' '11-12' '5-11' '1-5' '0-1' '24-36');
range = '24-36'; do index = 1 to dim(keys); key = keys[index]; if key ne range then logic_lookup.add(); end;
range = '36-48'; do index = 1 to dim(keys); key = keys[index]; if key ne range then logic_lookup.add(); end;
range = '48-60'; do index = 1 to dim(keys); key = keys[index]; if key ne range then logic_lookup.add(); end;
range = '60+'; do index = 1 to dim(keys); key = keys[index]; if key ne range then logic_lookup.add(); end;
end;
method findReplacement(char(10) range, int in_value) returns int;
declare int result rc;
if in_value not in (. -999999) then return in_value;
if logic_lookup.find() eq 0 then do; * hash host variables [range] and [key] are global, do not mask them with local variables of the same name;
if group_lookup.find() eq 0 then do; * hash host variables [keys] and [value] are global, do not mask them with local variables of the same name;
if value not in (. -999999) then return value;
do while (logic_lookup.has_next() = 0); * search the other rule ranges listed in the multidata;
if logic_lookup.find_next() = 0
& group_lookup.find() = 0
& value not in (. -999999) then return value;
end;
end;
end;
else do;
put 'ERROR: Invalid range in data,' range=;
end;
return in_value;
end;
method run();
declare int rc;
declare double index;
group_lookup.clear();
do index = 1 to CONSTANT('BIG') until(last.style);
set have(locktable=share);
by style;
group_lookup.add([range],["int"]); * key -> value;
end;
do index = 1 to index;
set have;
"int" = findReplacement(range, "int");
output;
end;
end;
enddata;
run;
quit;
%let syslast = want;