1 / 16

Creating and Using Custom Formats for Data Manipulation and Summarization

Creating and Using Custom Formats for Data Manipulation and Summarization. Presentation to Denver SAS Users Group January 14, 2009. Presented by John Schmitz, Ph.D. Schmitz Analytic Solutions, LLC Certified Advanced Programmer for SAS ®9. Summary. Summary:

dima
Download Presentation

Creating and Using Custom Formats for Data Manipulation and Summarization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating and Using Custom Formats for Data Manipulation and Summarization Presentation to Denver SAS Users Group January 14, 2009 Presented by John Schmitz, Ph.D. Schmitz Analytic Solutions, LLC Certified Advanced Programmer for SAS ®9

  2. Summary • Summary: • This presentation will review creation of custom formats directly in PROC FORMAT as well as through the data step. • These formats will then be used as lookup tables in a data step, replacing a merge and for summarization within a CLASS statement. • Audience: • The presentation is introductory to intermediate level. No experience with SAS formats is assumed. • This talk will serve as a base for our second talk on Multilabel formats. Schmitz Analytic Solutions, 2009

  3. Outline • Define meaning of a ‘SAS Custom Format’ • Creating custom formats with PROC FORMAT. • Creating custom formats in a DATA STEP. • Using a custom format within a CLASS statement. • Using a custom format in place of a data merge. Schmitz Analytic Solutions, 2009

  4. Before I Begin • Nikki Carroll presented ‘Tap Into the Power of Formats’ (JUL 07). That presentation is available on www.denversug.org. It will provide a nice complement to the comments I have here. Schmitz Analytic Solutions, 2009

  5. A Quick note on Naming Convention • Throughout today’s presentations, you will notice a naming convention. • Format names end in Fmt • Informat names end in InFmt • Multilabel formats end in MLF • When data sets are used, dataset names match the name of the format they define. • This is my naming convention and is not required to make the code work. Schmitz Analytic Solutions, 2009

  6. Defining a ‘SAS Custom Format’ • SAS has many built in formats that most users already use: (6.2, DOLLAR8.2, YYQ6., COMMA15.0, $15., DATE9.) • Base SAS provides PROC FORMAT which allows the user to expand the pre-defined format list with formats tailored to specific needs. These are ‘SAS Custom Formats’. • PROC FORMAT allows creation of a • FORMAT (numeric or character) • INFORMAT (numeric or character) • PICTURE (numeric) • Custom formats are stored in catalogs. • by default in work.formats and cleared at end of session. • User can define an alternate catalog and retain formats. Schmitz Analytic Solutions, 2009

  7. Creating a simple custom format PROC FORMAT; value $BossNamefmt ‘1’ = ‘Abe Lincoln’ ‘2’ = ‘George Washington’; RUN; DATAexample1; SET sashelp.company; boss= PUT(DeptHead,$BossNameFmt20.); RUN; Input field is $15 format so a character format is required. Use format width to define length of output character variable. For a PUT, match VARIABLE and FORMAT types, output will be CHAR. For a INPUT, use CHAR variable, output type will match format type. Use FORMAT with PUT and use INFORMAT with INPUT. See SAS Example1. Schmitz Analytic Solutions, 2009

  8. A quick INFORMAT Case PROC FORMAT; invalue $GroupInFmt 'FINANCE' = 'FIN' ‘MARKETING' = 'M/S' 'SALES' = 'M/S' 'MIS' = 'MIS' other = 'N/A’; invalue CityInFmt 'LONDON' = 1 'NEW YORK' = 2 'TOKYO' =3; RUN; DATA example2; setsashelp.company (keep=level2 level4); GroupCode = input(LEVEL4,$GroupInFmt.); CityID = input(LEVEL2,CityInFmt.); RUN; This format is a great example of how custom formats could be used to replace a large SELECT statement within code. Keyword OTHER is used for any unspecified value. Output variables are same type as the informat used. See SAS Example2. Schmitz Analytic Solutions, 2009

  9. A more realistic format example PROC FORMAT; value seasonsFmt other = ‘Missing’ low- <‘21DEC2008’d = ‘Prior ’ ‘21DEC2008’d- <’20MAR2009’d = ‘2009S1’ ’20MAR2009’d - <’21JUN2009’d = ‘2009S2’ ’21JUN2009’d- <’22SEP2009’d = ‘2009S3’ ’22SEP2009’d- <’21DEC2009’d = ‘2009S4’ ’21DEC2009’d- high = ‘Post ’; RUN; • < START is INCLUSIVE. END is EXCLUSIVE • <- START is EXCLUSIVE. END is INCLUSIVE • <-< NEITHER VALUE IS INCLUSIVE • - BOTH VALUES ARE INCLUSIVE Special values LOW and HIGH can be used for MIN and MAX values. Schmitz Analytic Solutions, 2009

  10. Creating custom formats in a DATA STEP. • The PROC FORMAT examples shown have many limitations. • More complex cases can become very lengthy. • Content is not dynamic. • Code can be difficult to maintain. • May wish to ‘auto generate’ code from data set contents from programming logic. • DATA STEPS can be used to greatly expand the usability of custom formats. Schmitz Analytic Solutions, 2009

  11. PROC FORMAT IMPORT / EXPORT FEATURE • PROC FORMAT provides options to read (write) format definitions from (to) data sets. • To generate a data set containing data from custom formats: • PROC FORMAT cntlout = formats; • RUN; • To generate a (or multiple) custom format from data set: • PROC FORMAT cntlin = formatData; • RUN; This data file must contain variables: START FMTNAME and LABEL. Other fields can be included to further control the format definition. Schmitz Analytic Solutions, 2009

  12. Equinox data Data stored in EquinoxData.xls Schmitz Analytic Solutions, 2009

  13. Converting RAW DATA to FORMAT data SeasonsFmt; set equinoxData (rename=(begins=START ends=END season=LABEL)) end=last; length HLO $5; retain fmtname 'SeasonsFmt' SEXCL 'N' EEXCL 'Y' hlo '‘ firstDate ; if _N_ = 1 then firstDate = start; output; if last then do; start = end; end=.; label = 'POST'; hlo = 'H';EEXCL=‘N’; output; EEXCL=‘Y’; hlo=‘O'; start=.; end=.; label='MISSING'; output; start = .; end= FirstDate; label = 'PRIOR'; hlo = 'L'; output; end; keep start end label fmtname hlo sexcl eexcl; format start end date9.; run; Schmitz Analytic Solutions, 2009

  14. Using the Custom Format in PROC MEANS • DATA sample; • set sashelp.citiday • (keep=date SNYDJCM); • RUN; • PROC MEANS data=sample N MEAN; • class DATE; • format DATEseasonsFmt.; • var SNYDJCM; • RUN; See SAS Example3. Schmitz Analytic Solutions, 2009

  15. Using custom formats as a convenient lookup table. • Custom formats can be used for lookup tables, potentially avoiding complex table joins and resource intensive sorts. • These lookup tables are limited to one field as a lookup criteria. * • Use of lookup formats will require additional memory since the format definition is maintained in memory. • They perform best when the lookup table is small relative to the main table. • * One can use HASH tables for lookups involving multiple criteria and obtain similar performance benefits. Schmitz Analytic Solutions, 2009

  16. COMMENTS / QUESTIONS? For More Information, Contact John Schmitz Schmitz Analytic Solutions Phone: 303-482-1860 Email: Jschmitz@SchmitzAnalytics.com Web: http://www.SchmitzAnalyticSolutions.com Schmitz Analytic Solutions, 2009

More Related