首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据表中的某些条件为每个记录分配不同的单元格值?

如何根据表中的某些条件为每个记录分配不同的单元格值?
EN

Stack Overflow用户
提问于 2016-03-26 19:38:15
回答 1查看 80关注 0票数 0

给定表中的某些条件,我必须为每条记录指定不同的单元格值。

在图像中,column1= elig, column2=status, column3= type, column4 =metro, column5=quartile, column6=urb, column7=cell。我刚才写的前三个条件是一个if,然后是else (它们来自另一个表)。

但是,一旦我开始使用变量“四分位数”、“urb”和“类型”,我需要比我所写的代码更好的代码。

  • 地铁将从1-4增加。
  • 四分位数与每一条其他记录一起递增。
  • 类型更改为一个组。

对于第一个块type = 1,对于第二个块type maybe be 4 or 5 (不一定是增量的)。下一个类型组可能是type 7,8,9,10

只有单元格值对每条记录都有更改。我知道我可以替代宏var来缩短名称和保存类型,但是如何使这个代码更紧凑、更高效。

在此之前,非常感谢您。

代码语言:javascript
复制
 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;
EN

回答 1

Stack Overflow用户

发布于 2016-03-29 15:41:59

从您发布的数据的外观来看,您只需重新编码原始变量并连接结果即可。

例如:

代码语言:javascript
复制
data out_data;
    set in_data;
length class_12 $2 class_3 class_4 $1 class $4.;

select (type);
    when (1) 
     do;
        select (metro);
            when (1) class_12 = '11';
            when (2) class_12 = '12';
            when (3) class_12 = '14';
            otherwise;
        end;
     end;
    when (4, 5) 
      do;
        select (metro);
            when (1) class_12 = '21';
            when (2) class_12 = '22';
            when (3) class_12 = '24';
            otherwise;
        end;
    end;
    otherwise;
end;
select (quartile);
    when (1) class_3 = '1';
    when (2) class_3 = '2';
    when (3) class_3 = '4';
    when (4) class_3 = '7';
    otherwise;
end;
select (urb);
    when (1, 2) class_4 = '1';
    when (3) class_4 = '2';
    otherwise;
end;
class = class_12||class_3||class_4;
run; 

添加附加条件,并在需要时引入进一步的变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36239986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档