/*
SAS workshop 2.sas
proc sql and proc datasets
Hint
- to highlight comments, change the background colour in
Tools->Options->Enhanced Editor
Hint
2 - save commonly used code snippets in Tools->Add Abbreviation (eg. program
header, proc summary)
*/
/******************************
Program title
Author
**************************/
data
en1(drop = course) en2 en3;
set
deet07.dx2007;
if
0 <= _N_ <= 1000
then output en1;
else
if 1000
< _N_ <= 2000 then
output en2;
else
if 2000
< _N_ <= 3000 then
output en3;
run;
/*
1. list contents of last dataset used, en3 */
proc
contents data=
_LAST_;
run;
/*
2. list contents of all datasets in the work library */
proc
contents data=work._ALL_;
run;
/*
3. list datasets in the work library */
proc
contents data=work._ALL_
nods;
run;
/*
4. proc datasets version of above - list
all datasets in the specified library */
proc
datasets library=work;
quit;
run;
/*
5. describe a single dataset */
proc
datasets library=work;
contents
data=en1;
quit;
run;
/*
6. describe all work datasets */
proc
datasets;
contents
data=_ALL_;
quit;
run;
/*
7. copy all datasets in work to stud07, excluding e3*/
proc
datasets;
copy
in=work out=stud07;
exclude
e3;
quit;
run;
/*
8. delete everything in work (deletes datasets and formats) */
proc
datasets library=work
kill;
quit;
run;
/*
9. delete just the datasets */
proc
datasets library=work
memtype=data kill;
quit;
run;
/*
10. move all datasets except en3 from stud07 to work */
proc
datasets;
copy move
in=stud07 out=work;
exclude
en3;
quit;
run;
/*
11. move just one dataset from stud07 to work */
proc
datasets;
copy
move in=stud07 out=work;
select
en3;
quit;
run;
/*
12. delete an individual dataset */
proc
datasets library=work;
delete
en3;
quit;
run;
/*
13. change the name of a dataset */
proc
datasets library=work;
change
en2=en4;
quit;
run;
/*
14. append datasets - note this is quicker than using a set command, as only
the appended dataset is read in */
proc
datasets library=work;
append
out=en4 data=en1;
quit;
run;
/*
15. rename a variable */
proc
datasets library=work
NOlist;
modify
en1;
rename
student=personid course = course_cd;
run;
quit;
/*
PROC SQL */
proc
sql;
select
*
from
deet07.en2007 as en
where
course like '10%';
quit;
/*
an alternative to a proc summary */
proc
sql;
create
table ensum as
select
course, sex, sum(eftsu) as load
from
deet07.en2007
group by
1,2;
quit;
/*
find units with between 1 and 5 enrolments */
proc
sql;
create
table small_enrols as
select
max(unit) as unit, count(student) as
enrols
from
deet07.ld2007
group by
unit
having count(student) < 6
order
by unit;
quit;
/*
an sql version of nodupkey */
proc
sql;
select
distinct(course)
from
deet07.en2007;
quit;
/*
joins */
/*
this is an inner join */
proc
sql;
select
*
from
deet07.en2007 en, deet07.ld2007 ld
where
en.student = ld.student
and
en.course = ld.course
and
en.course like '1012%';
quit;
/*
can more explicitly write it as .. */
proc
sql;
create
table test as
select
*
from
deet07.en2007 en left join deet07.ld2007 ld
on
(en.student = ld.student
and
en.course = ld.course)
where
en.course like '1012%';
quit;
/*
full join is where all records are included, including missing records from
left and right */
/*
left join includes all records from the left */
|