0% found this document useful (0 votes)
44 views11 pages

Lab題

Uploaded by

ipmessage1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views11 pages

Lab題

Uploaded by

ipmessage1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Advanced Lab 机经汇总

Nov. 11 2020

By Mike Q

Contents
三点说明:....................................................................................................................................... 2
所有机经均源于网络。 ........................................................................................................... 2
其他 Lab .................................................................................................................................... 2
数据........................................................................................................................................... 2
SQL .................................................................................................................................................... 3
Lab1: Sub-Query........................................................................................................................ 3
Lab 2: Label ............................................................................................................................... 3
Lab 3: SQL to Macro .................................................................................................................. 3
Lab 4: Inner Join / Full Join Unique ........................................................................................ 4
Lab 5: SQL: Case … When .......................................................................................................... 5
Lab 6: SQL : Having .................................................................................................................... 5
Lab 7: SQL : In-Line View ........................................................................................................... 5
Macro ................................................................................................................................................ 6
Lab 8: check macro.................................................................................................................... 6
Lab9: Macro Comments ............................................................................................................ 6
Lab 10: Macro Do Loop – i>10................................................................................................... 6
Lab 11: Macro Do Loop – from start to end .............................................................................. 7
Lab12: Macro Do until 2 ............................................................................................................ 7
Array .................................................................................................................................................. 8
Lab13: Array Missing to 0 .......................................................................................................... 8
Lab 14: Array: Missing to 0 Another.......................................................................................... 8
Lab 15: Array A B C D E to 1, 2, 3, 4, 5 ....................................................................................... 9
Lab 16: Array Inch to CM ........................................................................................................... 9
Function .......................................................................................................................................... 10
Lab17: Function Inch to CM .................................................................................................... 10
Lab 18: Function LBS to KG...................................................................................................... 10
Lab 19: Function String Input .................................................................................................. 11
Hash ................................................................................................................................................ 11
Lab 20: Hash Project................................................................................................................ 11
三点说明:

所有机经均源于网络。

感谢前人的分享。

其他 Lab

在此机经之外,如果有时间,再把 adv prep guide 的 scenario 做了,把 practice exam 做了

数据

数据请单独下载。
SQL

Lab1: Sub-Query

在 proc SQL 里创立一个 table,table 里读取某个文件所有的变量,但只显示另外一个文件里


NAME 存在的观察。

Proc sql;
create table work.lab1 as
Select *
from certadv.lab1
where name in (select name from certadv.lab12)
;
quit;

OR 变形
Where name not in (select name from certadv.lab12)

注:红色字体部分为 sub query

Lab 2: Label

Proc sql;
Select DID label=“Department ID”
Name label=”Employee Name”
from certadv.lab2
;
Quit;

注:label

Lab 3: SQL to Macro

用 sql 把 region='AMR'的 avg(var)存成一个 macro variable

Proc sql;
select avg(height)
into: macro_var1
from certadv.lab3
where region = ”AMR”
;
quit;

注:where 语句中必须用双引号。

%put &macro_var1

/*** Two Macros***/


Proc sql;
select avg(height) avg(weight)
into: macro_var1,
: macro_var2
from certadv.lab3
where region = ”AMR”
;
quit;

Lab 4: Inner Join / Full Join Unique

use SQL to create a new table that contains all unique values of Make and Type from cert.cars, and
Rebate from cert.rebate. For Rebate, show Make and Type if matching; if no matching Make and
Type, show missing. Only three variable: Make, Type, Rebate.

/**Inner Join**/
Proc sql;
Create table work.lab4 as
select distinct c.make, c.type, r.rebate
from certadv.cars as c
left join
certadv.rebate as r
on c.make=r.make and c.type=r.type
;
quit;

/*Full Join*/
Proc sql;
Create table lab42 as
select distinct c.make, c.type, r.rebate
from certadv.cars as c
full join
certadv.rebate as r
on c.make=r.make and c.type=r.type
;
quit;
注:要用 distinct,个人觉得应该用 full join

Lab 5: SQL: Case … When

在 PROC SQL 里面创建一个新的 Variable,case expression

Proc sql;
Create table work.lab5 as
Select *, case
when height<=170 then “Low”
when 170<height<181 then “Middle”
when height>=181 then “High”
end as HLM
from certadv.lab5
;
quit;

Lab 6: SQL : Having

Proc sql;
create table work.lab6 as
select *, avg(salary) as avgsalary
from certadv.lab6
group by did
having /*calculated*/ avgsalary > 5000
order by salary desc
;
quit;

注:having 中可以不用 calculated

Lab 7: SQL : In-Line View

Proc sql;
create table work.lab7 as
select *, avg(salary) as avgsalary
from certadv.lab7
group by did
having /*calculated*/ avgsalary >= (select median(salary) from certadv.lab7)
;
quit;
注:是 lab6 的变体,用了 in-line view

Macro

Lab 8: check macro

macro 用 option 在 log 里看 找到问题对应的内容就好


两道题,问 parameter 的是 N,问 variable 的是 age

options mprint mlogic;


%check15

Lab9: Macro Comments

%macro print;
/**Comment method 1: **/
Proc print data=certadv.lab9;
%*Comment method 2;
*Comment method 3;
run;
%mend;

%print

注:三种 comments 的方式。Comments 不会被执行。

Lab 10: Macro Do Loop – i>10

建一个 maro,在 do loop 里面写一个 if 语句

%macro loop(start,end);
%do i = &start %to &end;
%if &i>10 %then %do;
data x&i;
x=&i;
run;
%end;
%end;
%mend;
%loop(1,200)

Lab 11: Macro Do Loop – from start to end

%macro loop(start,end);
%do i=&start %to &end;
data data&i;
x=&i;
run;
%end;
%mend;

%loop(1,3)

Lab12: Macro Do until 2

写出一个 macro,包含 global 的 macro variable X,赋予其 initial value:1.25,做 do loop,by


increment 0.25,%put X 的值,直到 2 为止

%macro loop;
%let x=1.25;
%do %until (&x=2);
%let x=%sysevalf(&x+0.25);
%put &x;
%end;
%put the finale value is &x;
%mend;

%loop

注:题目要求到 2 为止,因此,可以用&x=2 或者 &x>=2 但不要用 &x>2


也可以用下面的 do while
%macro loop;
%let x=1.25;
%do %while (&x<2);
%let x=%sysevalf(&x+0.25);
%put &x;
%end;
%put the finale value is &x;
%mend;
%loop

Array

Lab13: Array Missing to 0

写一个 array,把一个 dataset 的所有的 missing value 变成 0

data work.lab13;
set certadv.lab13;
array vars* _numeric_;
do i=1 to dim(vars);
if vars[i]=. then vars[i]=0;
end;
drop i;
run;

注:
_numeric_ 用来把所有的 numeric 的变量都放到 array 里面
dim(vars) 用来确定 array 有多少个变量。

OR

Data work.lab13;
Set certadv.lab13;
array vars _numeric_;
do over vars;
if vars[i]=. then vars[i]=0;
end;
drop i;
run;

Lab 14: Array: Missing to 0 Another

写一个 array,把 var12-var15 所有的 missing value 变 0

Data work.lab14;
Set certadv.lab14;
array vars[4] var12-var15;
do i = 1 to 4;
if vars[i]=. then vars[i]=0;
end;
run;

Lab 15: Array A B C D E to 1, 2, 3, 4, 5

array 把 q1-q10 的 A,B,C,D,E 换成 1,2,3,4,5 存在 num1-num10 中

Data work.lab15;
Set certadv.lab15;
array QQ[10] Q1 – Q10;
array NN[10] num1 – num10;
do i =1 to 10;
if QQ[i] = “A” then NN[i] = 1;
else if QQ[i] = “B” then NN[i] = 2;
else if QQ[i] = “C” then NN[i] = 3;
else if QQ[i] = “D” then NN[i] = 4;
else if QQ[i] = “E” then NN[i] = 5;
end;
drop i;
run;

Lab 16: Array Inch to CM

Data work.lab16;
Set certadv.lab16;
array inches[3] height1 – height3;
array cms[3] newheight1 – newheight3;
do i = 1 to 3;
cms[i] = 2.54*inches[i];
end;
drop i;
run;
Function

Lab17: Function Inch to CM

proc fcmp IN to CM, 建好 function 后 options cmplib=work.functions,然后建个新 data 里面有


个新列 newheight=IN to CM(height)cm=2.45IN

Proc fcmp outlib=work.function.dev;


function inchtocm(inch);
cm = 2.54*inch;
return(cm);
endsub;
run;

options cmplib=work.function;

data work.lab17;
set certadv.lab17;
newheight = inchtocm(height);
run;

Lab 18: Function LBS to KG

Proc fcmp outlib=work.function.dev;


function lbstokg (lbs);
kg = 0.9*lbs;
return(kg);
endsub;
run;

options cmplib=work.function;

data work.lab17;
set certadv.lab17;
newweight = lbstokg(weight);
run;
Lab 19: Function String Input

Proc fcmp outlib=work.function.dev;


function reversename(name $) $;
length newname $40;
newname = catx (“ ”, scan(name,2,””),scan(name,1,””));
return(newname);
endsub;
run;

options cmplib=work.function;

data work.lab19;
set certadv.lab19;
new_name = reversename(name);
run;

Hash

Lab 20: Hash Project

ash 根据 country19 建立 hash 表,然后 input contry20,根据 key 查表,成功的输出 work.XXX,


失败的输出到 work.errors。

Data work.success work.fail;


length country_name $30;
call missing (country_name);
If _n_=1 then do;
declare hash C (dataset: “certadv.lab20”);
C.definekey(“country_code”);
C.definedata(“country_name”);
C.definedone();
end;
set certadv.lab21;
rc=C.find();
if rc=0 then output work.success;
else output work.fail;
run;

变形:else delete;

You might also like