SAS学习日志 类型转换 日期格式 length语句 数据缺失

1.Q41 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;
本题知识点:
CAT系列函数
cat(of x1-x4):即x1||x2||x3||x4 catt(of x1-x4):即trim(x1)||trim(x2)||trim(x3)||trim(x4)
cats(of x1-x4):即trim(left(x1))||trim(left(x2))||trim(left(x3))||trim(left(x4))
catx(sp,of x1-x4):即trim(left(x1))||sp||trim(left(x2))||sp||trim(left(x3))||sp||trim(left(x4))

B两个单词中间两个空格
为什么没有选C。从理解上,应该正确。但是,实际上是不对的。这正是SAS编程与其他语言的差异之一。 查看变量长度,可以发现,没有LENGTH语句申明长度时,默认每个变量(无论数值型或字符型)读取的长度都是8,不足的以空格填充。 proc contents data=WORK.PHONES varnum; run; 选项C的结果为FullName=’James Stevens‘,中间是空格是3个空格,即 James_ _ _ Stevens_
2.retain语句
53. The following SAS program is submitted:

data WORK.TOTAL_SALARY;
     retain Total; 
     set WORK.SALARY;
     by Department;
     if First.Department
        then Total=0;
     Total=sum(Total, Wagerate);
     if Last.Total;
  run;

What is the initial value of the variable Total?
A. 0
B. Missing
C. The value of the first observations Wagerate
D. Cannot be determined from the information given
问的是编译时初始值 retain Total; 意味着初始值缺失;
只有类似于retain Total=2; 编译时才有相应的值。
**retain Total=x;不成立 因为是编译时赋值,所以不能赋予其变量。
retain语句是非执行语句。
retain; /针对所有变量/
retain x y; retain x1-x5;
retain x1-x5 1 0 a b ‘abc’;
retain x1-x5(1); /x1=1,其余为缺失值/
retain x1-x4(1 2 3 4);
retain x1-x4(1:4);
array arr(2) x y;retain arr;
3
Q 55 The following SAS program is submitted:

data WORK.DATE_INFO; 
     X="01Jan1960" D ; 
  run; 

Variable X contains what value?
A. the numeric value 0
B. the character value “01Jan1960”
C. the date value 01011960
D. the code contains a syntax error and does not execute.
答案:D 本题知识点:日期时间的表示格式 起点:1960年1月1日0时0分0秒。 若将日期时间标示为数值型常数,需使用相应格式。 格式值带单引号,后紧跟跟一个D(日期)、T(时间)、DT(日期时间)。 在表示为数值常数时,不支持MMDDYYw.格式,支持datew.格式。
本题,答案D,是因为X=“01Jan1960” D ;中D之前有个空格。 若改为 X="01Jan1960"D ; ,答案就是A。 若改为 X="01011960"D ; ,答案就是C。
4. WEEKDAY(date)返回SAS日期值为一周的第几天
5.The following program is submitted:

proc format; 
     value salfmt.  
        0 -< 50000   = 'Less than 50K'
        50000 - high = '50K or Greater'; 
  options fmterr nodate pageno=1;
  title 'Employee Report'; 
  proc print data=work.employees noobs;
     var fullname salary hiredate; 
     format
        salary salfmt. 
        hiredate date9.;
     label 
        fullname='Name of Employee'
        salary='Annual Salary' 
        hiredate='Date of Hire';
  run;

Why does the program fail?
A. The PAGENO option is invalid in the OPTIONS statement.
B. The RUN statement is missing after the FORMAT procedure.
C. The format name contains a period in the VALUE statement.
D. The LABEL option is missing from the PROC PRINT statement.
第二行salfmt后面没有句号
6.:LENGTH语句
LEGTH规定的是变量的字节长度,不是格式,不能含小数点。
LENGTH variable-specification(s) <DEFAULT=n>
其中,DEFAULT=n是规定新建的数值变量的默认长度8改为n。 
数值变量 对于数值变量,LENGTH范围为3-8字节。LENGTH可放在任意位置。
在PROC SQL中ALERT不能改变数值变量长度。 
字符变量 对于字符变量,LENGTH范围为1-32767字节,空格占一个字符。
LENGTH必须放在SET语句之前。
实际上,较少使用LENGTH语句,而是通过PROC SQL中ALERT改变字符变量长度。
例如:

data work.accounting;
length jobcode $ 12;
set work.department;
run;

7.label

data WORK.ACCOUNTING;
     set WORK.DEPARTMENT;
     label Jobcode='Job Description';
  run;

Which statement is true about the output dataset?
A. The label of the variable Jobcode is Job (only the first word).
B. The label of the variable Jobcode is Job Desc (only the first 8 characters).
C. The label of the variable Jobcode is Job Description.
D. The program fails to execute due to errors. Labels must be defined in a PROC step.
可以看做 label 是没有限制的。
8.Q66. The following SAS program is submitted:
data WORK.SALES;
do Year=1 to 5;
do Month=1 to 12;
X + 1;
end;
end;
run;
How many observations are written to the WORK.SALES data set?
A. 0
B. 1
C. 5
D. 60
因为没有set merge所以是新建一个数据集,那么x经过循环后就输出一条数据 x 60。
9.缺失
默认缺失是无穷小
判断变量a是否缺失
missing(a);
若a缺失则返回1
如果知道a的数据类型 数值型 if a=.; 字符型 if a=’’; if a="";
英文单引号表示字符 英文双引号既可以是字符也可以是函数
逻辑计算时按ASCII进行比较, 缺失值主要是空格(ASCII值为32) 和英文句号(ASCII值为46)。
0-9为48-57 A-Z为65-90 a-z为97-122
10.字符型和数值型相互转换
基本只发生在 字符型完全是数值的情况下
字符型如果是 '0023’就默认转换为23

The following SAS program is submitted:
  data WORK.ACCOUNTING;
     set WORK.DEPARTMENT;
     length EmpId $6;
     CharEmpid=EmpId;
  run;

此处相当于给一个字符变量赋数值
编译的时候 会记录变量名 变量的类型 长度
如果set merge前没有任何规定 那么按照set 后跟的数据集的变量规定
如果是 set后数据集也没有的新变量 pdv读入的第一条
input 后面数值 字符是默认都是八个字节,没有input 只有数值型是默认的8个字节长度,而未规定长度的的字符型以pdv读到的第一条数据长度为长度,也就是说如果后续pdv有大于该长度的变量将被截断!!
11.where中有很多语句在if里不能用
比如like
12 导出
导出到excel excel相当于逻辑库(数据库) 所以如果两个不同的数据集导入同一个excel表的sheet里 就会覆盖 尽量不要这样
如果导出到网页 就不会覆盖 是继续写新的表

上一篇:华为网赛存储基础原理自测答案


下一篇:华为云HCNA--硬件