给定表格中的某些条件,我必须为每条记录分配不同的单元格值。
在图像中,column1= elig, column2=status, column3= type, column4 =metro, column5=quartile, column6=urb, column7=cell
。我刚刚在 if then else 中编写的前 3 个条件(它们来自另一个表)。
但是一旦我开始使用变量 `quartile、urb 和 type,我需要一个比我写的更好的代码。
- Metro 将从 1-4 递增。
- 四分位数随其他记录递增。
- 以组的形式键入更改。
对于第一个块type = 1
,对于第二个块type maybe be 4 or 5
(不一定是增量的)。下一个类型组可能是type 7,8,9,10
.
只有每条记录的单元格值会发生变化。我知道我可以替换宏变量来缩短名称并节省输入,但我怎样才能使这段代码更紧凑、更高效。
提前谢谢了。
If elig=0 then cell=0;
else if elig =1 then
do;
if status in ('2','3') then cell=1;
else if ( status = ' ' and typec=25 ) then cell =2;
else if (status ='1','4','') and (quartile = . ) then cell=2;
end;
else if elig= '1' and type =1 and metro eq='1' then
do;
if quartile = 1 and urb in ('1','2') then cell =1111;
else if quartile = 1 and urb = '3' then cell =1112;
else if quartile = 2 and urb in ('1','2') then cell =1121;
else if quartile = 2 and urb = '3' then cell =1122;
else if quartile =3 and urb in ('1','2') then cell =1141;
else if quartile = 3 and urb = '3' then cell =1142;
else if quartile = 4 and urb in ('1','2') then cell =1121;
else if quartile = 4 and urb = '3' then cell =1172;
end;
/*here will be 3 more blocks of code for metro =2,3,4* /
/*note type changes value after metro cycles through 4 iterations*/
else if elig='1' and type =('4','5') and metro eq='1 then
do;
if quartile = 1 and urb in ('1','2') then cell =1211;
else if quartile = 1 and urb = '3' then cell =1212;
else if quartile = 2 and urb in ('1','2') then cell =1221;
else if quartile = 2 and urb = '3' then cell =1222;
else if quartile =3 and urb in ('1','2') then cell =1241;
else if quartile = 3 and urb = '3' then cell =1242;
else if quartile = 4 and urb in ('1','2') then cell =1271;
else if quartile = 4 and urb = '3' then cell =1272;
end;
/*3 more blocks of code for metro =2,3,4* /
/*then type changes and metro=1 and so on*/
else if elig='1' and type type =('7','8') and metro eq=1 then
do;
/*more code until my groups end* /
end;