2016-03-26 42 views
0

我必須爲給定表中某些條件的每條記錄分配不同的單元格值。如何根據表中的某些條件爲每個記錄分配不同的單元格值?

Table

在圖像中,column1= elig, column2=status, column3= type, column4 =metro, column5=quartile, column6=urb, column7=cell。前面3個條件我剛寫在一個if then else(他們來自另一個表)。

但是,一旦我開始使用變量`四分位數,urb和類型我需要比我寫的更好的代碼。

  • 地鐵將從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; 
+0

你可以發佈一些樣本輸入和輸出數據嗎? – Reeza

+0

我添加了第一個表的type = 1的圖像,注意類型發生了變化,它的分組就像這樣。類型= 1,類型=(4,5,6),類型=(7,8,9,10,11) – user601828

+1

我敢肯定,不會輸入數據...我會建議創建該表,然後通過合併而不是IF/THEN語句進行查找。我還打賭,公式的分配有一個數學邏輯,可以幫助確定代碼,但目前我還沒有看到它。 – Reeza

回答

0

從你發佈的數據看,你只需要重新編碼你的原始變量並連接結果。

例如

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

感謝您的建議,我會嘗試並讓您知道。 – user601828

相關問題