(1) major 字段的分隔
先使用find发现字符串中and 子串的位置,然后用substr 提取前后的子串。 ..
proc import datafile = '/folders/myfolders/sas.course/files/sas_base/Lab3.xlsx'. .и
dbms = xlsx out = lab3_1_1 replace;
run;.--
. Χ
data lab3_1_2;
set lab3_1_1;
ind = find(major,'and','i');
if ind ne 0 then do;
minor = substr(major,ind+4);
major = substr(major,1,ind-2);
end;.--
drop ind;
run;
(2) 查找major和minor的频数
proc freq data = lab3_1_2;
tables major minor;
run;
(3) 选择二年级并且有minor来自nc的学生
data lab3_1_3;
set lab3_1_2;
where upcase(state) = 'NC' and minor ne ' ' and grade = 'sophomore';
run; ..
4. 处理数据
(1) 将state大写
proc import datafile = '/folders/myfolders/sas.course/files/sas_base/lab4.xlsx'
dbms = xlsx out = lab4_1 replace;
run;
. 1point 3acres
data lab4_2;. check 1point3acres for more.
set lab4_1;
state = upcase(state);
run;
. .и
(2) Sort data 并且只保留同类型第一个-baidu 1point3acres
proc sort data = lab4_2 out = lab4_3 nodupkey;.--
by state;
run;
(3) 加入average 和 evaluate 两个变量
data lab4_4;. ----
set lab4_2;
average = mean(math_1,math_2,math_3);
if average > 80 then evaluate = 'good';
else evaluate = 'bad';
run;
. 1point 3acres
(4) 去除以math开头的数据并且删除bad的evalute
data lab4_5 (drop = math:);
set lab4_4;
if evaluate = 'bad' then delete;
run;
..
5. 把一组数据导出到三组数据 .1point3acresproc import datafile = '/folders/myfolders/sas.course/files/sas_base/lab5.xlsx'. 1point3acres
dbms = xlsx out = lab6_1 replace; ..
run;
data lab6_2 lab6_3 lab6_4;
set lab6_1;.
if eid = 'M' then output lab6_2;.1point3acres
else if eid = 'Y' then output lab6_3;
else output lab6_4;
run;
. 1point 3acres
proc contents data = lab6_4 nods;
run;
. From 1point 3acres bbs
考试的时候建议大家把else output也改成用else if then确保正确。
data lab3_2_2;
set lab3_2_1;
chara = substr(name,1,3);
numb = substr(name,4);
numb = input(numb,5.);
run;. Χ
(2) 如果有z就是zflag等于1,没有就是0,考试的时候没有查有多少个0但问了有多少个1,直接proc print之后拖到底即可。
data lab3_2_3;. Χ
set lab3_2_2;
ind = find(chara,'z','i');
if ind ne 0 then do;
zflag = 1;
count1 + 1;
end;
else do zflag = 0;
count2 + 1;
end;
run;
.google и
7. PROC transpose
proc transpose data = lab5_1 out = lab5_2 (drop = _label_ rename=(_NAME_=type));
by team player;
var entry;-baidu 1point3acres
ID type;
run;
.google и
8. MERGE data . Χ
这个题也考了,大家要知道如果想merge data一定会有by statement,所以一定要先sort data。并且之后也加入了一个新的variable。这里提一下sort data 一定要output一个新的data,考试时候原始数据无法覆盖,切记。
proc import datafile = '/folders/myfolders/sas.course/files/sas_base/Lab8_1.xlsx'
dbms = xlsx out = lab8_1 replace;
run;
2. 改错题
数据表中有一个变量叫firstname,然后需要找出指定的first name (有Amanda, Tao, Chen, ...),如果它们是连续出现在原来的数据表中的话,就要建立一个increment variable叫count,对应的count值是0, 1, 2, 3,...这个样子。如果没有连续出现,这个observation对应的count值就是0. 题目给出的code里面有:
data dataname; . 1point3acres
set rawdataname;
by firstname;
if firstname in (‘AMANDA’ ‘TAO’ ‘CHEN’) then do;
if first.firstname then count=0;
count=count+1;
else count=0;
end;
run;
错误1:把count=count+1 改成count+1。因为原先的code不retain (我的考试的时候这个直接给的count+1,但运行出来并不对,我直接改成了if firstname in (….) then count+1,我也不知道对不对。
错误2:括号内的内容大小写错误并且没有加逗号
if firstname in (‘AMANDA’ ‘TAO’ ‘CHEN’) then do;
应改为 if firstname in (‘Amanda’,‘Tao’,‘Chen’) then do;