You are on page 1of 37

Data Manipulation

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

All the data is fictional


Site visits to Medicare providers
Data points:
Found
Medicare provider
Different business
Open
Hours Posted
BASICS OF SQL
Syntax

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

Pro Clai GT_5


v ms 0 Pc
1 100 1 t
Total
2 20 0.3
3 3
3 45

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

Clai Avera Clai GT_a


ms ge ms vg
100 74 100 1
20 20 0
45 45 0
90 90 1
115 115 1
10
Embedded Select
Statement
proc sql;
create table new_data as
select *,
case when claims gt
(select avg(claims) from data)
then 1 else 0 end
as GT_avg
from old_data
;quit;
11
RE-CODE DATA WITH
MACROS
Re-code Data With
Macros

Pro Address Foun Ope


v d n
1 555 Main 1 1
St.
2 123 State 0 0
St.
3 78 HW1
191 Foster 0
1 0

13
Re-code Data With
Macros
proc contents data = site_visits
out = vars (keep = varnum name
type length )
noprint ;
run;

proc sort data = vars;


by varnum;
run;
14
Re-code Data With
Macros
proc sql noprint;
select left(put(count(*), 5.))
into :obs
from vars
;quit;

%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

SQL - CASE WHEN statement


Define summary based on
other data
Ex: Found Summary
Location

555 Main 1 We found this


St. place.
123 State 0 We did not find
St. this place.
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

123 State St. when


found = 0
Found 0 and
Open 0 Diff_biz = 1
then
Hour 0 Found a
Posted different
Different 1 business at
Business
this address.
Example 2: Found and
Open
555 Main case when open=1
St. then This provider
Found 1 was open. else
Open 1 end ||
case when hours =
Hours 1 1
Posted then Hours were
Different 0 posted. else
Business end
as summary
Summarize Numbers With Words

Locatio Foun Open Hours Differe Summar


n d Poste nt y
d Busines Found a
s different
123 0 0 0 1 business
State St at this
address.
This
provider
555 1 1 1 0 was
Main St. open.
Hours
were
posted.
Multiple Response
Options
KEY for furnishings variable

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

case when _____ and ________


then ______. else end ||
case when _____ and ________
then ______. else end ||
case when _____ and ________
then ______. else end
ORGANIZING
ANALYSIS RESULTS
Organizing Analysis
Results
SQL UNION statement
Calculate summary statistics
Join together independent data tables

Not Numbe Descripti


Found r on
10 10 Not Found
Not
Open 50 Not Open
50
Organizing Analysis
Results
select count(*) as number, Not
Found as description
from results_table
where found=0
union
select count(*) as number, Not
Open as description
from results_table
Where open=0
Organizing Analysis
Results
Numbe Dollar Description Sectio
r s n
300 Site visit 1
population
10 Not Found 2
50 Not Open 2
$20,00 Average dollars 3
0 not found
$75,00 Average dollars 3
0 not open
Organizing Analysis
Results
create table all_numbers as
select count(*) as number, Not
Found as description

create table all_dollars as


select avg(allw) as dollars, Average
dollars not found as description

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

Benefits of the process

All of the statistics for your project


are together in one place
There is a description for each
statistic right there
You can use the name/description
to easily track the SAS code that
you used to create each step.
Questions?

Contact information:
Mara Werner
312.353.9872
Mara.Werner@oig.hhs.gov

You might also like