Creating Macro Calls using Proc Freq

Similar documents
%Addval: A SAS Macro Which Completes the Cartesian Product of Dataset Observations for All Values of a Selected Set of Variables

Using SAS Macro to Include Statistics Output in Clinical Trial Summary Table

A Format to Make the _TYPE_ Field of PROC MEANS Easier to Interpret Matt Pettis, Thomson West, Eagan, MN

SAS Macro Dynamics: from Simple Basics to Powerful Invocations Rick Andrews, Office of Research, Development, and Information, Baltimore, MD

Get Started Writing SAS Macros Luisa Hartman, Jane Liao, Merck Sharp & Dohme Corp.

Checking for Duplicates Wendi L. Wright

A SAS Macro for Producing Benchmarks for Interpreting School Effect Sizes

A Cross-national Comparison Using Stacked Data

ABSTRACT INTRODUCTION MACRO. Paper RF

So Much Data, So Little Time: Splitting Datasets For More Efficient Run Times and Meeting FDA Submission Guidelines

Using SAS/SCL to Create Flexible Programs... A Super-Sized Macro Ellen Michaliszyn, College of American Pathologists, Northfield, IL

Developing Data-Driven SAS Programs Using Proc Contents

Going Under the Hood: How Does the Macro Processor Really Work?

A SAS Macro for Balancing a Weighted Sample

ssh tap sas913 sas

PharmaSUG 2013 CC26 Automating the Labeling of X- Axis Sanjiv Ramalingam, Vertex Pharmaceuticals, Inc., Cambridge, MA

Coders' Corner. Paper Scrolling & Downloading Web Results. Ming C. Lee, Trilogy Consulting, Denver, CO. Abstract.

PharmaSUG Paper TT11

A SAS Macro Utility to Modify and Validate RTF Outputs for Regional Analyses Jagan Mohan Achi, PPD, Austin, TX Joshua N. Winters, PPD, Rochester, NY

ABSTRACT INTRODUCTION TRICK 1: CHOOSE THE BEST METHOD TO CREATE MACRO VARIABLES

Simplifying the Sample Design Process with PROC PMENU

WEB MATERIAL. eappendix 1: SAS code for simulation

Processing SAS Data Sets

Essential ODS Techniques for Creating Reports in PDF Patrick Thornton, SRI International, Menlo Park, CA

SAS Macro Dynamics - From Simple Basics to Powerful Invocations Rick Andrews, Office of the Actuary, CMS, Baltimore, MD

A Simple Framework for Sequentially Processing Hierarchical Data Sets for Large Surveys

A Useful Macro for Converting SAS Data sets into SAS Transport Files in Electronic Submissions

To conceptualize the process, the table below shows the highly correlated covariates in descending order of their R statistic.

1 Files to download. 3 Macro to list the highest and lowest N data values. 2 Reading in the example data file

Posters. Paper

Poster Frequencies of a Multiple Mention Question

Statistics and Data Analysis. Common Pitfalls in SAS Statistical Analysis Macros in a Mass Production Environment

Macros: Data Listings With Power. Daphne Ewing, Synteract, Inc., Ambler, PA

STAT:5400 Computing in Statistics. Other software packages. Microsoft Excel spreadsheet very convenient for entering data in flatfile

Chapter 6: Modifying and Combining Data Sets

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

Paper DB2 table. For a simple read of a table, SQL and DATA step operate with similar efficiency.

Using SAS to Analyze CYP-C Data: Introduction to Procedures. Overview

Let s Get FREQy with our Statistics: Data-Driven Approach to Determining Appropriate Test Statistic

CREATING A SUMMARY TABLE OF NORMALIZED (Z) SCORES

3N Validation to Validate PROC COMPARE Output

Chaining Logic in One Data Step Libing Shi, Ginny Rego Blue Cross Blue Shield of Massachusetts, Boston, MA

A Side of Hash for You To Dig Into

Paper An Automated Reporting Macro to Create Cell Index An Enhanced Revisit. Shi-Tao Yeh, GlaxoSmithKline, King of Prussia, PA

An Easy Route to a Missing Data Report with ODS+PROC FREQ+A Data Step Mike Zdeb, FSL, University at Albany School of Public Health, Rensselaer, NY

The Proc Transpose Cookbook

SD10 A SAS MACRO FOR PERFORMING BACKWARD SELECTION IN PROC SURVEYREG

New Vs. Old Under the Hood with Procs CONTENTS and COMPARE Patricia Hettinger, SAS Professional, Oakbrook Terrace, IL

USING DATA TO SET MACRO PARAMETERS

Windows Application Using.NET and SAS to Produce Custom Rater Reliability Reports Sailesh Vezzu, Educational Testing Service, Princeton, NJ

Exam Questions A00-281

Create a Format from a SAS Data Set Ruth Marisol Rivera, i3 Statprobe, Mexico City, Mexico

Using PROC REPORT to Cross-Tabulate Multiple Response Items Patrick Thornton, SRI International, Menlo Park, CA

Introduction. Getting Started with the Macro Facility CHAPTER 1

Seminar Series: CTSI Presents

Data Acquisition and Integration

EXAMPLE 3: MATCHING DATA FROM RESPONDENTS AT 2 OR MORE WAVES (LONG FORMAT)

Two useful macros to nudge SAS to serve you

Implementing external file processing with no record delimiter via a metadata-driven approach

%MAKE_IT_COUNT: An Example Macro for Dynamic Table Programming Britney Gilbert, Juniper Tree Consulting, Porter, Oklahoma

Data Quality Review for Missing Values and Outliers

A Tool to Compare Different Data Transfers Jun Wang, FMD K&L, Inc., Nanjing, China

Getting Classy: A SAS Macro for CLASS Statement Automation

Generating Least Square Means, Standard Error, Observed Mean, Standard Deviation and Confidence Intervals for Treatment Differences using Proc Mixed

The Ugliest Data I ve Ever Met

Program Validation: Logging the Log

Cleaning Duplicate Observations on a Chessboard of Missing Values Mayrita Vitvitska, ClinOps, LLC, San Francisco, CA

DSCI 325: Handout 15 Introduction to SAS Macro Programming Spring 2017

T.I.P.S. (Techniques and Information for Programming in SAS )

A Tutorial on the SAS Macro Language

Statistics, Data Analysis & Econometrics

SAS Macro Language: Reference

Using PROC SQL to Calculate FIRSTOBS David C. Tabano, Kaiser Permanente, Denver, CO

Amie Bissonett, inventiv Health Clinical, Minneapolis, MN

Ranking Between the Lines

Submitting SAS Code On The Side

A Lazy Programmer s Macro for Descriptive Statistics Tables

My objective is twofold: Examine the capabilities of MP Connect and apply those capabilities to a real-world application.

Using Templates Created by the SAS/STAT Procedures

SAS Macros for Grouping Count and Its Application to Enhance Your Reports

Tweaking your tables: Suppressing superfluous subtotals in PROC TABULATE

An Introduction to SAS Macros

Macro Magic III. SNUG Presentation (30mins)

Correcting for natural time lag bias in non-participants in pre-post intervention evaluation studies

Unlock SAS Code Automation with the Power of Macros

/********************************************/ /* Evaluating the PS distribution!!! */ /********************************************/

Using PROC SQL to Generate Shift Tables More Efficiently

Validation Summary using SYSINFO

PROC MEANS for Disaggregating Statistics in SAS : One Input Data Set and One Output Data Set with Everything You Need

Oh Quartile, Where Art Thou? David Franklin, TheProgrammersCabin.com, Litchfield, NH

PharmaSUG Paper PO12

Utilizing SAS for Cross- Report Verification in a Clinical Trials Setting

A Simple Time Series Macro Scott Hanson, SVP Risk Management, Bank of America, Calabasas, CA

TRANSFORMING MULTIPLE-RECORD DATA INTO SINGLE-RECORD FORMAT WHEN NUMBER OF VARIABLES IS LARGE.

Data Edit-checks Integration using ODS Tagset Niraj J. Pandya, Element Technologies Inc., NJ Vinodh Paida, Impressive Systems Inc.

3. Almost always use system options options compress =yes nocenter; /* mostly use */ options ps=9999 ls=200;

EXST SAS Lab Lab #6: More DATA STEP tasks

Handling Numeric Representation SAS Errors Caused by Simple Floating-Point Arithmetic Computation Fuad J. Foty, U.S. Census Bureau, Washington, DC

PDF Multi-Level Bookmarks via SAS

Want to Do a Better Job? - Select Appropriate Statistical Analysis in Healthcare Research

Transcription:

Creating Macro Calls using Proc Freq, Educational Testing Service, Princeton, NJ ABSTRACT Imagine you were asked to get a series of statistics/tables for each country in the world. You have the data, but when you run a distribution of country you find you have 680 countries. Assume at this point that using different SAS procedures with the by statement will not give you what the client wants. Obviously the easiest method is to create a macro to get the statistics and call the macro once for each country. However, writing the macro calls for all 680 countries would be very tedious. And the next time you are asked to run this, the countries may be different and you would need to retype many of the macro calls. There is a way to use macro language and a proc freq to generate the macro calls you need. The technique is also very useful if you need to run statistics for levels of several demographic variables (e.g. country, income level, educational level, etc.) This paper will demonstrate the use of macro language and a proc freq to generate a macro call for each level of a demographic variable without typing each level and without foreknowledge of exactly what levels exist in the data. INTRODUCTION The repeat macro (see Appendix A) generates calls to the ctable macro (see Appendix B) that runs an analysis and produces a report for each value of a demographic variable. It includes a frequency distribution with percent below and summary statistics (see Appendix C). The called macro ( ctable ) and the sample report are provided only as an example of how the repeat macro can be used. THE BASICS I used several standard SAS techniques in this macro. Among them are: 1. PROC FREQ will create a SAS data set with the results of a frequency distribution using the OUT= option on the TABLES statement. 2. Macro variable substitution using a variable name prefixed with &. 3. The SAS interpreter interprets a && as a single & when it first reads the program. This allows the program to create the macro variables and values before the interpreter attempts to resolve them. HOW THE REPEAT MACRO WORKS To simplify my explanation I created a sample dataset with two variables (score and educational level). The by variable (byvar) that I used is edlevel. Edlevel has six possible values. The macro call will look like this: %repeat(edlevel) When the macro executes, the first step is a PROC FREQ. This creates an output data set called list that has the variables edlevel, count and percent. Note these values are unformatted and that the value of Jun stands for Juniors. Here are a few records from a print of the data set list : OBS Edlevel Count Percent 4 Soph 202 15 5 Jun 281 21 6 Sen 306 23 The data step reads in this output data set and does a couple of things. First it creates a macro variable called totaln that holds the total number of observations in the dataset list. In this example, the value of totaln is 6. The second part of the data step creates a macro variable for each observation of the data set. Each macro variable holds the value of the byvar (edlevel) variable for that observation. For example, for the fifth observation of the list data set, the macro variable created would be x5 and would hold a value of Jun. The last part of the macro: (1) Verifies there were records in the output data set list (2) runs a loop starting with observation 1 to the maximum number of observations in the data set (3) creates a macro call for each iteration of the loop that calls another macro ctable. For example, the macro call generated for the level of Juniors would look like: %ctable (edlevel,jun) The output from the Juniors run of the ctable macro is in Appendix C. CONCLUSION The repeat macro is rather short, but very powerful. It can be used to quickly and easily produce multiple reports based on any demographic criteria without the need for a large amount of typing. AUTHOR CONTACT Your comments and questions are valued and welcome. Contact the author at: Educational Testing Service, 02-L Princeton, NJ 08541 Phone: (609) 683-2292 Fax: (609) 683-2130 E-mail: wwright@ets.org 1

APPENDIX A (the Repeat Macro) %macro repeat (byvar) /* Educational Testing Service June 2001 */ *CALCULATE A LIST OF ALL LEVELS OF THE DEMOGRAPHIC VARIABLE proc freq data=one tables &byvar / out=list *CALCULATE TWO TYPES OF MACRO VARIABLES: 1. TOTALN (one macro variable)= has the number of observations in the list dataset ( or the number of different values found in the demographic variable. 2. X# - has one macro variable for each observation in the dataset list. Will hold the value of the demographic variable that occurs in that observation. data list set list nobs=numobs if _n_ eq 1 then call symput ( totaln,left(numobs)) call symput ( x left(_n_),&byvar) *Using the TOTALN and the X# macro variables created above, call the macro ctable once for each level of the by-variable. %if &totaln ne 0 %then %do %do j=1 %to &totaln %ctable (&byvar,&&x&j) % % %m 2

APPENDIX B (the CTable Macro) %MACRO ctable (var,value) /* Educational Testing Service June 2001 */ * CREATE A MACRO VARIABLE WITH A SAMPLE DESCRIPTION TO BE INCLUDED IN THE TITLE. NOTE: You will need a previously defined format for the var variable. For example, if your variable is named edlevel, then you will need a format name $edlevel. data temp set one if &var eq "&value" then do call symput('samp',trim(put(&var,$&var..))) output *CALCULATE THE FREQ DISTRIBUTION, AND THE PERCENT BELOW proc freq data=temp tables score / out=a noprint data a (keep = score count pbelow) set a retain pblw pbelow 0 output if percent ne. then pblw = pblw + percent else pblw = 0 pbelow = round(pblw,.1) PROC SORT DATA = a BY descending score *CALCULATE THE N, MEAN, AND THE STANDARD DEVIATION proc means data=temp vardef=n var score output out=b mean=mean std=std n=n * PRINT OUT THE FREQUENCY DISTRIBUTION AND THE SUMMARY STATS data c set a if _n_ eq 1 then set b data c set c end=eof file out1 ps=60 n=ps header=h mod notitle if n ge 10 then do put @31 score 3. @38 count 6. @50 pbelow 4.1 3

if eof then do put /// @25 '*************************************' / @35 'N' @41 'Mean' @51 'STD' / @30 n 6. @42 mean 3. @51 std 3. / @25 '*************************************' / return *TITLE THAT GOES ON EVERY PAGE h: put @30 Summary Statistics / @30 Data for Fiscal Year 2000-01 / @30 &samp Examinees // @30 Score @40 Count @49 % Below / return %MEND 4

APPENDIX C (Sample Output from the Ctable macro) Summary Statistics Data for Fiscal Year 2000-01 Edlevel = Junior Examinees Score Count %Below 200 15 95 198 2 94 192 5 92 180 25 83 172 51 65 165 78 37 158 44 22 145 32 10 137 14 5 122 8 2 114 6 0 101 1 0 ************************************* N Mean STD 281 163 91 ************************************* 5