资源描述:
《07-writing-flexible-code-with-the-sas-macro-facility》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、The Little SAS Book: A Primer, Fifth EditionChapter7:WritingFlexibleCodewiththeSASMacroFacility"Nobodyistoooldtolearn—butalotofpeoplekeepputtingitoff".WilliamO'NeillFromTheOfficialExplanationsbyPaulDickson.Copyright1980byDelacortePress.Reprintedbypermi
2、ssionofthepublisher.7.1MacroConceptsNotsolongagotheSASmacrofacilitywasconsideredanadvancedtopicrelevantonlytoexperiencedSASusers.Overtime,however,macroshavebecomemoreprevalentsothatnowevennewSASuserswoulddowelltoknowalittleabouttheSASmacrofacility.Fort
3、unately,thebasicmacroconceptsarenotdifficulttounderstand.ThischapterintroducesthemostcommonlyusedfeaturesoftheSASmacrolanguage.BecausemacrostakelongertowriteanddebugthanstandardSAScode,yougenerallywon'twantmacrosinprogramsthatwillberunonlyafewtimes.But
4、usedproperly,macroscanmakethedevelopmentandmaintenanceofproductionprogramsmucheasier.Theydothisinseveralways.First,withmacrosyoucanmakeonesmallchangeinyourprogramandhaveSASechothatchangethroughoutyourprogram.Second,macrosallowyoutowriteapieceofcodeonce
5、anduseitoverandover,inthesameprogramorindifferentprograms.Youcanevenstoremacrosinacentrallocation—anautocalllibrary—andsharethembetweenprogramsandbetweenprogrammers.Third,youcanmakeyourprogramsdatadriven,lettingSASdecidewhattodobasedonactualdatavalues.
6、TheMacroProcessorWhenyousubmitastandardSASprogram,SAScompilesandthenimmediatelyexecutesit.Butwhenyouwriteamacro,thereisanadditionalstep.BeforeSAScancompileandexecuteyourprogram,SASmustpassyourmacrostatementstothemacroprocessorwhich"resolves"yourmacros,
7、generatingstandardSAScode.Becauseyouarewritingaprogramthatwritesaprogram,thisissometimescalledmeta-programming.MacrosandMacroVariablesSASmacrocodeconsistsoftwobasicparts:macrosandmacrovariables.Thenamesofmacrovariablesareprefixedwith[1]anampersand(&)wh
8、ilethenamesofmacrosareprefixedwithapercentsign(%).Amacrovariableislikeastandarddatavariableexceptthat,havingonlyasinglevalue,itdoesnotbelongtoadataset,anditsvalueisalwayscharacter.Thisvaluecouldbeavariablename,anumeral,oranytextthatyouw