Professional Documents
Culture Documents
Anne Wolfley
Senior Programmer/Analyst
February 9, 2015
Basic syntax
proc sql;
create table output-dataset as
select comma-separated variable list
from input-dataset
where
order by ;
quit;
Quick report
proc sql;
select name, age, height, weight
from sashelp.class
where sex=F
order by name;
quit;
------------------------------------------------proc sort data=sashelp.class out=class; by name; run;
proc print data=class noobs;
var name age height weight;
where sex=F;
quit;
OR
proc sql;
select distinct sex
from sashelp.class;
quit;
Joins
PROC SQL join = SAS data step merge*
Types of joins
Full join: All observations from both datasets
Inner join: Observations matched in both datasets
Left join: All observations from the left dataset +
matching observations from the right dataset
Right join: All observations from the right dataset +
matching observations from the left dataset (same
as a left join, just referencing the dataset listed on
the right side instead of the left side)
* Except for many-to-many merges
All Rights Reserved, Duke Medicine 2007
Aliases
Aliases are nicknames for datasets, used as a
shortcut
proc sql;
create table patientdata as
select a.patient, a.age, b.name
from sasdata.patientage a, sasdata.patientname b
where a.patient = b.patient
order by a.patient;
quit;
date1
date2
visit
12345
12345
12345
12345
12345
12345
12345
12345
12345
12345
12345
12345
09DEC2014
09DEC2014
09DEC2014
09DEC2014
01JAN2015
01JAN2015
01JAN2015
01JAN2015
15JAN2015
15JAN2015
15JAN2015
15JAN2015
08DEC2014
01JAN2015
09JAN2015
16JAN2015
08DEC2014
01JAN2015
09JAN2015
16JAN2015
08DEC2014
01JAN2015
09JAN2015
16JAN2015
1
2
3
4
1
2
3
4
1
2
3
4
Union
proc sql;
create table patientdata as
select patient, age, name
from ds1
union
select patient, age, name
from ds2
order by patient;
quit;
proc sql;
create table caselogic as
select name, age, sex,
case when sex=F then Female
when sex=M then Male
else
end as sex2
from sashelp.class
order by name;
quit;
Age
Alfred
Alice
Barbara
Carol
Henry
James
Jane
Janet
Jeffrey
John
Joyce
Judy
Louise
etc
14.0
13.0
13.0
14.0
14.0
12.0
12.0
15.0
13.0
12.0
11.0
14.0
12.0
School Name
Rolling
Rolling
Rolling
Rolling
Rolling
Rolling
Rolling
Rolling
Rolling
Rolling
Rolling
Rolling
Rolling
Green
Green
Green
Green
Green
Green
Green
Green
Green
Green
Green
Green
Green
Elementary
Elementary
Elementary
Elementary
Elementary
Elementary
Elementary
Elementary
Elementary
Elementary
Elementary
Elementary
Elementary
proc sql;
create table ifc as
select name, age, sex
ifc(sex=F,Female,Male,) as sex2
from sashelp.class
order by name;
quit;
Age
14
13
13
14
14
12
12
15
13
12
11
14
12
Sex
sex2
M
F
F
F
M
M
F
F
M
M
F
F
F
Male
Female
Female
Female
Male
Male
Female
Female
Male
Male
Female
Female
Female
Summary functions
Summary functions summarize the data vertically, like PROC
MEANS or PROC UNIVARIATE.
Full list of functions can be found here.
MEDIAN is not an available summary function. You must use
PROC MEANS or PROC UNIVARIATE.
mean_sql
F
13.2222
M
13.4000
------------------------------------------------PROC UNIVARIATE
Sex
F
M
mean_uni
13.2222
13.4000
This gives you the mean of the height and the weight for each
observation in the dataset.
Macro variables
proc sql noprint;
select mean(age)
into :mean_age
from sashelp.class;
quit;
Results in log:
MEAN_AGE = 13.31579
Results in log:
MEAN_AGE = 13.31579 / MIN_AGE =
11 / MAX_AGE =
16
Results in log:
MEAN_AGE = 13.31579 / MIN_AGE = 11 / MAX_AGE = 16
Results in log:
AGES = 14, 13, 13, 14, 14, 12, 12, 15, 13, 12, 11, 14, 12, 15,
16, 12, 15, 11, 15
date
15JUN2012
18SEP2013
19JUN2014
01FEB2011
03MAR2012
15FEB2013
08FEB2014
Results:
Column
Type
-----num