楼主: waxinigou
31332 7

求助index函数的使用 [推广有奖]

  • 0关注
  • 0粉丝

初中生

66%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
213 点
帖子
11
精华
0
在线时间
15 小时
注册时间
2007-3-29
最后登录
2016-4-11

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

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

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

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

扫码加我 拉你入群

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

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

关键词:Index IND 是什么原因 各位大侠 else 求助 函数 Index

回帖推荐

SignPingGu 发表于4楼  查看完整内容

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 t ...

eco-china 发表于2楼  查看完整内容

Samples from SAS.comFor these examples STRING = "ABCDEFG"Code                ReturnINDEX(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 ...

本帖被以下文库推荐

沙发
eco-china 发表于 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;


已有 2 人评分经验 论坛币 收起 理由
eijuhz + 20 精彩帖子
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 23  论坛币 + 3   查看全部评分

使用道具

藤椅
waxinigou 发表于 2008-7-23 10:58:00 |只看作者 |坛友微信交流群

谢谢你的回复

可是还是不太明白

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

谢谢

使用道具

板凳
SignPingGu 发表于 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编辑过]

已有 2 人评分经验 论坛币 收起 理由
eijuhz + 20 精彩帖子
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 23  论坛币 + 3   查看全部评分

使用道具

报纸
jjpanda1111 发表于 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;

使用道具

地板
waxinigou 发表于 2008-7-24 10:09:00 |只看作者 |坛友微信交流群

弄明白了

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

使用道具

7
dy21cn 发表于 2013-8-29 17:22:38 |只看作者 |坛友微信交流群
waxinigou 发表于 2008-7-24 10:09
弄明白了用index(x,strip(y))则可以了
不行吧?
测试过

使用道具

8
tangliang0905 发表于 2013-8-29 21:49:50 |只看作者 |坛友微信交流群

data temp;
input x $ y $;
cards;
abc d
abc a
;

data temp2;
set temp;
if index(x,strip(y))>0 then c=y;
else c=0;
run;

data temp3;
set temp;
if index(x,y)>0 then c=y;
else c=0;
run;

data temp4;
set temp;
if index(x,strip(y))>0 then c=y;
else c='0';
run;


这几个例子可以借鉴一下,我觉得应该是数据本身的问题

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 00:00