12.07.2015 Views

List Processing Basics: Creating and Using Lists of Macro ... - NESUG

List Processing Basics: Creating and Using Lists of Macro ... - NESUG

List Processing Basics: Creating and Using Lists of Macro ... - NESUG

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>NESUG</strong> 2007H<strong>and</strong>s-On WorkshopsMAKE AND ITERATE A MACRO ARRAYCall SymputX: The call symputx datastep routine is used to assign a value to amacro variable. By concatenating a numberto the macro variable name we createwhat is effectively a macro array: asequentially numbered set <strong>of</strong> macro variables.In this data step a series <strong>of</strong> macrovariables <strong>of</strong> the form Item1, Item2, . . . ,ItemN, is created, each holding the name<strong>of</strong> one region. When these macro variablesare used inside a macro %do loopthe macro variable form &&Item&I usesthe &I counter as an index to the macroarray; this allows us to step through thelist <strong>of</strong> values.Note: In this example we allocate themacro variable NmbrItems in line 7, <strong>and</strong>assign it the value <strong>of</strong> the upper bound <strong>of</strong>our macro array in line 15 which is used inthe %do loop in line 19.Note in lines 5, 17, <strong>and</strong> 28: the use <strong>of</strong>the macro variable Testing to print to thelog.See Ex02, pg. 16.Line 17 lists the local macro variables.make-array-symputx.sas1 %Include Project(make-unique-sort);2 %Include Project(Report-Region-macro);3 %<strong>Macro</strong> Demo(InData = Regions4 ,InVar = Region5 ,Testing = 0);6 %local I ; %Let I = 0;7 %local NmbrItems; %Let NmbrItems = 0;89 DATA _Null_;10 do I = 1 to Nrows;11 set &InData. nobs = Nrows;12 call symputx(’Item’ !! left(put(I,8.))13 ,&InVar., ’l’);14 end;15 call symputx(’NmbrItems’, Nrows, ’l’);16 run;17 %if &Testing. %then %put _local_;1819 %do I = 1 %to &NmbrItems.;20 %Put *Report(&InVar. = &&Item&I.);21 %Report(&InVar. = &&Item&I.);22 %end;23 run; %Mend Demo;2425 options mprint;%*echo macro statements;26 %Demo(InData = Regions27 ,InVar = Region28 ,Testing = 1);make-array-symputx.log77 DEMO NMBRITEMS 1078 DEMO INDATA Regions79 DEMO I 080 DEMO ITEM1 Africa81 DEMO ITEM10 Western Europe82 DEMO TESTING 183 DEMO ITEM3 CanadaThe macro %do loopin lines 19–22 writes anote to the log <strong>and</strong> callsthe macro Report witheach value <strong>of</strong> Region.make-array-symputx.log92 *Report(Region = Africa)93 MPRINT(REPORT): ods html file = "Africa.html" style = journal;94 NOTE: Writing HTML Body file: Africa.html95 MPRINT(REPORT): Title "Sales in Africa";96 MPRINT(REPORT): PROC Report data = sashelp.Shoes nowindows;97 MPRINT(REPORT): where Region = "Africa";Here is the list <strong>of</strong> thehtml files producedby this method:zdir-html.txt6 07/06/2007 08:32a 17,407 Africa.html7 07/06/2007 08:32a 15,603 Asia.html8 07/06/2007 08:32a 16,327 Canada.html9 07/06/2007 08:32a 15,992 Central America-Caribbean.html10 07/06/2007 08:32a 15,973 Eastern Europe.html11 07/06/2007 08:32a 15,615 Middle East.html12 07/06/2007 08:32a 16,692 Pacific.html13 07/06/2007 08:32a 17,057 South America.html14 07/06/2007 08:32a 16,341 United States.html15 07/06/2007 08:32a 17,404 Western Europe.html5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!