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 WorkshopsWRITE CALLS TO A MACRO VARIABLEIn the previous example the macro variable<strong>List</strong> was used to hold only the list<strong>of</strong> distinct regions. Since we want to useeach region in a macro call we could constructthe list to contain the macro callsthemselves.In this example the concatenation functioncat is used to build the macro call.Note the function cat does not trim trailingspaces; thus we use trim(Item) inline 11. Compare the function cat, usedhere, to the function cats in later examples.See Ex07 <strong>and</strong> Ex08, pg. 17.write-calls-into-mvar.sas1 %Include Project(ReportTest);2 %<strong>Macro</strong> Demo(InData =3 ,InVar =4 ,<strong>Macro</strong>Name =5 ,Testing = 0);67 PROC SQL %If not &Testing. %then noprint;8 ; select distinct &InVar. as Item,9 cat( ’%’10 , "&<strong>Macro</strong>Name.(&InVar.="11 , trim(Item), ’)’12 ) as Call<strong>Macro</strong>13 into :Item,14 :<strong>List</strong> separated by ’ ’15 from &InData.;16 quit;17 run;18 %If not &Testing. %then19 &<strong>List</strong>.; %*execute statements in mvar <strong>List</strong>;20 run; %Mend Demo;When testing,. . . the calls <strong>of</strong> themacro are writtento the list:When not testing. . . the calls are executed:write-calls-into-mvar.log43 25 %Demo(InData = sashelp.Shoes44 26 ,InVar = Region45 27 ,<strong>Macro</strong>Name = ReportTest46 28 ,Testing = 1);47 MPRINT(DEMO): PROC SQL ;48 MPRINT(DEMO): select distinct Region as Item, cat( ’%’ ,49 "ReportTest(Region=" , trim(Item), ’)’ ) as Call<strong>Macro</strong> into :Item, :<strong>List</strong>50 separated by ’ ’ from sashelp.Shoes;51 MPRINT(DEMO): quit;52 NOTE: The PROCEDURE SQL printed page 1.write-calls-into-mvar.lst6 Item Call<strong>Macro</strong>7 ------------------------------------------------------------------------8 Africa %ReportTest(Region=Africa)9 Asia %ReportTest(Region=Asia)10 Canada %ReportTest(Region=Canada)write-calls-into-mvar.log62 29 %Demo(InData = sashelp.Shoes63 30 ,InVar = Region64 31 ,<strong>Macro</strong>Name = ReportTest65 32 ,Testing = 0);66 MPRINT(DEMO): PROC SQL noprint ;67 MPRINT(DEMO): select distinct Region as Item, cat( ’%’ ,68 "ReportTest(Region=" , trim(Item), ’)’ ) as Call<strong>Macro</strong> into :Item, :<strong>List</strong>69 separated by ’ ’ from sashelp.Shoes;70 MPRINT(DEMO): quit;write-calls-into-mvar.log78 MPRINT(DEMO): run;79 REPORTTEST REGION Africa80 MPRINT(REPORTTEST): run;81 REPORTTEST REGION Asia82 MPRINT(REPORTTEST): run;8

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

Saved successfully!

Ooh no, something went wrong!