Lab部分(六道代码题需要自己按照题目要求从头写到尾,两道代码题是在已有代码上改错,一共八道题): Q1: 机经每日一练的第六题变形,真题是先按要求对 firstname, lastname还有CodePostal进行proc sort后找出每个CodePostal对应的最高income,需要把机经中department改成CodePostal即可。查找某个特定的observation时,比如600,可以使用proc print data=results.output06(obs=600 firstobs=600)只打印出observation为600的结果,这个小技巧特别实用,因为考试的数据太多了,只显示一条需要的observation能够帮助更快更准定位答案,如果用滚轮挨个查找有点浪费时间而且容易看花眼。
libname cert 'C:\cert\input';.--
libname results 'C:\cert\output';
proc sort data=cert.input06 out=sort_input06;
by department descending income;
run;
data results.output06;.google и
set sort_input06;
by department descending income;.--
if first.department;
run;
proc print data=results.output06;
run;
Q5: 机经每日一练的第十题,记得先用catx合并firstname和lastname,考试只问了zflag
libname cert 'C:\cert\input';
libname results 'C:\cert\output';
data results.output10(drop=firstname lastname);.1point3acres
set cert.input10;
fullname=catx(' ', firstname, lastname);
z=find(fullname, 'z', 'i');
if z ne 0 then do;. 1point 3 acres
zflag=1;
nozflag=0;
end;
else if z=0 then do;
zflag=0;
nozflag=1;
end;
run;
proc print data=results.output10;. Χ
sum zflag nozflag; run;
Q6: 机经每日一练中的第十五题,给了两个datasets, One dataset contains variables Product and Qty, another contains variables Product and Price. 要求在输出的output dataset中新建一个variable名叫total, total = Qty * Price,后面问了某些observation的the grant total price和mean total price
libname cert 'C:\cert\input';
libname results 'C:\cert\output';.--
proc sort data=cert.input15a out=sort_input15a;
by product;
run;
proc sort data=cert.input15b out=sort_input15b;
by product;
run;
data results.output15;. ----
merge sort_input15a sort_input15b;
by product;.1point3acres
total=price*qty; .--run;
proc print data=results.output15(obs=3 firstobs=3);
var total;
run;
proc means data=results.output15(obs=6 firstobs=6) mean;
var total;
run;. Χ
Q7:改错题, 机经每日一练的第二题类似题,输出的是三个分别关于beds, chairs, sofas的data sets,没有format,错误的地方:开头需要用libname cert读入数据,if…then output中大小写没有一致,最后一个语句不是else,是if。
libname cert 'C:\cert\input'; ..
data beds chairs sofas;
set cert.input07;
if name='Bed' then output beds; ..
if name='Chair' then output chairs;
if name='Sofa' then output sofa;
run;
proc print data=beds;
run;.
proc print data=chairs;
run;
proc print data=sofas;
run;
. check 1point3acres for more. Q8: 改错题,机经每日一练中的第十一题,同样需要注意大小写和first.name的用法
libname results 'C:\cert\output';
proc sort data=cert.input11 out=test11std; . From 1point 3acres bbs by name;
run;
data output11;
set test11std;
by name;
if name in ('Amanda', 'Tao', 'Chen') then
do;
if first.name then count=0;
count+1;
end;
else count=0;
run; ..
proc print data=output11;
run;