我有一个PAR文件,有10个变量: q1-q10,其值在0-1范围内。我需要为每个变量创建(q1,q2,q3.)一个新变量,它将根据以下范围( 0< q <0.2 => 1)接收值1-5。
0.2< Q< 0.4 => 2等。
最后,对于每个q应该是一个新列,它应该是一个额外的列,其值为1-5。
我已经成功地为一个变量创建了它,但是如何以更有效的方式为所有10个变量创建它呢?
data PAR1; set PAR;
if q1>0 and q1<0.2 then do; qq1=1;end;
if q1>0.2 and q1<0.4 then do; qq1 =2;end;
if q1>0.4 and q1<0.6 then do; qq1=3;end;
if q1>0.6 and q1<0.8 then do; qq1=4;end;
if q1>0.8 and q1<1 then do; qq1=5;end;
run;
proc print data=PAR1;
run;发布于 2015-04-22 20:21:03
你可以使用数组。
data par1;
set par;
array q{*} q1-q10;
array qq{10};
do i = 1 to dim(q);
if 0 < q[i] < 0.2 then qq[i] = 1;
else if 0.2 < q[i] < 0.4 then qq[i] = 2;
else if 0.4 < q[i] < 0.6 then qq[i] = 3;
else if 0.6 < q[i] < 0.8 then qq[i] = 4;
else if 0.8 < q[i] < 1 then qq[i] = 5;
end;
drop i;
run;第一个array语句创建一个由所有10个q变量组成的数组。第二个变量初始化10个新的qq变量,这些变量将通过qq10顺序命名为qq1。
这将通过数组中所有变量的索引(即位置)来遍历所有变量。所以q[2]引用了q2等等。对于每个变量,新变量是根据您提供的逻辑分配的。
顺便提一句,请注意,您的逻辑中有不等式,但没有等式--这将导致在例如q1 = 0.2时丢失值。
https://stackoverflow.com/questions/29807385
复制相似问题