SAS 常用函数学习

substr

功能:字符串的替换和截取

举例:

  截取:year=substr(sid,7,4)   ---------放在等号右边

  替换:substr(sid,7,3)="00000"

cat cats catx

 不去掉之间的空格:cat

去掉空格:cats

以分隔符连接:catx(“-”,city, county)

scan

功能:对有分隔符的字符串进行截取

举例:scan(phone,2,"-")

length

功能:返回一个非空字符的长度,不包括尾部空格

index indexc indexw

index(str,str1):寻找str中str1中首先出现的位置。index("abcABC","AB")=4

indexc(str,str1,<str2,...>):寻找str中str1,str2任何一个字符串的第一个字符出现的位置。indexc("abcABC","xyz","12","CD")=6

indexw(str,word,<分隔符>):查找字符串中要找的一个“单词或字符”,默认空格为分隔符。indexw("asdf adog dog","dog")=11

found

found(string, substring,<,modifiers,startpos>)

功能:若要搜寻的字符串被找到,返回其第一次出现的位置。如果未找到,返回0;

  modifiers:I表示不区分大小写;T表示忽略尾部空格

  startpos:从字符串的 何处开始搜寻子字符串。

left right

功能:对齐

常用的时间和日期函数

日期函数(date) 时间函数(time) 日期时间函数(datetime)
today time datetime
intnx intnx intnx
day hour hour
datdif   datepart

 

year/qtr/month/day(date)

返回年月日SAS中的年,嫉妒,月,天的数据

weekday(date)

返回周几

intnk(date_interval,v_startdate,v_enddate)

data_interval 为输入日期间隔值,如day,week,month,qtr year,hour

v_startdate:开始日期

v_enddate:结束日期

例子:inctk("year","01jun2012"d,"01dec2014"d)=2

intnx(interval,v_startdate,n<,alignment>)

计算从start到end经历过几个interval后的SAS日期。

例子:date=intnx("month","12dec1997"d,3,alignment)

data aa;
format date yymmdd10.;
date=intnx("month","12dec2019"d,3,"b");output;
date=intnx("month","12dec2019"d,3,"e");output;
date=intnx("month","12dec2019"d,3,"s");output;
date=intnx("month","12dec2019"d,3,"m");output;
run;

 

 datepart

把datetime中的提起提取出来

datedif(start,end,basis)

用来计算2日期之间的天数,basis为计算基数(basis=“30/360”,“360”,“act/act”,“actual”)

posted @ 2019-12-31 14:45  虾米WD  阅读(2526)  评论(0)    收藏  举报