SAS函数:常用的3个拼接函数

  1. cat函数
    函数结构:cat(str1, str2, str3, ...)
    函数作用:将字符数据按罗列顺序前后拼接,不去除空格
    举例:
data dt1;
    length year month day $10;
    input year month day ;
    date = cat(year,'-' ,month,'-' ,day);
cards;
2012 12 23
2014 05 10
2014 07 15
2015 09 12
;
run;

数据集如下:


dt1数据集

注意:
可以看到,length year month day $10;规定了year month day的长度,cat没有去除空格;

  1. cats函数
    函数结构:cats(str1, str2, str3, ...)
    函数作用:将字符数据按罗列顺序前后拼接,去除空格,且如有缺失值仍会进行拼接
    举例:
data dt2;
    length year month day $10;
    infile datalines  delimiter =",";
    input year month day ;
    date = cats(year,'/' ,month,'/' ,day);
datalines;
2012, 12 ,23,
2014, 05 ,,
2014,  ,15,
2015 ,09, 12,
;
run;

数据集如下:

dt2数据集

注意:
可以看到,length year month day $10;虽然规定了year month day的长度,但cats将多余的空格去掉了;而对于缺失的数据,仍然进行了拼接;

  1. catx函数
    函数结构:catx(sep,str1, str2, str3, ...)
    函数作用:sep是规定的分隔符,catx函数将字符数据前后拼接,中间分隔符相连。去除空格,且如有缺失值则不进行拼接
    举例:
data dt3;
    length year month day $10;
    infile datalines  delimiter =",";
    input year month day ;
    date = catx("-",year,month,day);
datalines;
2012, 12 ,23,
2014, 05 ,,
2014,  ,15,
2015 ,09, 12,
;
run;

数据集如下:


dt3数据集

注意:
可以看到,length year month day $10;虽然规定了year month day的长度,但catx将多余的空格去掉了;而对于缺失的数据,则被删除掉没有进行拼接。

你可能感兴趣的:(SAS函数:常用的3个拼接函数)