SlideShare une entreprise Scribd logo
1  sur  28
1
Efficient SAS Coding
with Proc SQL
When Proc SQL is Easier than Traditional
SAS Approaches
Mike Atkinson, May 4, 2005
2
Note on Efficiency
There are at least two kinds of efficiency: SAS
program efficiency and SAS programmer efficiency.
This talk addresses the latter.
I think Proc SQL can reduce coding effort, once you
become familiar with SQL.
3
Uses of Proc SQL
Proc SQL is the usual method to select data from an external
DBMS, such as Oracle, but don’t overlook that…
Proc SQL is very useful with SAS datasets:
• Summarising (alternative to Proc Summary)
• Select totals into a macro variables
• Joining (alternative to Merge in Data Step)
• Sorting
• Creating indexes for efficiency
4
Selecting from an External DBMS
• Proc SQL is the usual method to select data from
an external DBMS, such as Oracle
• Can use pass-through SQL queries to allow
DBMS to optimise the query
However, this particular talk focuses on how Proc
SQL can be used with SAS datasets.
5
Summarising Data
• Proc SQL can be used as an alternative to Proc
Summary
Advantages of Proc SQL:
• Can use functions (like put), removing need for an
additional data step
• Can join two or more datasets in the same step
• Don’t need to sort input prior to Proc SQL
• Can sort results in the same step
6
Traditional SAS Code
proc sort data=sasdata.pracds
out=my_pracds;
by praclha;
run;
proc summary data=my_pracds;
by praclha;
output out=prac_lha_counts
(drop=_type_ rename=(_freq_=prac_cnt));
run;
7
Proc SQL Code
proc sql;
create table prac_lha_counts as
select praclha,
count(*) as prac_cnt
from sasdata.pracds
group by praclha
order by praclha;
quit;
8
Results (portion)
Obs PRACLHA prac_cnt
1 1 85
2 2 144
3 3 34
4 4 48
5 5 60
6 6 33
7 7 190
8 9 50
9 10 25
10 11 171
11 12 30
9
Traditional SAS Code
proc sort data=sasdata.fitmserv
out=my_fitmserv;
by servcd;
run;
proc summary data=my_fitmserv;
by servcd;
output out=servcd_fitm_cnts_0
(drop=_type_ rename=(_freq_=fitm_cnt));
run;
data servcd_fitm_cnts;
set servcd_fitm_cnts_0;
servcd_descrip = put(servcd, svcd2ds.);
run;
10
Proc SQL Code
proc sql;
create table servcd_fitm_cnts as
select servcd,
put(servcd, svcd2ds.) as servcd_descrip,
count(*) as fitm_cnt
from sasdata.fitmserv
group by servcd, servcd_descrip
order by servcd;
quit;
11
Results (portion)
Obs SERVCD servcd_descrip fitm_cnt
1 1 1 - REGIONAL EXAMINATIONS (0100,0107) 79
2 2 2 - CONSULTATION (0110) 21
3 3 3 - COMPLETE EXAMINATIONS (0101) 20
4 4 4 - COUNSELLING (0120) 22
5 5 5 - HOME VISITS 13
6 6 6 - EMERGENCY VISITS 26
7 7 7 - INSTITUTIONAL VISITS 22
8 8 8 - MISCELLANEOUS AND OTHER VISITS (GP) 202
9 9 9 - VISIT PREMIUMS (NOT INCLUDED IN SERVICE COUNT 20
10 11 11 - PROLONGED OR EXTENDED VISIT 6
12
Select Values into Macro
Variable
proc sql noprint;
select count(distinct pracnum) into :prac_cnt
from sasdata.pracds;
quit;
%put prac_cnt = &prac_cnt;
Results (In Log):
prac_cnt = 24793
13
Select More Values into Macro
Variables
proc sql noprint;
select sum(popn), count(*)
into :pop_lha61, :rec_cnt_lha61
from sasdata.people
where lfsclyr = 20042005
and clntlha = 61;
quit;
%put pop_lha61=&pop_lha61;
%put rec_cnt_lha61=&rec_cnt_lha61;
14
Results (in log)
pop_lha61= 208372
rec_cnt_lha61= 40
15
Aside: Formatting Macro
Variables for Footnotes
%let pop_fmtd = %sysfunc(putn(&pop_lha61, comma9.));
footnote1 j=l “Note: LHA 61 had population of
&pop_fmtd in 2004/2005”;
Resulting footnote:
Note: LHA 61 had population of 208,372 in 2004/2005
16
Joining SAS datasets
• Proc SQL can be an alternative to using a data
step merge
Advantages of Proc SQL:
• Datasets do not need to be sorted first
• Can use functions (like substr) in the join,
removing need for an additional data step
• Can perform many to many joins
• Can sort results in the same step
17
Traditional SAS Code
proc sort data=my_pracs;
by pracnum;
run;
proc sort data=sasdata.pracds out=my_pracds;
by pracnum;
run;
data prac_info;
merge my_pracs (in=a keep=pracnum)
my_pracds;
by pracnum;
if (a);
run;
18
Proc SQL Code
proc sql;
create table prac_info as
select a.pracnum, b.*
from my_pracs a
left join sasdata.pracds b
on a.pracnum = b.pracnum
order by a.pracnum;
quit;
19
Alternative Proc SQL Code
(not quite the same)
proc sql;
create table prac_info as
select a.*
from sasdata.pracds a,
my_pracs b
where a.pracnum = b.pracnum
order by a.pracnum;
quit;
20
Proc SQL Join
With Index for Efficiency
proc sql;
create unique index phnnum on my_phns;
quit;
proc sql;
create table hosp_ar_days as
select "2004/2005" as fiscal_year,
p.phnnum,
sum(h.ar_days) as ar_days
from sasdata.hosp04 h,
my_phns p
where h.phn = p.phnnum
group by fiscal_year, p.phnnum
order by p.phnnum;
quit;
21
Results (portion)
fiscal_
Obs year phnnum ar_days
1 2004/2005 9013461036 15
2 2004/2005 9013510873 6
3 2004/2005 9013751586 4
4 2004/2005 9013931499 2
5 2004/2005 9013948747 22
6 2004/2005 9014182899 5
7 2004/2005 9015106078 7
8 2004/2005 9015259421 3
9 2004/2005 9015371037 4
10 2004/2005 9015670239 1
11 2004/2005 9015834749 2
12 2004/2005 9016265127 33
13 2004/2005 9016750346 1
14 2004/2005 9017397422 10
15 2004/2005 9017503546 2
22
NOTE: MERGE statement has more than one
data set with repeats of BY values.
proc sort data=sasdata.fitmserv
out=my_fitmserv;
by fitm;
run;
proc sort data=sasdata.fitmspec
out=my_fitmspec;
by fitm;
run;
data spec_serv;
merge my_fitmserv (in=a)
my_fitmspec (in=b);
by fitm;
run;
23
Many to Many Joins
Are Possible with Proc SQL
proc sql;
create table spec_serv as
select a.servcd,
b.speccode,
count(*) as fitm_count
from sasdata.fitmserv a,
sasdata.fitmspec b
where a.fitm = b.fitm
group by a.servcd, b.speccode;
quit;
24
Results (portion)
fitm_
Obs SERVCD SPECCODE count
1 1 30 6
2 1 31 3
3 1 32 17
4 1 34 4
5 1 38 7
6 1 39 4
7 1 42 21
8 1 43 10
9 2 00 1
10 2 32 1
11 2 34 4
12 2 38 5
13 2 42 5
14 2 43 1
25
To Find Out, Are Any Fee Items Related to
More than One Service Code?
proc sql;
create table fitm_mults as
select fitm,
count(*) as rec_cnt
from sasdata.fitmserv
group by fitm
having count(*) > 1;
quit;
26
Results (portion)
Obs FITM rec_cnt
1 41 2
2 116 2
3 122 2
4 127 2
5 141 2
6 222 2
7 223 2
8 224 2
9 333 2
10 381 2
11 613 2
12 614 2
13 615 2
27
Building a Format
proc sql noprint;
create table mypracnm as
select a.pracnum as start,
a.pracnum as end,
case when b.pracnum is missing then
put(a.pracnum, z5.) || ' - **No Name Found**'
else
put(a.pracnum, z5.) || ' - ' || b.pracnm
end as label,
'mypracnm' as fmtname,
'N' as type
from my_pracs a
left join sasdata.pracds b
on a.pracnum = b.pracnum;
quit;
proc format cntlin=mypracnm;
run;
28
Sample Usage of Format
data two_small_pracs;
pracnum = 2600; output;
pracnum = 2624; output;
run;
proc print data=two_small_pracs;
format pracnum mypracnm.;
run;
Obs pracnum
1 02600 - **No Name Found**
2 02624 - SMITH JOHN M

Contenu connexe

Similaire à Save Coding Time with Proc SQL.ppt

Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...Databricks
 
Habits of Effective SAS Programmers
Habits of Effective SAS ProgrammersHabits of Effective SAS Programmers
Habits of Effective SAS ProgrammersSunil Gupta
 
SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6Mahesh Vallampati
 
JDBC Connecticity.ppt
JDBC Connecticity.pptJDBC Connecticity.ppt
JDBC Connecticity.pptSwapnil Kale
 
No more struggles with Apache Spark workloads in production
No more struggles with Apache Spark workloads in productionNo more struggles with Apache Spark workloads in production
No more struggles with Apache Spark workloads in productionChetan Khatri
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ICarlos Oliveira
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the ServerdevObjective
 
Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statementsxKinAnx
 
Scaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQLScaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQLJim Mlodgenski
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 
SAS Access / SAS Connect
SAS Access / SAS ConnectSAS Access / SAS Connect
SAS Access / SAS Connectguest2160992
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksMYXPLAIN
 
Top 10 tips for Oracle performance
Top 10 tips for Oracle performanceTop 10 tips for Oracle performance
Top 10 tips for Oracle performanceGuy Harrison
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain PlanMaria Colgan
 
Quick Guide to Refresh Spark skills
Quick Guide to Refresh Spark skillsQuick Guide to Refresh Spark skills
Quick Guide to Refresh Spark skillsRavindra kumar
 
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0mCloud
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OraclemCloud
 

Similaire à Save Coding Time with Proc SQL.ppt (20)

Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
 
Habits of Effective SAS Programmers
Habits of Effective SAS ProgrammersHabits of Effective SAS Programmers
Habits of Effective SAS Programmers
 
SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6
 
JDBC Connecticity.ppt
JDBC Connecticity.pptJDBC Connecticity.ppt
JDBC Connecticity.ppt
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
 
No more struggles with Apache Spark workloads in production
No more struggles with Apache Spark workloads in productionNo more struggles with Apache Spark workloads in production
No more struggles with Apache Spark workloads in production
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
 
Sql killedserver
Sql killedserverSql killedserver
Sql killedserver
 
Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statements
 
Pl sql using_xml
Pl sql using_xmlPl sql using_xml
Pl sql using_xml
 
Scaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQLScaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQL
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
 
SAS Access / SAS Connect
SAS Access / SAS ConnectSAS Access / SAS Connect
SAS Access / SAS Connect
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
 
Top 10 tips for Oracle performance
Top 10 tips for Oracle performanceTop 10 tips for Oracle performance
Top 10 tips for Oracle performance
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
 
Quick Guide to Refresh Spark skills
Quick Guide to Refresh Spark skillsQuick Guide to Refresh Spark skills
Quick Guide to Refresh Spark skills
 
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman Oracle
 

Plus de ssuser660bb1

define_xml_tutorial .ppt
define_xml_tutorial .pptdefine_xml_tutorial .ppt
define_xml_tutorial .pptssuser660bb1
 
Session4_TrackA_Workshop_Tinazzi_Faini.pptx
Session4_TrackA_Workshop_Tinazzi_Faini.pptxSession4_TrackA_Workshop_Tinazzi_Faini.pptx
Session4_TrackA_Workshop_Tinazzi_Faini.pptxssuser660bb1
 
Pharmacology_Basics.ppt
Pharmacology_Basics.pptPharmacology_Basics.ppt
Pharmacology_Basics.pptssuser660bb1
 
ISO dates in SAS.pdf
ISO dates in SAS.pdfISO dates in SAS.pdf
ISO dates in SAS.pdfssuser660bb1
 
SAS Programming.ppt
SAS Programming.pptSAS Programming.ppt
SAS Programming.pptssuser660bb1
 
citc-rwe-8dec2021_v2.pdf
citc-rwe-8dec2021_v2.pdfcitc-rwe-8dec2021_v2.pdf
citc-rwe-8dec2021_v2.pdfssuser660bb1
 
David-Graham-HGML-presentation-20190424.pptx
David-Graham-HGML-presentation-20190424.pptxDavid-Graham-HGML-presentation-20190424.pptx
David-Graham-HGML-presentation-20190424.pptxssuser660bb1
 

Plus de ssuser660bb1 (10)

protocols.pptx
protocols.pptxprotocols.pptx
protocols.pptx
 
regulatory.pptx
regulatory.pptxregulatory.pptx
regulatory.pptx
 
define_xml_tutorial .ppt
define_xml_tutorial .pptdefine_xml_tutorial .ppt
define_xml_tutorial .ppt
 
Session4_TrackA_Workshop_Tinazzi_Faini.pptx
Session4_TrackA_Workshop_Tinazzi_Faini.pptxSession4_TrackA_Workshop_Tinazzi_Faini.pptx
Session4_TrackA_Workshop_Tinazzi_Faini.pptx
 
Pharmacology_Basics.ppt
Pharmacology_Basics.pptPharmacology_Basics.ppt
Pharmacology_Basics.ppt
 
ADaM
ADaMADaM
ADaM
 
ISO dates in SAS.pdf
ISO dates in SAS.pdfISO dates in SAS.pdf
ISO dates in SAS.pdf
 
SAS Programming.ppt
SAS Programming.pptSAS Programming.ppt
SAS Programming.ppt
 
citc-rwe-8dec2021_v2.pdf
citc-rwe-8dec2021_v2.pdfcitc-rwe-8dec2021_v2.pdf
citc-rwe-8dec2021_v2.pdf
 
David-Graham-HGML-presentation-20190424.pptx
David-Graham-HGML-presentation-20190424.pptxDavid-Graham-HGML-presentation-20190424.pptx
David-Graham-HGML-presentation-20190424.pptx
 

Dernier

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 

Dernier (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 

Save Coding Time with Proc SQL.ppt

  • 1. 1 Efficient SAS Coding with Proc SQL When Proc SQL is Easier than Traditional SAS Approaches Mike Atkinson, May 4, 2005
  • 2. 2 Note on Efficiency There are at least two kinds of efficiency: SAS program efficiency and SAS programmer efficiency. This talk addresses the latter. I think Proc SQL can reduce coding effort, once you become familiar with SQL.
  • 3. 3 Uses of Proc SQL Proc SQL is the usual method to select data from an external DBMS, such as Oracle, but don’t overlook that… Proc SQL is very useful with SAS datasets: • Summarising (alternative to Proc Summary) • Select totals into a macro variables • Joining (alternative to Merge in Data Step) • Sorting • Creating indexes for efficiency
  • 4. 4 Selecting from an External DBMS • Proc SQL is the usual method to select data from an external DBMS, such as Oracle • Can use pass-through SQL queries to allow DBMS to optimise the query However, this particular talk focuses on how Proc SQL can be used with SAS datasets.
  • 5. 5 Summarising Data • Proc SQL can be used as an alternative to Proc Summary Advantages of Proc SQL: • Can use functions (like put), removing need for an additional data step • Can join two or more datasets in the same step • Don’t need to sort input prior to Proc SQL • Can sort results in the same step
  • 6. 6 Traditional SAS Code proc sort data=sasdata.pracds out=my_pracds; by praclha; run; proc summary data=my_pracds; by praclha; output out=prac_lha_counts (drop=_type_ rename=(_freq_=prac_cnt)); run;
  • 7. 7 Proc SQL Code proc sql; create table prac_lha_counts as select praclha, count(*) as prac_cnt from sasdata.pracds group by praclha order by praclha; quit;
  • 8. 8 Results (portion) Obs PRACLHA prac_cnt 1 1 85 2 2 144 3 3 34 4 4 48 5 5 60 6 6 33 7 7 190 8 9 50 9 10 25 10 11 171 11 12 30
  • 9. 9 Traditional SAS Code proc sort data=sasdata.fitmserv out=my_fitmserv; by servcd; run; proc summary data=my_fitmserv; by servcd; output out=servcd_fitm_cnts_0 (drop=_type_ rename=(_freq_=fitm_cnt)); run; data servcd_fitm_cnts; set servcd_fitm_cnts_0; servcd_descrip = put(servcd, svcd2ds.); run;
  • 10. 10 Proc SQL Code proc sql; create table servcd_fitm_cnts as select servcd, put(servcd, svcd2ds.) as servcd_descrip, count(*) as fitm_cnt from sasdata.fitmserv group by servcd, servcd_descrip order by servcd; quit;
  • 11. 11 Results (portion) Obs SERVCD servcd_descrip fitm_cnt 1 1 1 - REGIONAL EXAMINATIONS (0100,0107) 79 2 2 2 - CONSULTATION (0110) 21 3 3 3 - COMPLETE EXAMINATIONS (0101) 20 4 4 4 - COUNSELLING (0120) 22 5 5 5 - HOME VISITS 13 6 6 6 - EMERGENCY VISITS 26 7 7 7 - INSTITUTIONAL VISITS 22 8 8 8 - MISCELLANEOUS AND OTHER VISITS (GP) 202 9 9 9 - VISIT PREMIUMS (NOT INCLUDED IN SERVICE COUNT 20 10 11 11 - PROLONGED OR EXTENDED VISIT 6
  • 12. 12 Select Values into Macro Variable proc sql noprint; select count(distinct pracnum) into :prac_cnt from sasdata.pracds; quit; %put prac_cnt = &prac_cnt; Results (In Log): prac_cnt = 24793
  • 13. 13 Select More Values into Macro Variables proc sql noprint; select sum(popn), count(*) into :pop_lha61, :rec_cnt_lha61 from sasdata.people where lfsclyr = 20042005 and clntlha = 61; quit; %put pop_lha61=&pop_lha61; %put rec_cnt_lha61=&rec_cnt_lha61;
  • 14. 14 Results (in log) pop_lha61= 208372 rec_cnt_lha61= 40
  • 15. 15 Aside: Formatting Macro Variables for Footnotes %let pop_fmtd = %sysfunc(putn(&pop_lha61, comma9.)); footnote1 j=l “Note: LHA 61 had population of &pop_fmtd in 2004/2005”; Resulting footnote: Note: LHA 61 had population of 208,372 in 2004/2005
  • 16. 16 Joining SAS datasets • Proc SQL can be an alternative to using a data step merge Advantages of Proc SQL: • Datasets do not need to be sorted first • Can use functions (like substr) in the join, removing need for an additional data step • Can perform many to many joins • Can sort results in the same step
  • 17. 17 Traditional SAS Code proc sort data=my_pracs; by pracnum; run; proc sort data=sasdata.pracds out=my_pracds; by pracnum; run; data prac_info; merge my_pracs (in=a keep=pracnum) my_pracds; by pracnum; if (a); run;
  • 18. 18 Proc SQL Code proc sql; create table prac_info as select a.pracnum, b.* from my_pracs a left join sasdata.pracds b on a.pracnum = b.pracnum order by a.pracnum; quit;
  • 19. 19 Alternative Proc SQL Code (not quite the same) proc sql; create table prac_info as select a.* from sasdata.pracds a, my_pracs b where a.pracnum = b.pracnum order by a.pracnum; quit;
  • 20. 20 Proc SQL Join With Index for Efficiency proc sql; create unique index phnnum on my_phns; quit; proc sql; create table hosp_ar_days as select "2004/2005" as fiscal_year, p.phnnum, sum(h.ar_days) as ar_days from sasdata.hosp04 h, my_phns p where h.phn = p.phnnum group by fiscal_year, p.phnnum order by p.phnnum; quit;
  • 21. 21 Results (portion) fiscal_ Obs year phnnum ar_days 1 2004/2005 9013461036 15 2 2004/2005 9013510873 6 3 2004/2005 9013751586 4 4 2004/2005 9013931499 2 5 2004/2005 9013948747 22 6 2004/2005 9014182899 5 7 2004/2005 9015106078 7 8 2004/2005 9015259421 3 9 2004/2005 9015371037 4 10 2004/2005 9015670239 1 11 2004/2005 9015834749 2 12 2004/2005 9016265127 33 13 2004/2005 9016750346 1 14 2004/2005 9017397422 10 15 2004/2005 9017503546 2
  • 22. 22 NOTE: MERGE statement has more than one data set with repeats of BY values. proc sort data=sasdata.fitmserv out=my_fitmserv; by fitm; run; proc sort data=sasdata.fitmspec out=my_fitmspec; by fitm; run; data spec_serv; merge my_fitmserv (in=a) my_fitmspec (in=b); by fitm; run;
  • 23. 23 Many to Many Joins Are Possible with Proc SQL proc sql; create table spec_serv as select a.servcd, b.speccode, count(*) as fitm_count from sasdata.fitmserv a, sasdata.fitmspec b where a.fitm = b.fitm group by a.servcd, b.speccode; quit;
  • 24. 24 Results (portion) fitm_ Obs SERVCD SPECCODE count 1 1 30 6 2 1 31 3 3 1 32 17 4 1 34 4 5 1 38 7 6 1 39 4 7 1 42 21 8 1 43 10 9 2 00 1 10 2 32 1 11 2 34 4 12 2 38 5 13 2 42 5 14 2 43 1
  • 25. 25 To Find Out, Are Any Fee Items Related to More than One Service Code? proc sql; create table fitm_mults as select fitm, count(*) as rec_cnt from sasdata.fitmserv group by fitm having count(*) > 1; quit;
  • 26. 26 Results (portion) Obs FITM rec_cnt 1 41 2 2 116 2 3 122 2 4 127 2 5 141 2 6 222 2 7 223 2 8 224 2 9 333 2 10 381 2 11 613 2 12 614 2 13 615 2
  • 27. 27 Building a Format proc sql noprint; create table mypracnm as select a.pracnum as start, a.pracnum as end, case when b.pracnum is missing then put(a.pracnum, z5.) || ' - **No Name Found**' else put(a.pracnum, z5.) || ' - ' || b.pracnm end as label, 'mypracnm' as fmtname, 'N' as type from my_pracs a left join sasdata.pracds b on a.pracnum = b.pracnum; quit; proc format cntlin=mypracnm; run;
  • 28. 28 Sample Usage of Format data two_small_pracs; pracnum = 2600; output; pracnum = 2624; output; run; proc print data=two_small_pracs; format pracnum mypracnm.; run; Obs pracnum 1 02600 - **No Name Found** 2 02624 - SMITH JOHN M