1 Jerry Tsai Jerry.Tsai@clintuition.com This presentation and code available at: clintuition.com/pubs/
22 Efficiently Handle Lists of Items using the SAS® Macro Language --- Save Time and Reduce Errors Jerry Tsai
3 Problem Statement Working with a data set that contains many variables often requires repeating the same editing task over and over on those variables. Problems with having to make repetitive edits: Increased chance of inconsistency, leading to errors in syntax or in logic Increase in time required for task completion
4 Data Set Examples #1: Laboratory values Variables: hemoglobin, hematocrit, MCH, MCV, RBC, platelets, etc. #2: Survey data Variables: Question 1, Question 2, Question 3, etc.
5 Desired Solution Write code addressing these multiple items with as little unnecessary repetition as possible– and do it correctly the first time.
6 Process Flow Write code that addresses one item on the list and validate that it works as desired. Extend the validated code to all items on the list.
7 Method Edit the validated code for use with a macro: Replace the list item with a wildcard character. With each iteration, this wildcard will be replaced with a item on the list. The macro will iterate through the list using a loop.
8 MACRO %ITERLIST Two parameters: LIST CODE %iterlist(list=, code=)
20 Advantages of %ITERLIST() Robust approach separates programming logic from the items being iterated over. Save programming time by avoiding needless typing. By retaining the syntax of validated code, reduce the likelihood of error.
22 Suggested Reading Carpenter, Art. Resolving and Using &&var&i Macro Variables - SUGI 22 Proceedings (1997) Carpenter, Art. Storing and Using a List of Values in a Macro Variable - SUGI 30 Proceedings (2005) Morris, Robert. Text Utility Macros for Manipulating Lists of Variable Names - SUGI 30 Proceedings (2005) Clay, Ted. Tight Looping With Macro Arrays - SUGI 31 Proceedings (2006)
23 Jerry Tsai Jerry.Tsai@clintuition.com This presentation and code available at: clintuition.com/pubs/ Questions? Comments?