全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
31926 7
2008-07-23

求助各位大侠
问题:
x,y分别是两个变量,字符型
现在我想看x中是否含有y这个字符串,含的话赋值c=y否则c=0

我的程序如下:
if index(x,y)>0 then c=y;
else c=0;
可是结果总是显示c=0

请问大侠这是什么原因,谢谢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2008-7-23 10:04:00
Samples from SAS.com

For these examples STRING = "ABCDEFG"

Code                Return
INDEX(STRING,'C')   3 (the position of the 'C')
INDEX(STRING,'DEF') 4 (the position of the 'D')
INDEX(STRING,'X')   0 (no "X" in the string)
INDEX(STRING,'ACE') 0 (no "ACE" in the string)

It looks your code is OK. The following code may help to solve your problem.

%let X = "abcdefg";
%let A = "a";
%let F = "f";
%let H = "h";

data _null_;

    if index(&X,&A) > 0 then %put A is &A;
    if index(&X,&F) > 0 then %put F is &F;
    if index(&X,&H) NG 0 then %put H is not found;;

Run;


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2008-7-23 10:58:00

谢谢你的回复

可是还是不太明白

你下面的这些语句的意思是?

谢谢

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2008-7-23 21:14:00
If your code is working inside Data step, and X, Y are variable of the dataset you are working with, then the code will be fine.

But if your code is working inside Data step, and neither X or Y is the variable of the dataset you are working with, then SAS will treat X, Y as two new variables without iniitalization. The code will run as you have stated, just make C = 0 all the way.

If you run the code, you will find the result in the log file.

Good luck

[此贴子已经被作者于2008-7-23 21:18:21编辑过]

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2008-7-23 21:16:00
data temp;
x='123a456';
y='a';
format c $1.0;
if index(x,y)>0 then c=y;
else do
     num_c=input(c,1.0);
     num_c=0;
     end;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2008-7-24 10:09:00

弄明白了

用index(x,strip(y))则可以了

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群