t********m 发帖数: 939 | 1 if var1 in ('x','y','z') or var2 in ('x','y','z') or var3 in ('x','y','z
') or var4 in ('x','y','z') then target=1; else target=0;
就是只要var1-var4里面任何一个变量含有x或y或z,目标变量就等于1,否则为0。
上述程序很繁琐,能不能用array来优化一下啊?多谢指教! | t*******t 发帖数: 633 | 2 这个可以啵?
data test;
input var1 $ var2 $ var3 $ var4 $ var5;
cards;
x 8 2 1 4
3 4 5 21 2
0 z 88 45 2
;
run;
data test2;
set test;
array var{4} var1-var4;
do i=1 to 4;
if var{i} in ('x','y','z') then target=1;
end;
if missing(target) then target=0;
run; | c******n 发帖数: 380 | 3 if indexc(catx('',of var1-var4),'xyz')>0 then target=1;else target=0 | s******r 发帖数: 1524 | 4 You assume var1-var4 is one-letter variable. It may not be true.
【在 c******n 的大作中提到】 : if indexc(catx('',of var1-var4),'xyz')>0 then target=1;else target=0
| c******n 发帖数: 380 | 5
It is true according to LZ's codes, not words.
Even if it is not true, why would my code not work?
【在 s******r 的大作中提到】 : You assume var1-var4 is one-letter variable. It may not be true.
| s******r 发帖数: 1524 | 6 if var1 in ('x','y','z') or var2 in ('x','y','z') or var3 in ('x','y','z
') or var4 in ('x','y','z')
does not mean var1-var4 is one-letter variable. It could be
'xag','x','yz'.
So only the second one meets LZ's requirement.
【在 c******n 的大作中提到】 : : It is true according to LZ's codes, not words. : Even if it is not true, why would my code not work?
| c******n 发帖数: 380 | 7
'z
I see, you're right.
【在 s******r 的大作中提到】 : if var1 in ('x','y','z') or var2 in ('x','y','z') or var3 in ('x','y','z : ') or var4 in ('x','y','z') : does not mean var1-var4 is one-letter variable. It could be : 'xag','x','yz'. : So only the second one meets LZ's requirement.
| t********m 发帖数: 939 | 8 这个works,非常感谢!我自己写的时候,没用missing function,而是用了下面的if.
..else,所以逻辑怎么都搞不对,要么是不加output,只输出最后的var4满足条件的
records;要么就是加output,最后每个var满足的records都被输出了,其中难免有重
复的。Thanks again!
data test;
set test;
array var(4) var1-var4;
do i=1 to 4;
if drugid(i) in ('x','y','z') then steroid=1;
else steroid=0;
*output;
end;
run;
【在 t*******t 的大作中提到】 : 这个可以啵? : data test; : input var1 $ var2 $ var3 $ var4 $ var5; : cards; : x 8 2 1 4 : 3 4 5 21 2 : 0 z 88 45 2 : ; : run; : data test2;
| t********m 发帖数: 939 | 9 你说的很对,我的variables确实不是one-letter的。
【在 s******r 的大作中提到】 : You assume var1-var4 is one-letter variable. It may not be true.
| t********m 发帖数: 939 | 10 谢谢,但是正如“无限江山”大侠说的,我的variables的确不是one-letter的,所以
你的code不太合适。但还是要感谢你,以后碰到单字母variable就知道用这个啦!
【在 c******n 的大作中提到】 : if indexc(catx('',of var1-var4),'xyz')>0 then target=1;else target=0
| | | t********m 发帖数: 939 | 11 Thanks, everyone! 高手在mitbbs啊,呵呵~ | s******r 发帖数: 1524 | 12 Your code could work after a little change.
data test;
set test;
array drugid(4) var1-var4;
steroid=0;
do i=1 to 4;
if drugid(i) in ('x','y','z') then steroid=1;
end;
if steroid=0 then delete;
drop i;
run;
if.
【在 t********m 的大作中提到】 : 这个works,非常感谢!我自己写的时候,没用missing function,而是用了下面的if. : ..else,所以逻辑怎么都搞不对,要么是不加output,只输出最后的var4满足条件的 : records;要么就是加output,最后每个var满足的records都被输出了,其中难免有重 : 复的。Thanks again! : data test; : set test; : array var(4) var1-var4; : do i=1 to 4; : if drugid(i) in ('x','y','z') then steroid=1; : else steroid=0;
| m*****a 发帖数: 658 | 13 I think this is interesting one. Could you tell me why catx function have to
be used here?
Thanks,
【在 c******n 的大作中提到】 : if indexc(catx('',of var1-var4),'xyz')>0 then target=1;else target=0
| c******n 发帖数: 380 | 14
to
I don't think I understand your question. Are you asking why not using other
concatenate functions?
【在 m*****a 的大作中提到】 : I think this is interesting one. Could you tell me why catx function have to : be used here? : Thanks,
| m*****a 发帖数: 658 | 15 I mean why f indexc(of var1-var4,'xyz)>0 then target=1;else target=0 ,
doesnt work ? | c******n 发帖数: 380 | 16
见6楼回复
【在 m*****a 的大作中提到】 : I mean why f indexc(of var1-var4,'xyz)>0 then target=1;else target=0 , : doesnt work ?
|
|