二、考试题目(后面我贴出我平时准备时练习的代码,接近原题,基本包括考试的所有题型) . check 1point3acres for more.我遇到的Project:
SQL
1. 从一个表中读name,要求name必须在另一个表的name中. ----
select name from a
where name in (select name from b);
. 1point3acres
2. 从一个表中选 unique Make and Type, 从另一个表中匹配Rebate,有对应的Make 和 Type 则显示Rebate,否则为空。我自己用left join 做的。. 1point3acres
. From 1point 3acres bbs
select distinct c.Make, c.Type, r.Rebate from table1 c left join table2 r on
c.Make=r.Make and c.Type=r.Type.--
2. 所有的missing value 全部变成0
. From 1point 3acres bbs
3.写一个array LS,有LENGTH1-LENGTH3, 另外一个array NEW,NEWLENGTH1-NEWLENGTH3. 有一个data set,LS单位是in,NEW是换算后的cm,NEW(i)=2.54*LS(i). 1point3acres.com
. check 1point3acres for more.
Functions 类
1. Inch to Centimeters
. ---- 四、我参考上述机经后准备的练习代码,应对考试题型基本没啥问题,数据在附件中,自己建一个也可以。
另外,强烈建议考前做以下那个practice,很有帮助
/*********************************************************************************. Χ
Prepare data
X1 and X2 are used as inputs.
/*********************************************************************************/
libname cert '/folders/myfolders/Certification_SAS for beginners_SAS codes/Cert_base/input/';
libname new xlsx '/folders/myfolders/Certification_SAS for beginners_SAS codes/Cert_base/input/input01.xlsx';
libname out '/folders/myfolders/Certification_SAS for beginners_SAS codes/Cert_base/output/';
.--
data x1;
set new.sheetc;
run;
data x2;
set new.sheeta;
run;
. 1point 3acres
proc sql;. check 1point3acres for more.
select * from dictionary.indexes
where libname=’SASUSER’ and memname=’SALE2000’;
quit;
.
/*********************************************************************************/. ----
/*Function*/
/*********************************************************************************/
/*---------------------------------------------
Function Exmple 1 .--
proc fcmp IN to CM, 建好function后options cmplib=work.functions,然后建个新data里面
有个新列newheight=IN to CM(height)cm=2.45IN
----------------------------------------------*/
proc fcmp outlib=work.functions.dev;.
function in2cm(in);
return(in*2.54);
endsub;
options cmplib=work.functions;.1point3acres
Data ACT03;
Set x1;.
NewHeight=in2cm(height);
Run;
. 1point 3 acres
/*---------------------------------------------
Function Exmple 2
Weight change: lbs to kg
----------------------------------------------*/
proc fcmp outlib=work.a.c;-baidu 1point3acres
Function l2k(l);
k=0.9*l;
return(k);
endsub;
run;
options cmplib=work.a;
data x2;
set x1;. Χ
new_weight=l2k(weight);
run;
/*---------------------------------------------
Function Exmple 3-baidu 1point3acres
String as input. 数据集需要添加name= first name + second name,没时间就没加
----------------------------------------------*/.
options cmplib=(work.functions);
proc fcmp outlib=work.functions.dev;
function ReverseName(name $) $ ;
length newname $40;
newname=catx(' ',scan(name,2,','),scan(name,1,','));
return (newname);
endsub;
data xx;
set yy;
zz=ReverseName(Name);
run;
/*********************************************************************************
Array https://support.sas.com/resource ... s/sugi30/242-30.pdf
*********************************************************************************/. Waral dи,
/*---------------------------------------------
Arrary Exmple 1 . From 1point 3acres bbs
Change all missing value to 0-baidu 1point3acres
----------------------------------------------*/
data new;. Waral dи,
set x1;
array var(*) _numeric_;
do i=1 to dim(var);
if var(i)=. then var(i)=0;
end;. From 1point 3acres bbs
drop i;
run;. .и
. check 1point3acres for more.
/*---------------------------------------------
Arrary Exmple 2. ----
把q1-q3的A,B,C,D,E换成1,2,3,4,5存在num1-num3中
----------------------------------------------*/. 1point 3acres
data new2;
set x1;
array q(3) q1-q3;
array num(3) num1-num3;
do i=1 to 3;
if q(i)='A' then num(i)=1;
else if q(i)='B' then num(i)=2;
Else If q{i}='C' then num(i)=3;.
Else If q{i}='D' then num(i)=4;. ----
Else If q(i)='E' then num(i)=5;
.--
end;
run;
/*---------------------------------------------
Arrary Exmple 3
写一个array LS,有LENGTH1-LENGTH3, 另外一个array NEW,NEWLENGTH1-NEWLENGTH3.
有一个data set,LS单位是in,NEW是换算后的cm,NEW(i)=2.54*LS(i). 1point3acres.com
----------------------------------------------*/
data ls;
set x1;
array ls(*) LENGTH1-LENGTH3;
array new(*) NEWLENGTH1-NEWLENGTH3;
do i =1 to 3;
NEW(i)=2.54*LS(i);
end;. ----
drop i;
run;
/*********************************************************************************/
/*SQL*/
/*********************************************************************************/
/*---------------------------------------------
SQL Exmple 1
----------------------------------------------*/
Proc sql;
Create table sql01 as. Χ
Select *
From x1
Where name in (select name from x2);
Quit;
proc sql;.
create table new_table as
select name, height, case
when height >=180 then 'high'
when height >=170 and height <180 then 'middle'
when height <170 then 'low'
end as height_marker from x1;
quit;
/*----------------------------------------------baidu 1point3acres
SQL Exmple 3
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;.1point3acres
if no matching Make and Type, show missing. Only three variable: Make, Type, Rebate
----------------------------------------------*/
proc sql;
select distinct x2.name, height, gender, benefit from x1 right join x2 on
x1.name=x2.name;
quit; ..
/*---------------------------------------------
SQL Exmple 4
给选出来的变量加label (select x label="foo", y)
----------------------------------------------*/
proc sql;
select name label='Student Name', n1 label='Index1' from x1;
quit;
/*---------------------------------------------
SQL Exmple 5
having avg大于150 用department进行分组,按着一个variable进行sort
----------------------------------------------*/
proc sql;.google и
create table xx as .
select department, avg(height) as hei from x1
group by department
having hei>150
order by department;
quit;.1point3acres
. From 1point 3acres bbs
.
/*---------------------------------------------
SQL Exmple 6
Use dictionary.columns to get the list of variable name of a table
----------------------------------------------*/
.--
proc sql;
select name into: collist separated by ','
from dictionary.columns
where libname='WORK' and memname='X1' and name like '%ght';
quit;
Data info19 errors19;. .и
Length country name $30;
If _n_=1 then do;
Declare hash C(dataset:'cert.country19');
C.definekey('name');
C.definedata('population');
C.definedone();. Χ
Call missing (country,name);
end; ..
set cert.country12;
rc=c.find(); /*rc=return code*/
if rc=0 then output info19;
else output errors19;
run;
. 1point 3acres
data _null_;
length fruit $10. amount 8.;
if _n_ = 1 then do ;
/*Declare Hash Object*/
declare hash myhash (dataset:'hash_sample',duplicate:'r');/*Replace the duplicate, only keep the last one*/
rc = myhash.definekey('fruit'); . Waral dи,
rc = myhash.definedata('fruit','amount');
rc = myhash.definedone();
call missing(fruit,amount); /*When no matching values, return none*/
. From 1point 3acres bbsend;
/*Check*/.--
rc=myhash.find(key:'cherry'); /*find if cherry exist*/ ..
if rc=0 then do; /*rc=0: find cherry*/. 1point3acres.com
put fruit= amount= ;. From 1point 3acres bbs
end;
/*Check and Replace*/
rc=myhash.check(key:'tomato');
if rc=0 then do;
put 'tomato is exist';
rc = myhash.replace(key:'tomato',data:'tomato',data:350);
end;
.--
/*Find and put*/
rc=myhash.find(key:'tomato');
if rc=0 then do;. 1point 3acres
put 'new tomato' fruit= amount= ;
end;
/*Add a record*/. 1point 3 acres
myhash.add(key:'peach',data:'peach',data:400);/*add new vaule*/. check 1point3acres for more.
rc=myhash.find(key:'peach');
if rc=0 then do;
put 'new peach' fruit= amount= ;
end;
. 1point3acres/*If not exist, then add; otherwise keep it*/
myhash.ref(key:'balabala',data:'balabala',data:600);
rc = myhash.find(key:'balabala');
if rc=0 then do;. 1point 3 acres
put 'balbala' fruit= amount= ; . Χend;
. 1point 3acres
/*Total number of records*/
totalitems = myhash.num_items;/*totalitems*/
put totalitems;.--
myhash.output(dataset:'out');/*Put data into 'out' dataset*/
run;
data success fail;
length name $10;
call missing(name);
if _n_=1 then do;
declare hash c(dataset:'xxx');
c.definekey(yy); ..
c.definedata(zz,name);
c.definedone();.--
end;
set dajl;
if c.find() then output success;
else output fail;
run;
set cert.planes; /* 这里是你要去用你刚刚建立的hash表查找的另一个表,跟定义hash的表不同 */
rc=c.find(); /* 你可以理解为哈希表的查找结果,如果RC=0,就说明查找成功,这个cert.planes里面有哈希表这样一个对应关系,如果rc不等于0 ,说明没有*/
if rc=0 then output work.success; /* 如果rc=0,查找成功,那么就输出成功表 */
if rc ne 0 then output work.fail; /* 如果rc不等于0,查找失败,那么就输出失败表 */
run;