Professional Documents
Culture Documents
with SQL
CMS SAS Users Group Conference
Learn more about THE POWER TO
KNOW
October 17, 2011
Objectives
Basics of SQL
Syntax
Embedded select statement
Examples
Re-code data with macros
Summarize numerical data in words
Organize analysis results
Scenario
proc sql;
create table new_table
as
select var1, var2, var3
from old_table
;quit;
5
Syntax
Pro Clai
v ms
Pro Clai Addre
1 100 v ms ss
2 20 1 100 55
3 45 State
Prov Addres 2 20 82
s Main
1 55 3 45 78
State HW1
6
2 82 Main
Syntax
proc sql;
create table new_table as
select a.prov, a.claims,
b.address
from claims_table as a,
enrollment_table as b
where a.prov=b.prov
;quit; 7
Embedded Select
Statement
8
Embedded Select
Statement
proc sql;
create table newdata as
select a.gt_50/b.total as pct
from (select count(*) as gt_50
from claims_table
where claims gt 50) as a,
(select count(*) as total
from claims_table) as b
;quit;
9
Embedded Select
Statement
13
Re-code Data With
Macros
proc contents data = site_visits
out = vars (keep = varnum name
type length )
noprint ;
run;
%put &obs;
15
Re-code Data With
Macros
proc sql noprint;
select name
into :var1 - :var&obs
from vars
;quit;
16
Re-code Data With
Macros
%macro backfill;
%do i=1 %to &obs;
, case when a.&&var&i =
then b.&&var&i
else a.&&var&i end
as &&var&i
%end;
%mend backfill;
17
Re-code Data With
Macros
proc sql;
create table site_visit_update as
select a.prov,
%backfill
from site_visit as a
left join update as b
on a.prov=b.prov
;quit;
18
SUMMARIZE
NUMERICAL DATA
WITH WORDS
Summarize Numbers With Words
proc sql;
create table results as
select location, found,
case when found=1
then We found this place.
else We did not find this place.
end as Summary
from source
;quit;
Example 1: Found a different
business
Code Description
0 Saw inside, no
furnishings
1 Saw inside, saw
furnishings
8 Could not see inside
Multiple Response
Options
Locatio Foun Open Furnishin Summary
n d gs
17 1 0 8 This provider
Forrest was not open.
Ave. We could not
see inside.
555 1 1 1 This provider
Main St. was open. We
saw furnishings
through a
window or door.
Multiple Response
Options
case
when furnishings=1 then
We saw furnishings through a
window or door.
when furnishings=0 then
We could see inside, there were
no furnishings.
when furnishings=8 then
We could not see inside.
else end
Things to Note
Organizing Analysis
Results
data all_results;
format number10. dollar dollar15.;
length number 8. description $50.;
set all_numbers all_dollars;
run;
proc sort data = all_results;
by description;
run;
Organizing Analysis
Results
create table all_numbers as
select count(*) as number,
Not Found as description,
2 as section
Contact information:
Mara Werner
312.353.9872
Mara.Werner@oig.hhs.gov