Example: bachelor of science

113-2007: List Processing Basics: Creating and …

Paper 113-2007 List Processing basics : Creating and using lists of Macro VariablesRonald J. Fehd, Centers for Disease Control and Prevention, Atlanta, GA, USAArt Carpenter, CA Occidental Consultants, Vista, CA, USAABSTRACTList Processing or dynamic programmingtechniques free up your programmingtime by eliminating hard coded parame-ter values. List values can be obtained di-rectly from information you already have,and there is more than one way to do it!Commonly the list is stored as series ofmacro variables or as a macro variablewith a series of values or even as valuesin a data paper compares and contrasts thebasic methods of treating variable val-ues as parameters and placing them intorepetitive :intermediate :call execute, call symput, dynamic pro-gramming, into:, list Processing , self-modifyingContentsPreparing a Routine2 Making Lists3 using Lists4 Make and Iterate a Macro Array .. 5 Make and Scan a Macro Variable .. 6 Write Calls to a Macro Variable.

Jun 06, 2008 · Paper 113-2007 List Processing Basics: Creating and Using Lists of Macro Variables Ronald J. Fehd, Centers for Disease …

Tags:

  Basics, Using, Lists, Creating, Processing, List processing basics, Creating and, Creating and using lists

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of 113-2007: List Processing Basics: Creating and …

1 Paper 113-2007 List Processing basics : Creating and using lists of Macro VariablesRonald J. Fehd, Centers for Disease Control and Prevention, Atlanta, GA, USAArt Carpenter, CA Occidental Consultants, Vista, CA, USAABSTRACTList Processing or dynamic programmingtechniques free up your programmingtime by eliminating hard coded parame-ter values. List values can be obtained di-rectly from information you already have,and there is more than one way to do it!Commonly the list is stored as series ofmacro variables or as a macro variablewith a series of values or even as valuesin a data paper compares and contrasts thebasic methods of treating variable val-ues as parameters and placing them intorepetitive :intermediate :call execute, call symput, dynamic pro-gramming, into:, list Processing , self-modifyingContentsPreparing a Routine2 Making Lists3 using Lists4 Make and Iterate a Macro Array .. 5 Make and Scan a Macro Variable .. 6 Write Calls to a Macro Variable.

2 8 Write to File then Include .. 9 Call Execute .. 10 Summary13 Suggested Reading .. 13 Bibliography .. 13 Appendix15 Project Files for Batch Processing .. 15 Example Files for Workshop .. 16 Solutions to Examples .. 20 INTRODUCTIONOne of the greatest strengths of the SASR macro language is its ability to build and execute repetitive code. Thelevel of repetition that encourages conversion to a macro depends on the tolerance of the programmer. For Ronand Art a program with a repetition of more than two items is a candidate for we write more sophisticated macros we want to have the macrodetermine as much of the information that it needs as possible. In asimple case consider the macro%doloop:%macro DoLoop;%do I = 1 %to 5;%*..;This loop has a hard coded number of repetitions (5). We can gener-alize by passing the number of repetitions as a parameter:%macro DoLoop(Count);%do I = 1 %to %*..;In a dynamic coding solution the macro will include code so that thenumber of repetitions can be determined by the macro DoLoop;%*.

3 Code determines Count;%do I = 1 %to %*..;The final solution is one kind of dynamic process, and in the following sections of this paper we will present severaldifferent approaches and techniques that are commonly used to solve dynamic coding a dynamic programming situation it is very often desirable to process one or more operations for a series of items themselves may be data sets, variables in a data set, (columns in a table), or values of a column within hallmark of dynamic programming techniques is that the program itself will determine what the list is, build it,1 SASG lobalForum2007 Hands-on Workshops and then use it. In a dynamic program all this is done without programmer the following simpleProcReportstep which routes the output html file = "& "2style = journal;34 Title "Sales in Africa";56 PROC Report data = ;8where region = Africa ;9column product subsidiary, sales;10define product / group;11define subsidiary / across;12define sales / analysis ;13run;14ods html close;If we want a similar report for each region we cannot simply use abystatement in the PROC step because we wanttheTitleand thehtmlfilename to contain the name of the region.

4 Hard coding the value of one region whiletesting is no problem, for two it is irritating, for three it is time to find a better solution. We need a dynamic solutionthat can determine the number of regions, the values of each, and then execute this step for each the following sections we examine this problem from several anglesand we show several different techniques that can be used to build theability to provide the needed examples show the concepts of testing the routine as it is beingdeveloped. prepare routine identify program parameter(s) convert program to routine make list of parameter values iterate with each value: call routinePREPARING A ROUTINEIDENTIFY PROGRAM PARAMETERSOur report has three occurences of the stringAfrica. This is the firstvalue of the variable Region in the data set file = 2 Title Sales inAfrica ;3where Region = Africa ;CONVERT TO ROUTINEThe first step in converting our program to a routine is to keep theprogram running by making it into a parameterized include file.

5 Addone macro variable for each parameter and replace the occurences ofeach parameter value with the macro and replace the three occurences of the stringAfricawith themacro variable reference& Note that the prefix to the macrovariable reference is the special character ampersand (&) and the suf-fix is the character dot (.). In the filename-extension the first dot endsthe macro variable reference and the second dot separates the file-name and the ; file = "& "3 Title "Sales in 4where Region = " 2 SASG lobalForum2007 Hands-on Workshops CONVERT TO MACROF inally change the allocation of the macro variablefrom%Letto% Report(Region=Africa); file = "& "3 Title "Sales in 4where Region = " The next task is to identify the list of pa-rameter values and decide how best togenerate the macro calls shown here inthe test section at the bottom of the lines 5 9: Since one value con-tains a slash (/) we create a newvariableFilenameand change theslash to a hyphen to avoid an oper-ating system clash of folderCentralAmerica/Caribbeannot : How to generate these macro calls?

6 (Region = Africa);29%Report(Region = Asia );30%Report(Region31= Central America/Caribbean);See Report(InData = ,Region =3,OutPath = /*here: same folder*/4);5%local Filename; %Let Filename = 6%** change slash to hypen in7 Central America/Caribbean;8%If %index(&Region,/) %then %Let Filename =9%sysfunc(translate(&Region.,-,/));1011 ods html file = "& "12style = journal;1314 Title "Sales in 1516 PROC Report data = & ;18where Region = " 19column Product Subsidiary, Sales;20define Product / group;21define Subsidiary / across;22define Sales / analysis ;23run;24ods html close;25run; %mend Report;To speed up our testing, we write a verysimple test macro which only lists its pa-rameter ReportTest(Region = );2%put _local_;3run; %Mend;MAKING LISTSWHAT IS A LIST?In conversation we use the wordlistto mean any collection ofitems,however they may be related. An example would be a todo or shop-ping list. The items in the list share a common property: tasks to bedone, items to purchase.

7 In a formal language, such as set theory,a list is defined as an unordered set. In the SAS language a list isa table. For our discussion here, the list is a collection of the uniquevalues of the variable region of the table of List1. natural language: list contains items2. formal language: an unordered set3. SAS: data set or tableWHERE ARE lists ?SAS provides many unique data sets in either sashelp views or sqldictionary tables. DiIorio and Abolafia [5, ] provide a com-parision of sashelp views and sql dictionary tables and a dozen exam-ples. Most summarization procedures freq and means/summary can produce an output data set which contains a unique list of vari-able values. Fehd [8, ] provides usage examples ofthe more common sql dictionary of Lists1. sashelp views2. sql dictionary tables3. summarization procedure output3 SASG lobalForum2007 Hands-on Workshops MAKING UNIQUE FROM DATAFor this reporting example con-sider these methods to extract aunique list of parameter valuesfrom a data set.

8 Each method pro-duces a data set,Regions, with alist of unique values of the variableRegion. We use this list to writethe calls to the Ex00: review orEx02: symputx. proc Sort data = (keep = Region)3out = Regions4nodupkey;5by Region; proc SQL; create table Regions as2select distinct Region3from ;4quit; using LISTSH aving obtained a list of unique values ina table, we next need to convert the liststored in the table to a list available in themacro environment. Then we can gener-ate the series of macro calls from this the following methods to gener-ate these calls to the macro Report: make table into macro array, using either:1. symputxSee sql intoSee call in macro%doloop: .. See Ex04. make table into macro variable,pick item using %scanin%doloopSee Ex06. make table into macro variable containing calls,execute calls in macro variable .. See Ex08. read table, write calls to file and includeSee Ex09. read table, call execute generates calls.

9 See Workshops MAKE AND ITERATE A MACRO ARRAYCall SymputX:Thecall symputxdatastep routine is used to assign a value to amacro variable. By concatenating a num-ber to the macro variable name we cre-ate what is effectively a macro array: asequentially numbered set of macro vari-ables. In this data step a series of macrovariables of the formItem1,Item2, ..,ItemN, is created, each holding the nameof one region. When these macro vari-ables are used inside a macro%doloopthe macro variable form&&Item&Iusesthe&Icounter as an index to the macroarray; this allows us to step through thelist of : In this example we allocate themacro variableNmbrItemsin line 7, andassign it the value of the upper bound ofour macro array in line 15 which is used inthe%doloop in line in lines 5, 17, and 28: the use ofthe macro variableTestingto print to Project(make-unique-sort);2%Include Project(Report-Region-macro);3%Macro Demo(InData = Regions4,InVar = Region5,Testing = 0);6%local I ; %Let I = 0;7%local NmbrItems; %Let NmbrItems = 0;89 DATA _Null_;10do I = 1 to Nrows;11set &InData.

10 Nobs = Nrows;12call symputx( Item !! left(put(I,8.))13,&InVar., l );14end;15call symputx( NmbrItems , Nrows, l );16run;17%if &Testing. %then %put _local_;1819%do I = 1 %to &NmbrItems.;20%Put *Report(&InVar. = & 21%Report(&InVar. = & 22%end;23run; %Mend Demo;2425options mprint;%*echo macro statements;26%Demo(InData = Regions27,InVar = Region28,Testing = 1);Line 17 lists the local macro NMBRITEMS 1078 DEMO INDATA Regions79 DEMO I 080 DEMO ITEM1 Africa81 DEMO ITEM10 Western Europe82 DEMO TESTING 183 DEMO ITEM3 CanadaThe macro%doloopin lines 19 22 writes anote to the log and callsthe macro Report witheach value of *Report(Region = Africa)93 MPRINT(REPORT): ods html file = " " style = journal;94 NOTE: Writing HTML Body file: (REPORT): Title "Sales in Africa";96 MPRINT(REPORT): PROC Report data = nowindows;97 MPRINT(REPORT): where Region = "Africa";Here is the list of thehtml files producedby this 09:36a 17,407 09:36a 15,603 09:36a 16,327 09:36a 15,992 Central 09:35a 15,973 Eastern 09:35a 15,615 Middle 09:36a 16,692 09:36a 17,057 South 09:36a 16,341 United 09:36a 17,404 Western Workshops.))


Related search queries