Planting Your Rows: Using SAS ® Formats to Make the Generation of Zero-Filled Rows in Tables Less Thorny Kathy Hardis Fraeman HASUG Q3 2012.

Slides:



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

Introduction to ReportSmith and Effective Dated Tables
© 2012 Jenzabar, Inc. Prepared by Eric Urff of Daytona State College Presented by Talia Brown of Graceland University
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Creating and Managing Views Using PROC SQL Chapter 7 1.
John Porter Why this presentation? The forms data take for analysis are often different than the forms data take for archival storage Spreadsheets are.
SAS Output Delivery System. Find heart in the sashelp library Double click.
Quick Data Summaries in SAS Start by bringing in data –Use permanent data set for these examples Proc Tabulate –Produces summaries very quickly and easily.
Chapter 2 Section 2 Solving a System of Linear Equations II.
Introduction to SQL Session 1 Retrieving Data From a Single Table.
1 times table 2 times table 3 times table 4 times table 5 times table
Let SAS Do the Coding for You! Robert Williams Business Info Analyst Sr. WellPoint Inc.
PROC_CODEBOOK: An Automated, General Purpose Codebook Generator
Google Labs Code Search James Bidwell Amgen Ltd. 19 th Oct 2010 © 2010.
Collection and Analysis of Data CPH 608 Spring 2015.
Agenda Journalling More Embedded SQL. Journalling.
Don’t Import It! Setting Up a SAS Libname to an Excel Spreadsheet or Access Database… in 10 Seconds or Less! Kevin Druhan Wait Time Improvement Office.
Internet Forms and Database Bob Kisel Amgraf, Inc.
Chapter 15: Combining Data Horizontally 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Using SAS® Information Map Studio
Solving Lab 3 Los Angeles Pierce College Computer Science 560.
Knowing Understanding the Basics Writing your own code SAS Lab.
Bringing Data into SAS From Menu: –File –Import Data –Spreadsheet example first Pick file by browsing Select Library and Member (we will talk about this.
Database Development Tr ươ ng Quý Quỳnh. References UDEMY: SQL Database MasterClass: Go From Pupil To Master! Database Systems - A Practical Approach.
Simulation is the process of studying the behavior of a real system by using a model that replicates the behavior of the system under different scenarios.
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.
THE SIMPLEX ALGORITHM Step 1 The objective row is scanned and the column containing the most negative term is selected (pivotal column) - indicate with.
Lesson Objective: To compare traditional hard engineering to more modern holistic approaches.
DBT544. DB2/400 Advanced Features Level Check Considerations Database Constraints File Overrides Object and Record Locks Trigger Programs.
Chapter 17: Formatting Data 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Writing and Reading XML files with SAS (Statistical Analysis System) What is SAS ? SAS Institute (or SAS, pronounced "sass") is an American developer of.
Chapter 1: Overview of SAS System Basic Concepts of SAS System.
Karnaugh Mapping Digital Electronics. Karnaugh Mapping or K-Mapping This presentation will demonstrate how to Create and label two, three, & four variable.
14b. Accessing Data Files in SAS ®. 1 Prerequisites Recommended modules to complete before viewing this module  1. Introduction to the NLTS2 Training.
Copyright © 2004, SAS Institute Inc. All rights reserved. SASHELP Datasets A real life example Barb Crowther SAS Consultant October 22, 2004.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
Techniques for List Creation (2) Data formatting and control level processing Basics for Interactive Lists Detail lists The Program Interface Interactive.
Tables Learning Support
© 1997 UW CSE 10/27/97H-1 Embedded SQL and the CLI Chapter 7.7.
Operating System Concepts and Techniques Lecture 9 Memory Management-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques,
Real Time Remote Access Comparing SAS and SPSS David Price Quy Do April 2013.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
Computer Information Technology – Section 4-4 Some text and examples used with permission from: Note: We not endorsing or promoting.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
An Analysis of the n- Queens problem Saleem Karamali.
SAS Certification Prep Guide Chapter 7 Creating and Applying User-Defined Formats.
1 Integration of the LCP Reporting Into the E-PRTR Current status and Technical proposal August 4th.
Better Metadata Through SAS® II: %SYSFUNC, PROC DATASETS, and Dictionary Tables.
Beautiful PROC CONTENTS Output Using the ODS Excel Destination Suzanne Dorinski SESUG 2015 Disclaimer: Any views expressed are those of the author and.
HOUSTON PUBLIC LIBRARY Build Your Résumé. Objectives By the end of this course you should know… What a résumé is and the parts that should be included.
Lesson Objectives Aims Key Words Paging, Segmentation, Virtual Memory
SmallTalk Done By: Nathalie Arzu Erienne Burgess Shanice Joseph.
Spreadsheets.
Solving Systems of Equations using Substitution
The Inverse of a Square Matrix
Chapter 8 Advanced SQL Pearson Education © 2014.
Times Tables.

BACK SOLUTION:
זכויות סוציאליות.
أنماط الإدارة المدرسية وتفويض السلطة الدكتور أشرف الصايغ
Please thank our sponsors!
SAS Libname Quiz (You have 5 mins to complete, fill in the blanks)
3 Views.
3 times tables.
6 times tables.

THE SIMPLEX ALGORITHM Step 1
Presentation transcript:

Planting Your Rows: Using SAS ® Formats to Make the Generation of Zero-Filled Rows in Tables Less Thorny Kathy Hardis Fraeman HASUG Q3 2012

Planting your rows with SAS ® Formats One of many clever tricks that can be done with SAS formats Techniques used for this trick are not unique to this trick, can be applied to other programming tricks I SAS formats!

When to use this trick with SAS formats Need tables with all possible values of a variable or variables All possible values of the variable dont exist in the data 3

Solution without using SAS Formats Hard code all possible values of a variable in the program Disadvantages –Tedious –Potential for large number of variables and/or values –Possible values could change over time 4

Solution using SAS Formats Attach a format to each variable with all possible values described Advantages –Dynamic –Only need to include variable values in the SAS format 5

SAS Programming Techniques 1. Saving a permanent SAS format library 2. Attach a format to a variable 3. Determine the format attached to a variable –SCL –SQL 4. Identify the data values defined in the attached format using CNTLOUT 6

Technique 1 Saving a permanent SAS format library libname library ">>pathname<<"; proc format library = library;... run; 7

Permanent SAS format library 8 Named formats Stored as a SAS Catalog

SAMPLE DATA Obs employee year num dollar 1 Hall FY $10, Hall FY $15, Oates FY $ Brooks FY $11, Brooks FY $12, Abbott FY $75, Abbott FY $99, Costello FY $33, Costello FY $44,

Variables with attached formats EMPLOYEE and YEAR are not text variables Variables have attached formats –EMPLOYEE has an attached format emplfmt. –YEAR has an attached format yearfmt. 10

SAS Formats attached to variables proc format library = library; value emplfmt 1 = "Hall" 2 = "Oates" 3 = "Brooks" 4 = "Dunn" 5 = "Abbott" 6 = "Costello" ; value yearfmt 2008 = "FY 2008" 2009 = "FY 2009" 2010 = "FY 2010" ; run; 11

Technique 2 Attach format to the variable data in.sales; set sales; format employee emplfmt. year yearfmt. ; run; 12

Obs employee year num dollar 1 Hall FY $10, Hall FY $15, Oates FY $ Brooks FY $11, Brooks FY $12, Abbott FY $75, Abbott FY $99, Costello FY $33, Costello FY $44, Dunn hasnt sold anything No sales by anyone in FY 2009 Oates didnt sell anything in

REPORT WITH MISSING ROWS Number Amount of Year Employee of Sales Sales ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ FY 2008 Hall 10 $10, Oates 8 $ Brooks 15 $11, Abbott 50 $75, Costello 33 $33, FY 2010 Hall 15 $15, Brooks 20 $12, Abbott 75 $99, Costello 44 $44,

Technique 3 Determine a Variables Format Dont need to hard code the name of a variables format unless in a FORMAT statement Can use two techniques to determine a variables format –%SYSFUNC with SCL –A dictionary table with PROC SQL 15

Method 1 -- %SYSFUNC with SAS SCL %let dsid = %sysfunc(open(IN.SALES, i)); %let varnum = %sysfunc(varnum(&dsid,EMPLOYEE)); %let format = %sysfunc(varfmt(&dsid, &varnum)); %let rc = %sysfunc(close(&dsid)); %put EMPLOYEE VARIABLE FORMAT = &format; 999 %put EMPLOYEE VARIABLE FORMAT = &format; EMPLOYEE VARIABLE FORMAT = EMPLFMT. Note that the. in the format name is included in the macro variable &format 16

Method 2 – PROC SQL DICTIONARY TABLES proc sql; create table formats as select format from dictionary.columns where upcase(libname) = 'IN' and upcase(memname) = 'SALES' and upcase(name) = ('EMPLOYEE') ; quit; 17

PROC SQL Dictionary Table Output proc print data=formats; run; Obs format 1 EMPLFMT. Again note that the. in the format name is included in the value of the variable 18

Technique 4 Identify all values of a SAS format CNTLOUT Option of PROC FORMAT –FMTNAME – name of the format –START – starting value of the format –LABEL – descriptive label associated with the value of START 19

PROC FORMAT Code proc format library = library cntlout = formatlib (keep = fmtname start label); run; proc print data = formatlib; run; 20

Output data set FORMATLIB Obs FMTNAME START LABEL 1 EMPLFMT 1 Hall 2 EMPLFMT 2 Oates 3 EMPLFMT 3 Brooks 4 EMPLFMT 4 Dunn 5 EMPLFMT 5 Abbott 6 EMPLFMT 6 Costello 7 YEARFMT 2008 FY YEARFMT 2009 FY YEARFMT 2010 FY

Output data set FORMATLIB Note that the variable with the name of the format FMTNAME does not have a. at the end Can add a. to the end fmtname = cats(fmtname,.); 22

Putting it all together – finding all possible values for variables using attached formats Uses previously described techniques Can be done with SCL or SQL Complete code is provided 23

SAS Macro to put it all together Two macros, one for each technique –%SYSFUNC with SCL –PROC SQL Dictionary tables Both macros use CNTLOUT Both macros input name of variable, output values of variable in a data set %getfmt1(var=employee, outvals=empvals); %getfmt1(var=year, outvals=yearvals); 24

Method 1 – SCL %macro getfmt1(var=, outvals=); %let dsid = %sysfunc(open(IN.SALES,i)); /* Input data is hard coded*/ %let varnum = %sysfunc(varnum(&dsid, &var)); %let varfmt = %sysfunc(varfmt(&dsid, &varnum)); %let rc = %sysfunc(close(&dsid)); %put &varfmt; proc format library = library cntlout = &outvals (keep = fmtname start label where = (cats(fmtname,'.') = "&varfmt")); run; title "Data set &outvals from macro GETFMT1 -- SYSFUNC and SCL"; proc print data = &outvals; run; %mend getfmt1; %getfmt1(var=employee, outvals=empvals); %getfmt1(var=year, outvals=yearvals); 25

Method 2 – SQL (part 1) %macro getfmt2(var=, outvals=); proc sql; create table &var.fmt as select format from dictionary.columns where upcase(libname) = 'IN' and /* Input data is hard coded*/ upcase(memname) = 'SALES' and upcase(name) = upcase("&var") ; quit; data _null_; set &var.fmt; call symputX("varfmt", format, 'L'); run; %put &varfmt; 26

Method 2 – SQL (part 2) proc format library = library cntlout = &outvals (keep = fmtname start label where = (cats(fmtname,'.') = "&varfmt")); run; title "Data set &outvals from macro GETFMT2 SQL Dictionary Table"; proc print data = &outvals; run; %mend getfmt2; %getfmt2(var=employee, outvals=empvals); %getfmt2(var=year, outvals=yearvals); 27

Output Data from %GETFMT1 (SCL) Data set empvals from macro GETFMT1 -- SYSFUNC and SCL Obs FMTNAME START LABEL 1 EMPLFMT 1 Hall 2 EMPLFMT 2 Oates 3 EMPLFMT 3 Brooks 4 EMPLFMT 4 Dunn 5 EMPLFMT 5 Abbot 6 EMPLFMT 6 Costello Data set yearvals from macro GETFMT1 -- SYSFUNC and SCL Obs FMTNAME START LABEL 1 YEARFMT 2008 FY YEARFMT 2009 FY YEARFMT 2010 FY

Output Data from %GETFMT2 (SQL) Data set empvals from macro GETFMT2 SQL Dictionary Table Obs FMTNAME START LABEL 1 EMPLFMT 1 Hall 2 EMPLFMT 2 Oates 3 EMPLFMT 3 Brooks 4 EMPLFMT 4 Dunn 5 EMPLFMT 5 Abbot 6 EMPLFMT 6 Costello Data set yearvals from macro GETFMT2 SQL Dictionary Table Obs FMTNAME START LABEL 1 YEARFMT 2008 FY YEARFMT 2009 FY YEARFMT 2010 FY

Data set with all possible values of both variables Uses data sets created by macros –EMPVALS – all possible values of EMPLOYEE –YEARVALS – all possible values of YEAR 30

Data set with all possible values of both variables Combined with SQL join to create a data set will all 6 possible values of the variable EMPLOYEE with all 3 possible values of the variable YEAR Name of combined data set is ALLROWS ALLROWS has 6 x 3 = 18 observations 31

SQL Join to create ALLROWS data yearvals_mod (keep = year a); set yearvals; year = input(trim(left(start)),8.); a = 1; run; data empvals_mod (keep = employee b); set empvals; employee = input(trim(left(start)),8.); b = 1; run; proc sql; create table allrows as select year, employee from yearvals_mod y, empvals_mod e where y.a = e.b; quit; proc sort data=allrows; by year employee; run; 32

Data set ALLROWS All rows needed for table Formats will automatically be added later in the merge Obs year employee

Create data set for report proc sort data=in.sales out=sales; by year employee; run; data sales_all; merge allrows (in=a) sales (in=s); by year employee; /* */ /* Zero-fill the missing rows /* */ if a and ^s then do; num = 0; dollar = 0; end; run; 34

Data set SALES_ALL Input data for report Obs year employee num dollar 1 FY 2008 Hall 10 $10, FY 2008 Oates 8 $ FY 2008 Brooks 15 $11, FY 2008 Dunn 0 $ FY 2008 Abbott 50 $75, FY 2008 Costello 33 $33, FY 2009 Hall 0 $ FY 2009 Oates 0 $ FY 2009 Brooks 0 $ FY 2009 Dunn 0 $ FY 2009 Abbott 0 $ FY 2009 Costello 0 $ FY 2010 Hall 15 $15, FY 2010 Oates 0 $ FY 2010 Brooks 20 $12, FY 2010 Dunn 0 $ FY 2010 Abbott 75 $99, FY 2010 Costello 44 $44,

REPORT WITHOUT MISSING ROWS Number Amount of Year Employee Sales Sales ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ FY 2008 Hall 10 $10, Oates 8 $ Brooks 15 $11, Dunn 0 $0.00 Abbott 50 $75, Costello 33 $33, FY 2009 Hall 0 $0.00 Oates 0 $0.00 Brooks 0 $0.00 Dunn 0 $0.00 Abbott 0 $0.00 Costello 0 $0.00 FY 2010 Hall 15 $15, Oates 0 $0.00 Brooks 20 $12, Dunn 0 $0.00 Abbott 75 $99, Costello 44 $44,

Conclusions Use of SAS formats can improve the programming process Techniques used for this programming trick can be adapted to other programming tricks 37