SAS Base (41)

Given the raw data record in the file phone.txt:

----|----10---|----20---|----30---|
Stevens James SALES 304-923-3721 14

The following SAS program is submitted:

data WORK.PHONES;
infile ‘phone.txt’;
input EmpLName $ EmpFName $ Dept $ Phone $ Extension;
<_insert_code_>
run;

Which SAS statement completes the program and results in a value of “James Stevens” for the variable FullName?

A. FullName=CATX(‘ ‘,EmpFName,EmpLName);
B. FullName=CAT(‘ ‘,EmpFName,EmpLName);
C. FullName=EmpFName||EmpLName;
D. FullName=EmpFName + EmpLName;

Check Answer
Answer: A

注解:CAT function用于连接括号内的字符串,字符串头部和尾部的空格会得到保留。例如:
data _null_;
x = ‘ SAS ‘;
y = ‘ R Python.’;
z = cat(x, y);
put z $char.;
run;
‘SAS’前面和后面的空格,’R’前面的空格都会得到保留。在LOG中会输出以下结果(不包括标尺):
----|----10---|
 SAS  R Python.

CATX function在连接字符串时,会在各字符串之间插入一个设定好的分隔符,并且去掉字符串头部和尾部的空格。例如:
data _null_;
x = ‘ SAS ‘;
y = ‘ R Python.’;
z = catx(‘*’, x, y);
put z $char.;
run;
输出的结果为:
----|----10---|
SAS*R Python.

||为concatenation operator,起作用几乎相当于CAT,即CAT(a, b)和a||b输出相同的结果。唯一的区别是,接收合并后字符串的那个变量的默认长度(Length),例如以下程序:
data d;
x = ‘ SAS ‘;
y = ‘ R Python.’;
z1 = cat(x, y);
z2 = x||y;
put z1 $char.;
put z2 $char.;
run;
如果z1和z2的长度都没有预先定义,z1的长度为200,而z2的长度为x的长度加上y的长度。以下为PROC CONTENTS的输出结果:

Alphabetic List of Variables and Attributes
# Variable Type Len
1 x Char 5
2 y Char 10
3 z1 Char 200
4 z2 Char 15

D选项适度求2个字符型变量的和,字母之间无法相加。

One thought on “SAS Base (41)

Leave a Reply

Your email address will not be published. Required fields are marked *