Lab題
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
数据
数据请单独下载。
SQL
Lab1: Sub-Query
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)
Lab 2: Label
Proc sql;
Select DID label=“Department ID”
Name label=”Employee Name”
from certadv.lab2
;
Quit;
注:label
Proc sql;
select avg(height)
into: macro_var1
from certadv.lab3
where region = ”AMR”
;
quit;
注:where 语句中必须用双引号。
%put ¯o_var1
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
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;
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;
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
%macro print;
/**Comment method 1: **/
Proc print data=certadv.lab9;
%*Comment method 2;
*Comment method 3;
run;
%mend;
%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)
%macro loop(start,end);
%do i=&start %to &end;
data data&i;
x=&i;
run;
%end;
%mend;
%loop(1,3)
%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
Array
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;
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;
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;
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
options cmplib=work.function;
data work.lab17;
set certadv.lab17;
newheight = inchtocm(height);
run;
options cmplib=work.function;
data work.lab17;
set certadv.lab17;
newweight = lbstokg(weight);
run;
Lab 19: Function String Input
options cmplib=work.function;
data work.lab19;
set certadv.lab19;
new_name = reversename(name);
run;
Hash
变形:else delete;