1 Efficient SAS Coding with Proc SQL When Proc SQL is Easier than Traditional SAS Approaches Mike Atkinson, May 4, 2005.

Slides:



Advertisements
Similar presentations
Effecting Efficiency Effortlessly Daniel Carden, Quanticate.
Advertisements

Haas MFE SAS Workshop Lecture 3:
How SAS implements structured programming constructs
Axio Research E-Compare A Tool for Data Review Bill Coar.
SAS Programming:File Merging and Manipulation. Reading External Files (review) data barf; * create the dataset BARF; infile ’s:\mysas\Table7.1'; * open.
Slide C.1 SAS MathematicalMarketing Appendix C: SAS Software Uses of SAS  CRM  datamining  data warehousing  linear programming  forecasting  econometrics.
Outline Proc Report Tricks Kelley Weston. Outline Examples 1.Text that spans columnsText that spans columns 2.Patient-level detail in the titlesPatient-level.
Introduction to SQL Session 1 Retrieving Data From a Single Table.
Hash vs Join A case study evaluating the use of the data step hash object to replace a SQL join Geoff Ness Sep 2014.
Basic And Advanced SAS Programming
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science.
Let SAS Do the Coding for You! Robert Williams Business Info Analyst Sr. WellPoint Inc.
Chapter 18: Modifying SAS Data Sets and Tracking Changes 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Data Preparation for Analytics Using SAS Gerhard Svolba, Ph.D. Reviewed by Madera Ebby, Ph.D.
SAS SQL SAS Seminar Series
SAS Macros ® 101 How I learned to stop worrying and love macros Alex Chaplin BCS USA Section.
Chapter 10:Processing Macro Variables at Execution Time 1 STAT 541 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Different Decimal Places For Different Laboratory Tests PharmaSug 2004, TT01 A. Cecilia Mauldin.
SAS SQL Part 2 Alan Elliott. Dealing with Missing Values Title "Dealing with Missing Values in SQL"; PROC SQL; select INC_KEY,GENDER, RACE, INJTYPE, case.
Niraj J. Pandya, Element Technologies Inc., NJ.  Summarize all possible combinations of class level variables even if few categories are altogether missing.
Chapter 15: Combining Data Horizontally 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Multiple Uses for a Simple SQL Procedure Rebecca Larsen University of South Florida.
PROC SQL: Tips and Translations for Data Step Users By: Gail Jorgensen Susan Marcella.
PROC SQL Phil Vecchione. SQL Structured Query Language Developed by IBM in the early 1970’s From the 70’s to the late 80’s there were different types.
SAS ® PROC SQL or Vanilla Flavor Cecilia Mauldin January
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
Introduction to Using the Data Step Hash Object with Large Data Sets Richard Allen Peak Stat.
1 SAS 1-liners SAS Coding Efficiencies. 2 Overview Less is more Always aim for robust, reusable and efficient code Coding efficiency versus processing.
SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.
1 Filling in the blanks with PROC FREQ Bill Klein Ryerson University.
Formats to the Rescue Gary McQuown Data and Analytic Solutions Inc. Fairfax, VA.
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
1 Using the Magical Keyword “INTO” in PROC SQL Thiru Satchi Blue Cross and Blue Shield of Massachusetts Boston Area SAS Users Group April 5, 1999.
Code Generation. 2 Overview of presentation Goal Background Dynamic SQL Method Examples.
Chapter 22: Using Best Practices 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
© OCS Biometric Support 1 APPEND, EXECUTE and MACRO Jim Groeneveld, OCS Biometric Support, ‘s Hertogenbosch, Netherlands. PhUSE 2010 – CC05 PhUSE 2010.
Creating and Using Custom Formats for Data Manipulation and Summarization Presented by John Schmitz, Ph.D. Schmitz Analytic Solutions, LLC Certified Advanced.
YET ANOTHER TIPS, TRICKS, TRAPS, TECHNIQUES PRESENTATION: A Random Selection of What I Learned From 15+ Years of SAS Programming John Pirnat Kaiser Permanente.
SAS Basics. Windows Program Editor Write/edit all your statement here.
Time Series Data Processes by Tai Yu April 15, 2013.
Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS.
Copyright © 2004, SAS Institute Inc. All rights reserved. SASHELP Datasets A real life example Barb Crowther SAS Consultant October 22, 2004.
1 Chapter 3: Getting Started with Tasks 3.1 Introduction to Task Dialogs 3.2 Creating a Listing Report 3.3 Creating a Frequency Report 3.4 Creating a Two-Way.
CC07 PhUSE 2011 Seven Sharp tips for Clinical Programmers David Garbutt Rohit Banga BIOP AG.
SHRUG, F EB 2013: N ETWORKING EXERCISE Many Ways to Solve a SAS Problem.
Patrick Thornton SRI International.  Example of a Multiple Response Item ◦ Variable coding and example data  A Cross Tabulation using Proc REPORT 
1 Data Manipulation (with SQL) HRP223 – 2009 October 12, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
Online Programming| Online Training| Real Time Projects | Certifications |Online Classes| Corporate Training |Jobs| CONTACT US: STANSYS SOFTWARE SOLUTIONS.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 26 By Tasha Chapman, Oregon Health Authority.
1 Ready To Become Really Productive Using PROC SQL? Sunil Gupta Gupta Programming.
Working Efficiently with Large SAS® Datasets Vishal Jain Senior Programmer.
Longitudinal Data Techniques: Looking Across Observations Ronald Cody, Ed.D., Robert Wood Johnson Medical School.
Better Metadata Through SAS® II: %SYSFUNC, PROC DATASETS, and Dictionary Tables.
Hints and Tips SAUSAG Q SORTING – NOUNIQUEKEY The NOUNIQUEKEY option on PROC SORT is a useful way in 9.3 to easily retain only those records with.
SAS and Other Packages SAS can interact with other packages in a variety of different ways. We will briefly discuss SPSSX (PASW) SUDAAN IML SQL will be.
Session 1 Retrieving Data From a Single Table
Putting tables together
Chapter 18: Modifying SAS Data Sets and Tracking Changes
Creating the Example Data
Data Structures – 1D Lists
Merging in SAS These slides show alternatives regarding the merge of two datasets using the IN data set option (check in the SAS onlinedoc > “BASE SAS”,
3 Iterative Processing.
Combining Data Sets in the DATA step.
Lab 3 and HRP259 Lab and Combining (with SQL)
Trigger %macro check_trigger_run;
Fixed Effects estimate using person level data
Python Basics with Jupyter Notebook
Tips and Tricks for Using Macros to Automate SAS Reporting.
Presentation transcript:

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

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 REGIONAL EXAMINATIONS (0100,0107) CONSULTATION (0110) COMPLETE EXAMINATIONS (0101) COUNSELLING (0120) HOME VISITS EMERGENCY VISITS INSTITUTIONAL VISITS MISCELLANEOUS AND OTHER VISITS (GP) VISIT PREMIUMS (NOT INCLUDED IN SERVICE COUNT 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 = and clntlha = 61; quit; %put pop_lha61=&pop_lha61; %put rec_cnt_lha61=&rec_cnt_lha61;

14 Results (in log) pop_lha61= 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 / / / / / / / / / / / / / / /

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

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

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 **No Name Found** SMITH JOHN M