失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > c语言1a和$20比较大小 [转载]Compare过程(字符的查询与比较)

c语言1a和$20比较大小 [转载]Compare过程(字符的查询与比较)

时间:2021-10-24 11:31:50

相关推荐

c语言1a和$20比较大小 [转载]Compare过程(字符的查询与比较)

SCAN只能用于CHAR的搜索。而INDEX, INDEXC则不一定受到这种局限。而且,INDEX

(S,S1)表示在S1中搜索S。而INDEXC(S,S1-1)则表示同样的意思,只是有不同的表述。INDEXC可以搜索一些在文字里的奇怪的符号,例如“-”。

以下的引用可以更好的说明:

In SAS, the INDEX() function will scan a string and return the

location of a substring. So for example INDEX(“Alabama”,”bam”)

would return 4, since “bam” starts at the 4th letter of the word

“Alabama.”

INDEXC(“Alabama”,”bam”) returns 3… Huh?

The INDEXC() function takes the list of characters in the string

after the comma and searches for the first instance of any of them,

thus when it encounters the lower case “a,” at the third character,

it returns that index. (If these were all uppercase, it would

return a 1).

This is useful if you need to find any instances of special

characters. For example, searching a variable field called

“phone_number” for parentheses or dashes could be done with

INDEXC(phone_number,”()-“).

函数1:lag(变量名) 函数

此函数用于DATA

STEP中,返回当前变量前N行该变量的值,默认返回上一行信息

data

a;

infile

datalines ;

length a

8.;

input

a;

datalines;

12

3

4

5

;

run;

data

_null_;

set a;

c=lag(a);

put c=

a=;

run;

输出

c=.

a=1

c=1

a=2

c=2

a=3

c=3

a=4

c=4

a=5

函数2:dif(变量名) 函数

dif(变量名)=变量名-lag(变量名)

其返回当前行变量值和上N行变量值的差异

函数3:COUNT(字符串,子串)返回字符串中子串出现的次数,可用限定符来不区分大小写匹配,默认是区分

xyz='This is

a thistle? Yes, this is a thistle.';

howmanythis=count(xyz,'this');

put

howmanythis;

结果:3

函数4:INDEX(字符串,搜索的字符串)返回搜索的字符串在字符串中首次出现的位置

a='ABC.DEF

(X=Y)';

b='X=Y';

x=index(a,b);

put x;

结果:10

函数indexw和index雷同,只是前者搜索是单个的字

函数5:find(字符串,子串,开始位置,限定符号)这个函数在我看来就是用来取代index函数的

其可以指定从哪儿开始寻找,并可以指定是否区分大小写,比index方便多了

xyz='She

sells seashells? Yes, she does.';

startposvar=22

whereisshe_22=find(xyz,'she',startposvar);

put

whereisshe_22;

结果:27

函数6:VERIFY(source,excerpt-1)返回source中不在excerpt-1中的第一个字符的位置

data

scores;

input Grade : $1. @@;

check='abcdf';

if verify(grade,check)>0

thenput @1 'INVALID ' grade=;

datalines;

a b c b c d f a a q a b d d b

;

结果:INVALID

Grade=q

函数7:RXMATCH(匹配规则,字符串)

寻找字符串中匹配上的第一个字符的位置

需要和RXPARSE

函数搭配使用,其匹配规则符合正则表达匹配规则,如下

data

test;input string $;

datalines;

abcxyzpq

xyyzxyZx

x2z..X7z

;

data _null_;

set;

length to $20;

if _n_=1 thenrx=rxparse("` x < ? > 'z' to ABC

=1'@#%'");

retain rx;

drop rx;

put string=;

match=rxmatch(rx,string);

put @3 match=;

call rxsubstr(rx,string,position);

put @3 position=;

call

rxsubstr(rx,string,position,length,score);

put @3 position= Length= Score=;

call rxchange(rx,999,string,to);

put @3 to=;

call rxchange(rx,999,string);

put @3 'New ' string=;

run;

1、字符串连接符

包括|| ¦¦ !!等

例1:

data _null_;

a=" 中 华";

b="人民";

c="共和国";

x1=a||b||c;

x2=a¦¦b¦¦c;

x3=a!!b!!c;

put x1;

put x2;

put x3;

run;

输出结果:

华人民共和国

华人民共和国

华人民共和国

可以看到这些连接符输出结果完全一致,只是单纯的连接,不会对空格进行特殊处理。

2、字符连接函数

包括以下几个:

trim() 去掉字符串尾部空格,如果字符串为空,则返回一个空格

left()把字符串开头的空格移到尾部

strip()去掉字符串开头和结尾的所有空格

cat() 与||作用类似,保留首尾全部空格

catt() 但是连接之前会去掉各字符串尾部空格,相当于连接符结合trim()使用

cats() 但是连接之前会去掉首尾全部空格,相当于连接符结合strip()使用

catx() 但是连接之前会去掉首尾全部空格,并且在字符串之间加上一个指定的字符串

例2:

data _null_;

a=" 中 华";

b="人民";

c="共和国";

x1=trim(a)||trim(b)||trim(c);

x2=left(a)¦¦left(b)¦¦left(c);

x3=strip(a)!!strip(b)!!strip(c);

x4=cat(a,b,c);

x5=catt(a,b,c);

x6=cats(a,b,c);

x7=catx('我爱',a,b,c,);

put x1=/x2=/x3=/x4=/x5=/x6=/x7=;

run;

x1=中

华人民共和国

x2=中 华

人民共和国

x3=中

华人民共和国

x4=中

华人民共和国

x5=中

华人民共和国

x6=中

华人民共和国

x7=中

华我爱人民我爱共和国

一般我们要连接不同字符变量时,用“||”符号进行连接,如果变量值前后有空白字符值,有几种方法,看看

第一种方法:trim(

proc sort data=sashelp.ZIPCode out=CDstates

重复的观测对象/

where StateName in: (“C”,”D”);/in后面的制定了查询匹配的范围/

by State StateName;

run;

data concat1;

set CDstates;

CombinedData=trim(

run;

proc print data=concat1;

var State StateName CombinedData;

title1 “Information for ‘C’ and ‘D’ States”;

run;

第二种方法是:

proc sort data=sashelp.ZIPCode out=CDstates nodupkey;

where StateName in: (“C”,”D”);

by State StateName;

run;

data concat2;

set CDstates;

CombinedData=

run;

proc print data=concat2;

var State StateName CombinedData;

title1 “Information for ‘C’ and ‘D’ States”;

run;

看看结果如下:

Information for ‘C’ and ‘D’ States

Obs STATE STATENAME CombinedData

1 6 California 6:California

2 8 Colorado 8:Colorado

3 9 Connecticut 9:Connecticut

4 10 Delaware 10:Delaware

5 11 District of Columbia 11:District of Columbia

Information for ‘C’ and ‘D’ States

Obs STATE STATENAME CombinedData

1 6 California 6:California

2 8 Colorado 8:Colorado

3 9 Connecticut 9:Connecticut

4 10 Delaware 10:Delaware

5 11 District of Columbia 11:District of Columbia

第二个方法是属于通杀型,前后空格字符全部删除

而第一个方法只是让变量往左靠齐,但是没有删除空格符,所以长度没变, 因此trim(

个人理解是:第一个

对于长度函数可以看看下面的测试程序:

data crackman;

x=’ crackman’;

y=’crackman’;

m=

mc=

if n=m then put “left函数没有改变变量的长度” n= m=;

else put “left函数改变了变量的长度” n= m=;

run;

关于

options pageno=1 nodate ls=80 ps=60;

data crackman;

original = ‘*’ || string || ‘*’;

datalines;

abcd

abcd

abcd

abcdefgh

x y z

;

proc print data=crackman;

run;

全文请参见:

/?p=1257

如何去发现一个包含有多个word的字符变量,是否包含某一个word,我们一般有find函数

以及index函数,这两个方法来处理

第一个:这里需要对变量进行大写化处理

proc

freqdata=sashelp.zipcode;

whereindex(upcase(statename),”ISLAND”)

> 0;

tables

statename;

title1 “Count for each

‘Island’ area”;

run;

第二个方法:

采用find函数(SAS9.X)支持的新函数

proc

freqdata=sashelp.zipcode;

wherefind(statename,”Island”,”i”)

> 0;

tables

statename;

title1 “Count for each

‘Island’ area”;

run;

Count for each ‘Island’ area

1

03月05日 星期六

下午05时57分39秒

FREQ

PROCEDURE

Full name of

state/territory

累积 累积

STATENAME 频数 百分比 频数

百分比

———————————————————————

Northern Mariana Islands 3

2.73 3 2.73

Rhode Island 91 82.73 94

85.45

Virgin Islands 16 14.55 110

100.00

看看find函数的介绍:通过几个例子来说明一下

EX1:最简单的用法,就是用在She sells

seashells? Yes, she

does.寻找第一个独立的she这个单词在哪个位置,返回的是27

说明she这个word出现在上面这段字符串中第27个字符处。

data

crackman;

whereisshe=find(‘She sells seashells? Yes,

she does.’,'she ‘);

put

whereisshe;

run;

EX2:注意这里的’I'参数是忽略要查询单词中字符的大小写差异

data

crackman;

variable1=’She sells

seashells? Yes, she does.’;

variable2=’she

‘;

variable3=’i';

whereisshe_i=find(variable1,variable2,variable3);

put

whereisshe_i;

run;

EX3:

data

crackman;

expression1=’She sells

seashells? ‘||’Yes, she does.’;

expression2=kscan(‘he or she’,3)||’

‘;

expression3=trim(‘t ‘);

whereisshe_t=find(expression1,expression2,expression3);

put

whereisshe_t;

run;

这里其实对t这个参数的解释,如果有t这个参数表示的是忽略要查询和被查询字符串中的前后空格符对于查询匹配的影响,例如第一个例子里面

因为没有t参数,所以结果是27,这里结果是14,也就是在seashe…中的she进行匹配的

其他更多的情参考help文档

如果觉得《c语言1a和$20比较大小 [转载]Compare过程(字符的查询与比较)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。