軟件chapter復(fù)旦管理學(xué)院統(tǒng)計(jì)學(xué)系_第1頁(yè)
軟件chapter復(fù)旦管理學(xué)院統(tǒng)計(jì)學(xué)系_第2頁(yè)
軟件chapter復(fù)旦管理學(xué)院統(tǒng)計(jì)學(xué)系_第3頁(yè)
軟件chapter復(fù)旦管理學(xué)院統(tǒng)計(jì)學(xué)系_第4頁(yè)
軟件chapter復(fù)旦管理學(xué)院統(tǒng)計(jì)學(xué)系_第5頁(yè)
已閱讀5頁(yè),還剩230頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

SASSASMACR1/MACR1/11223344556677MACR2/MACR2/可以讓我們從這些繁瑣的重復(fù)工作中解脫出來(lái)。SASMacro的機(jī)制子程序”SAS自己的過(guò)程,完成比MACR3/MACR3/%letmonth_sold=4;procprintdata=books.ytdsalestitle”BooksSoldforMonth&month_sold”;varbooktitlesaleprice;sumsalepriceMACRMACR4/%mend%mendsales%salessalepricelistpricesectionfor&year”Information”Salestitlclassprocmeansdata=books.sold&year%macrosales%doyear=2007MACRMACR5/MONTH_SOLD是一個(gè)宏變量,它用于選取數(shù)據(jù)中的一 和MACR6/ 圖??梢园l(fā)現(xiàn),每個(gè)宏語(yǔ)句都以%開(kāi)始,在 定義的宏變量時(shí),以&MACR7/MACR7/%let %let %let books.ytdsales;label mosale=’Month?of?Sale’;run; tabulat title”SalesDuring&repmword&repyear”wheremosale=&repmonthandyear(datesold)=&repyear;class section; saleprice listprice costtables section all=’**TOTAL**’,(salepricelistprice cost)*(n*f=4.sum*f=dollar10.2);MACR8/procgchartdata=temp(where=(mosale<=&repmonthandyear(datesold)=&repyear));title”SalesThrough&repmword&repyear”pie section coutline=black percent=outsidesumvar=saleprice run;quit;MACRMACR9/ MACRPAGE10/當(dāng)SAS碰到宏變量后,將會(huì)自動(dòng)把替換為該變量的值。下面的程序中,我們用兩個(gè)%LET語(yǔ)句定義了兩個(gè)宏變量:REPTITLE和REPVAR并給它們賦值。在過(guò)程FREQ中,這兩個(gè)變量被。同SAS自動(dòng)定義的。雙引號(hào)不影響宏變量的解析。%let reptitle=BookSection%let repvar=sectiontitle ”Frequenciesby&reptitle of&sysday”; freq data=books.ytdsales;tables&repvar;run;MACRMACR12/ freq data freq data=books.ytdsalestables section Friday”titl ”FrequenciesbyBookSection MACRMACR13/%let reptitle=BookSection%let repvar=sectiontitle ”Frequencies by&reptitle freq data=books.ytdsales;tables&repvar;run;

of&sysday”title ’Means?by?&reptitle?as?of?&sysday’;procmeansdata=books.ytdsalessummaxdec=2;class&repvar; salepriceMACRMACR14/title ”FrequenciesbySection Friday”; freq data=books.ytdsales;tables section;run;title ’Means?by?&reptitle?as?of?&sysday’;procmeansdata=books.ytdsalessummaxdec=2;class section; salepriceMACRMACR15/用PUTSASSYMBOLGEN。這兩種方法將宏變量SASLOGLOG窗口。MACR16/ MACRPAGE17/第法是用%PUT語(yǔ)句。該語(yǔ)句不是數(shù)據(jù)步或過(guò)程步的一部分,它SASLOG中。該語(yǔ)句的用 / /NOTE:/%put<text/_ALL_/_AUTOMATIC__GLOBAL_/_LOCAL_/_USER_所謂TEXT,既可以是文本字符串,也可以是宏變量的。然后是五個(gè),用于指示SAS列出不同類別的宏變量。后邊三個(gè)用于指示SAS輸出信息的類別。這些的意義可以參見(jiàn)下表。Table關(guān)于%put_ALL__AUTOMATIC__GLOBAL__LOCAL__USER_ERROR:%put%put_automatic_;MACRMACR19/_GLOBAL__LOCAL_這兩個(gè)選項(xiàng)。它們能列出全局和局部變量,reptvar=sreptvar=section_global_;reptitle=BookSection%let%letMACRMACR20/從SAS的LOG中可以看到,SAS了REPTITLE和REPTVARMACR21/%let reptitle=BookSection%let reptvar=section%putMymacrovariableREPTITLEhas value&reptitle;%putmymacrovariableREPTVARhas value&reptvar;%putAutomaticmacro variableSYSDAY value&sysday;title ”Frequenciesby&reptitle of&sysday”; freq data=books.ytdsales;tables&reptvarMACRMACR22/SYMBOLGEN選項(xiàng)。該選項(xiàng)對(duì)于宏編SYMBOLGEN選項(xiàng)比使用PUT更加簡(jiǎn)單,但是它的缺點(diǎn)是,該選的類型。SYMBOLGEN只在變量被時(shí)才會(huì)在LOG窗口中輸出而PUTLOGMACR23/MACRMACRPAGE24/optionssymbolgen%let reptitle=BookSection%let repvar=sectiontitle ”Frequencies by&reptitle freq data=books.ytdsales;tables&repvar;run;

of&sysday”title”Meansby&reptitle of&sysday”;procmeansdata=books.ytdsales;class&repvar; saleprice%let macro?%let macro?variable?name=macro?variable?valueSASSAS宏語(yǔ)句的區(qū)別。SAS%let nocalc=53*21+100.%put&noc%let nocalc=53*21+100.%put&nocalc%let %let %let result=&value1+&value2%put&result%let rept report i for *DepartmentXYZ*%put&reptext%let %put®ion%let text=Sales Report%put&text%let Report”%put&moretext%let reptitle=&text®ionMACR26/%let reptitl2=&moretext®ion%put&reptitl2%let sentence=This start%put&sentence%let %put&chars%let &novalue%let holdvars=varnames%put&holdvars

leadingblanks.%let&holdvars=titl %put&holdvars%put&varnames

datesoldMACRMACR27/空格。以%&_的字符串,SAS并不進(jìn)行宏解MACRMACR(.)作為兩個(gè)部分的分隔。MACR29/%let %let level books.ytdsales(where=(month(datesold)=&mosold));attrib over&level length=$3 label=”Cost>$&level”;if cost>&level over&level=’YES’;else over&level=’NO’ freq title ”FrequencyCountofBooksSoldDuringMonth&mosold”;title2”GroupedbyCostOver$&level”;tables over&level;MACRMACR30/ books.ytdsales(where=(month(datesold)=4));attrib over25 label=”Cost>$25”;if cost>25thenover25=’YES’;else over25=’NO’; freq title ”FrequencyCountofBooksSoldDuringMonth4”;title2”GroupedbyCostOver$25”;tables over25;MACRMACR31/&prefix.&prefix.&prefix.5;tables&prefix.&prefix.frprefi%let%let prefi fr tablesQUESTION1QUESTION2QUESTION3QUESTION4QUESTION5;MACRMACR32/data=&survlidata=&survlib..survey1tablesfreqsurvli%letMACRMACR33/datadata=BOOKSURV.survey1freqfreqfreq data=BOOKSURVsurvey1MACRMACR34/ 開(kāi)。有時(shí),我們還要在宏變量前用上多個(gè)“&”,這稱為間接 MACRMACR%letsection1=CertificatiTraining%letsection2=Internet%let section3=NetworkingandCommunication%let secti Systems%letsection5=ProgrammingandApplications%let section6=WebDesign*????Lookfor sectionnumberdefinedbymacrovarn;%let procmeansdata=books.ytdsalestitle ”Sales for Section:&§ion&n”; section=”&§ion&n”; saleprice;run;MACRMACR36/ saleprice saleprice Systems””section”Sales for Sectiondata=books.ytdsalesproctitlMACRMACR37/optionssymbolgenoptionssymbolgen%letsectiSystems%letMACRMACR38/optionssymbolgen%let section1=Certificati andTraining%l%l%l%l%l%l%l

section2=Internetsection3=NetworkingandCommunication;section4=Operating Systems;section5=ProgrammingandApplications;section6=WebDesign;puter;dept2=Reference;%let dept3=Science%let %let wherevar=sectionprocmeansdata=books.ytdsalestitle ”Sales for&wherevar:&&&wherevar&n”;where&wherevar=”&&&wherevar&n”; salepriceMACR39/MACR39/MACR40/ MACR41/%MACRO%MACROprogram<(parameter?list)></option(<textMACRMACR42/宏定義用%MACRO語(yǔ)句開(kāi)始,以%MEND語(yǔ)句結(jié)束。下表列出了定義%MACROSAS語(yǔ)句%MENDMACRMACR43/ DES=PARMBUFF|PBUFFSTMTSTOREMSTORED選項(xiàng)STOREMACRMACR44/MACRMACRPAGE45/ saleschart;goptions reset=all;pattern1c=graybb; ftext=swiss rotate=landscapetitle ”Sales forWeekEnding&sysdate9”; data=temp;wheretoday()?1800<=datesold<=today()?800; section /sumvar=profit type=sum;run;quit;%mendsaleschartSASWORK%s%salescharprofit=saleprice?costprofi label=’Sale?Price?Costformat=dollar8.2;attri books.ytdsalesMACRMACR47/LOG中。該選項(xiàng)有三個(gè)值:NONE、NOAUTOCALLALL。NONESAS不輸出任何關(guān)于宏MACR48/MACRMACRPAGE49/optionsopti mprintMPRINTSAS運(yùn)行的宏按行輸出opti mprintMACR51/%macroprogram(positional?1,positional?2, ...,positional?n);macroprogramreferencingthemacrovariables positional listMACRMACR52/%program%program(value?1,value?2, ... ?n)MACRMACR53/STARTSTOPLISTPARM兩次。在調(diào)用時(shí),對(duì)應(yīng)三個(gè)OPT使用的是空值。options mlogic%macrolistparm(opts start stop)title”BooksSoldbySectionBetween&startand&stopprocmeansdata=books.ytdsales&opts;”&start”d ldatesoldl”&stop”dclasssection saleprice;%mendlistparm*????Fir cal to all3%listparm(nsum,01JUN2007,15JUN2007*????Secondcallto first *????secondandthird specified%listparm(,01SEP2007,15SEP2007

specifiei nullMACR 54/在%MACRO語(yǔ)句后是宏程序的名稱,然后是參數(shù)列表。參數(shù)列表中用“= 值”的方法定義參數(shù)列表中的一系列參數(shù)。當(dāng)宏程序有很多以省略這些參數(shù),SAS自動(dòng)用默認(rèn)值替代這些參數(shù)的位置。MACR55/MACR55/%macroprogram(keyword1=value keyword2=value...,keywordn=value)macroprogramreferencingthemacrovariables thekeywordparameter list%pragram%pragram(keyword1keywrod2keywordnvlauen...MACRMACR56/opti mprintmogic%macrokeyparm(opts=NSUMMINMAX,title”BooksSoldbySectionBetween&startand&stopprocmeansdata=books.ytdsales&opts;where”&start”d le datesold le ”&stop”d;class section; salepriceMACRMACR57//*????Fir cal tospecify al parameters;%keyparm(opts=n *????Secondcal to specify startandstop, i null default forPROC*????Thirdcal to default startandstop,*????specify opts%keyparm(opts=nMACRMACR58/%macroprogram(positional?1,positional?2,...,positional?n,keyword1=value1,keyword2=value2, ...,keywordm= valuem);macroprogramreferencingbothkindsofMACRMACR59/%program%program(positionalvalue?1, positionalvalue?2, ...,positionalvalue?n, keyword1=value,keyword2=value,... keywordm=valueMACRMACR60/MACRMACRPAGE61/optionsmprintmlogictitle”BooksSoldbySectionBetween&startand&stoprocmeansdata=books.ytdsales&stats&othropts;”&start”dledatesoldle”&stop”dclasssectionvar saleprice;run;default stat forDecember%*????and values tobevalid %*????creating theCLASSvariables%mixdparm(,missing,startdefault stat for al ()PARMBUFF%macroprogram(<positional?1,positional?2,...positional?n)>/PARMBUFF;macroprogramreferencingavaryingnumber valuesSYSPBUFF中的所有之自動(dòng)賦給宏程序中相應(yīng)的變量。%macropbuffparms/parmbuff;goptions reset=all;pattern1c=graybb; pattern2c=graydd;pattern3c=white; pattern4c=grayaa;goptions ftext=swiss rotate=landscape; thi section values specified%if&syspbuffne%then%do%let i%let %do%while(&monthne) data=books.ytdsales(where=(month(datetitle”Sales forMonth&month”; section/sumvar=saleprice run;quit%let i=%eval(&isMACR/%*????Processthi sectionwhennoparameter%else%do data=books.ytdsalestitl ” SalesbyQuarter”hbarsection/sumvar=saleprice subgroup=datesold coutline=black; datesold qtr.;run;quit%mendpbuffparms sal%pbuffparms(8, sal%pbuffparms()

forAugustandNovemberfor entir yearMACR 64/MACRPAGE65/%GLOBALmacro?v%GLOBALmacro?variable(s)%LOCALmacro?variable(s)MACRMACR67/optionssymbolgenmprint%let subset=Internet%macroloclmvar%local subset%letsubset=WebDesignprocmeansdata=books.ytdsalesnsummaxdec=2;titl Sales Produced&sysdate9”title2 ”UsesLOCALSUBSETmacro variable:&subset”; section=”&subset”; saleprice;run;%mendloclmvarMACR 68/SASSAS語(yǔ)句的一部分。MACR69/SASSAS宏函數(shù)的風(fēng)格格式已經(jīng)比較熟悉了。MACR70/MACRMACRPAGE71/%INDEX(source,sourcestring%LENGTH(string|text%SCAN(argument,argumentn%SUBSTR(argument,argumentpositionlength%UPCASE(string|textprocmeansdata=books.ytdsalestitle ”Sales for01%substr(&sysdate,3,3) &sysdate9”;where”01%substr(&sysdate,3)”d le datesoldle”&sysdate”d;class section; saleprice;run;%let February April MayJune%let print data=books.ytdsalestitle ”Sales for%scan(&months,&repmonth)”;wheremonth(datesold)=&repmonth; booktitle saleprice;run;(+&!$*)|主要是兩個(gè)函數(shù),%EVAL和%SYSEVALF。這兩個(gè)函數(shù)計(jì)算算術(shù)和邏輯表達(dá)式的值。這些結(jié)果最終轉(zhuǎn)為文本字符串。函數(shù)%EVAL只能做整數(shù)運(yùn)算,函數(shù)%SYSEVALF可以做浮點(diǎn)運(yùn)算。函數(shù)%EVAL的一般形式expressionexpressionexpression/logical%EVAL(arithmetic函數(shù)%SYSEVALFllogica expression<,conversionexpression%SYSEVALF(arithmetic默認(rèn)情況下,函數(shù)%SYSEVALF的計(jì)算結(jié)果是一個(gè)數(shù)然后轉(zhuǎn)成字符串。BOOLEANCEILFLOORINTEGERTable函數(shù)%EVAL和%SYSEVALFPUT%leta=3;%letb=%put3%putMACR 75/..)=%nrstr(.=publisherpublisher%let%letMACRMACR76/%BQUOTE(string|text%NRBQUOTE(sting|text與%BQUOTE相同,另外不解析%和&%QUOTE(string|text%NRQUOTE(string|text作用與%BQUOTE%作用與%QUOTE相同,另外不解析%%STR(string)%NRSTR(string)與%STR相同,且不解析%%SUPERQ(macro-variable-&和%%UNQUOTE(string|textMACR77/MACR77/MACRMACRPAGE78/%l%letp=%str( print run;)%%let myvar=%str(a%’)%myvara′。%let myvar=%str%let myvar=%str(log%(12%put?mand%put?mandMACRPAGE79/%macrofileit(infile)%if%bquote(&infile)NE%do%letchar1=%bquote(%substr(&infile,1,1))%if%bquote(&char1)=%str(%’le;????????????%else?%let?command=FILE?”&infile”;下面的程序是關(guān)于函數(shù)SUPERQ的例子。這個(gè)例子里該函數(shù)了在TESTMV1TESTMV22MV1MV2的解析。SYMPUT是一個(gè)在數(shù)據(jù)步里給宏變量賦值的子過(guò)MACR80/data_null_;call symput(’mv1’,’Smith&Jones’);cal symput(’mv2’,’%macro?abc;’)%let %let %putMacrovariableTESTMV1is&testmv1%putMacrovariableTESTMV2is&testmv2MACRMACR81/MACR82/%SYMEXIST宏變量名%SYMGLOBL宏變量名%SYMLOCAL(宏變量名MACR 83/lobal x;%macrotest%local y%i %symexist(x)%then%nrstr(%symexist(x)) =%else%put%nrstr(%symexist(x)) =%i %symexist(y)%then%nrstr(%symexist(y) =%else%put%nrstr(%symexist(y) =%if%symexist(z)%then%nrstr(%symexist(z)) =%else%put%nrstr(%symexist(z)) =%mendtest%testMACRMACR84/1122%SYSFUNC((參數(shù)%QSYSFUNC((參數(shù)于%SYSFUNC%SYSGET主機(jī)環(huán)境變量%SYSPROD(SAS產(chǎn)品名證MACR85/MACR85/titl ”Sales for%sysfunc(date(),monname.%sysfunc(date(),year.)” getopt(whatopt)%let optvalue=%sysfunc(getoption(&whatopt)) Option&whatopt=&optvalue%mendgetopt%getopt(ps%getopt(ls%getopt(date%getopt(symbolgen%getopt(compressMACRMACR86/MACR87/MACR87/MACR88/1+2—2^*/3乘4除4+加5—減5<LT6LE6=EQ6NE6>GT6GE6&7|8MACRMACR89/一章我們提到的%EVAL和%SYSEVALF將它的參數(shù)臨時(shí)轉(zhuǎn)變?yōu)閿?shù)值型它們會(huì)自動(dòng)地調(diào)用%EVAL函數(shù)。例如%SUBSTR和%QSUBSTR函數(shù)以及%SCAN和%QSCAN函數(shù)。另外,%DO及其相關(guān)語(yǔ)句如%UNTIL和%WHILE以及%IF語(yǔ)句都能自動(dòng)調(diào)用%EVALMACR90/%IF expression%THENaction<%ELSEelseacti%IF expression%THENaction<%ELSEelseactionMACRMACR91/%macro%macrocomp2vars(value1,value2)%putCOMPARISON1:%if &value1 ne&value2%then%put&value1is to&value2..;%else%put&value equals&value2..%putCOMPARISON2:%if &value1>&value2%then%put&value1is greater than&value2..;%else%if &value1<&value2%then%put&value1is less than&value2..;%else%put&value equals&value2..%putCOMPARISON3:%let result=%eval(&value1>&value2)%if&result=1%then%putEVALresult &value1>&value2 isTRUE.;%else%putEVALresul of&value1>&value i%putCOMPARISON4:%let result=%sysevalf(&value1>&value2)%if&result=1%then%putSYSEVALFresultof&value1>&value2 isTRUE.;%else%putSYSEVALFresul of&value1>&valuei MACR/*????F*????FirstcalltoCOMP2VARS;p2vars(3,4)*????SecondcalltoCOMP2VARS;p2vars(3.0,3)*????ThirdcalltoCOMP2VARS;p2vars(X,x)MACRMACR93/REPORTS包含了兩種不同的報(bào)告類型,總結(jié)報(bào)告和詳細(xì)報(bào)告。第一個(gè)REPTYPE,可以取兩個(gè)值,SummaryDetail。宏對(duì)輸出報(bào)告TABULATE過(guò)程。MACR94/ reports(reptype,repmonth)%let %*????Beginsummaryreport section%if%upcase(&reptype)=SUMMARY%then%do%*????Dosummaryreport for reportmonth; tabulate data=books.ytdsales;title ”Sales for&lblmonth”;wheremonth(datesold)=&repmonth;class section; listprice salepricetables section,(listprice saleprice)*(n*f=6.sum*f=dollar12.2);MACRMACR95/ end quarter alsodosummary for qtr%if&repmonth=3or&repmonth=6or&repmonth=9or&repmonth=12%then%do;%let qtrstart=%eval(&repmonth?2);%let strtmo=%sysfunc(mdy(&qtrstart,1%substr(&sysdate,6,2)),monname.); tabulate data=books.ytdsales;title ”Sales for from&strtmo to&lblmonth”;where&qtrstart le month(datesold) le&repmonth;class section listprice salepricetabl section,(listpric saleprice)*(n*f=6.sum*f=dollar12.2)MACRMACR96/%*????Endsummaryreport section detai report section%else%if%upcase(&reptype)=DETAIL%then%do%*????Dodetail report formonth; print data=books.ytdsales;wheremonth(datesold)=&repmonth booktitle listprice saleprice;sumcost listprice saleprice;%*????Enddetai report section%mendreports cal toREPORTSdoesaeport for *???Secondcal toREPORTSdoesaDetaileport for October%reports(Detail,MACRMACR97/主要有%DO循環(huán),%DO-%UNTIL循環(huán)和%DO-%WHILE循環(huán),它們SAS語(yǔ)句中的循環(huán)結(jié)構(gòu)類似。tt %DOmacro?variable=start\%TOstop<\%BYMACRMACR98/%macromultrep(startyear,stopyear) yrvalue=&startyear%to&stopyear;title ”Sales for&yrvalue”;procmeansdata=sales.year&yrvalue;class section cost listprice saleprice;run; data=sales.year&yrvalue section/sumvar=saleprice run;quit%mendmultrep*????Produce3set reports*????onefor2005,onefor2006*????andoneforMACRMACR99/%macrosumyears(startyear,stopyear); manyyears;set

yearvalue=&startyear%to&stopyearsales.year&yearvalue;%let yearstring=; yearvalue=&startyear%to&stopyear%let yearstring=&yearstring&yearvalueMACRMACR100/ data=manyyearstitle ”Charts yzeData for:&yearstring”; section/sumvar=saleprice type=sum;run;quit;*????Concatenatethree sets onefrom2005,one*????2006,andonefromMACRMACR101/ sales.year sales.year data=manyyearstitle ”Charts yzeDatafor: 200520062007”; section/sumvar=saleprice type=sum;run;quit;MACRMACR102/關(guān)于%DO-%UNTIL循環(huán),重復(fù)執(zhí)行一段代碼直到%UNTIL條件滿足為%DO%UNTIL(expression)MACRMACR103/MACR104/%macromosales/parmbuff%let listindex%do%unti (%scan(&syspbuff,&listindex )%let repmonth=%scan(&syspbuff,&listindex);procmeansdata=books.ytdsalesnsum;%if&repmonthne%then%dotitle”Sales duringmonth&repmonth”;wheremonth(datesold)=&repmonth;%else%dotitl ”O(jiān)veral Sales”class section; saleprice%let listindex=%eval(&listindex%mendmosalesMACRMACR105/*????Fir callto stat forMarch,%mosales(35*????Secondcal to overal stats%mosales()MACRMACR106/有類似功能的循環(huán)結(jié)構(gòu)是%DO-%WHILEtt %DO%WHILE(expression)MACRMACR107/ staffsales(salesreps,repmonth)%let %do%while(%scan(&salesreps )%let salesinits=%scan(&salesreps,& procmeansdata=books.ytdsalesnsum;title”Sales for&salesinits month&repmonth”; saleinit=”&salesinits” class section; saleprice%let %mendstaffsales%staffsales(MJMBLTJMB,MACRMACR108/除了用%IF-%THEN-%ELSE結(jié)構(gòu)外,還有一個(gè)分支結(jié)構(gòu)%GOTO,它SAS轉(zhuǎn)到用%LABEL標(biāo)明的地方繼續(xù)執(zhí)行。%LABEL語(yǔ)句的用%%label MACRMACR109/%GOTOl%GOTOlabelMACRMACR110/下面的程序是一個(gè)用%GOTO detail(dsname,varlist)%*DoesDSNAMEexist?%let foundit=%sysfunc(exist(&dsname))%if&foundit l 0%then nodatasettitle ”P(pán)ROCPRINTof&dsname”; print var&varlist;run; finishedMACRMACR111/%nodataset%put ****Dataset&dsnamenotfound%put%* libref I i isnot%* specified assumeatemporary set%*andassignWORKtoDSLIBREF;%let %if 0%then%letdslibref=%scan(&dsname,1,.)%else%let dslibref=work datasets library=&dslibref details;run;quit%finished%menddetail

****MACRMACR112/*????First cal to exists%detail(books.ytdsales, booktitle saleprice cal to does exist%detail(books.ytdsaless, booktitl saleprice cal to for inWORKlibrary%detail(ytdsales, booktitl saleprice)MACRMACR113/SASSAS語(yǔ)言和宏之間提供了一個(gè)接口,該接口可以使這兩種不同MACR114/SYMGET參數(shù)SYMGETN(參數(shù)RESOLVE(參數(shù)CALLSYMPUT宏變量,值CALLSYMPUTX宏變量,CALLEXECUTE參數(shù)MACRMACR115/SYMGETSYMGETN函數(shù),它們?cè)跀?shù)據(jù)步執(zhí)行時(shí),從宏變量列表中獲得宏變量的值。它們的區(qū)別在于,SYMGET獲得的是文本字SYMGETN獲得的是數(shù)值。在使用這兩個(gè)函數(shù)前,它們11yy=symget(’x’)2323MACR116/MACR116/%l%l%l

certific=CNT283817;internet=INT3521P8;%let operatin=OPSI18375%let %let temp; books.ytdsalesattribcompsectlength=$8label=’Sectionsectioncode length=$ label=’Section?Code’*????Constructmacrovariablenamebycompressingsectionnameandtaking first 8characters.whensection=WebDesign,ComPSECT=”WEBDESIG”;compsect=substr(compress(section),1,8);sectionid=symget(compsect)MACRMACR117/YTDSALES中MACR118/%l%l%l

certifiT283817;internet=INT3521P8;%let %let %let temp; books.ytdsalesattri label=’Sectionsectioni label=’Section?ID’*????Constructmacrovariablename sectionnameand first characters.Whensection=WebDesign,COMPSECT=”WebDesig”;compsect=substr(compress(section),1,8);sectionid=symget(compsect);run; print title ”Defining Section Identification section sectionid;MACR 119/MACRMACRPAGE120/%let webfctr=%let intfctr=1.35; temp; books.ytdsales(where=(section (’Web?Design’, ’Internet’)));if section=’Web?Design’ costfctr=symgetn(’webfctr’);else if section=’Internet’ costfctr=symgetn(’intfctr’);newprice=costfctr*cost; print title”P(pán)ricesbasedonCOSTFCTR”; section cost costfctr newprice; dollar8.2;%let managerquarter%let managerquarter%let managerquarter%let managerquarter4=JBR; managers; books.ytdsaleslength managerinits $3;symget(cats(’managerquarter’,put(qtr(datesold),1.))) print title ”Sale DatesandManagers”; datesold managerinits;SYMPUTSYMPUTX可以在數(shù)據(jù)步中創(chuàng)建宏變量。如果宏變量已經(jīng)存在,則ttextCALLSYMPUT(macro?variable1InternetSYMPUTSAS的過(guò)程,因此兩個(gè)參數(shù)必須用引1’’Internet’)symput(’booksect’calresultvresultvar=’n45’cal symput(resultvar,nhigh)2該例中,NHIGHSYMPUT該變量被賦N45。call symputcall symput(cats(’authorname’,put(_n_,4.)),cat(’Author?Name:?’,author));3MACR123/MACR123/MACRMACRPAGE124/CALLSYMPUTX(macroCALLSYMPUTX(macro?variable,text<,symbol?table>)SYMPUT相似,但是,它將賦給宏變量的值前導(dǎo)和結(jié)尾的SYMPUT相同,第三個(gè)參數(shù)是可選的, data_null_; i salepricege45thennhighif call run;procmeansdata=tempnmeanmintitl ”All BooksSold”title2”NumberofforMore salepricecal symput(’n45’cal symput(’n45’,nhigh) iNumberofBooksSoldforMoreThanMACRMACR126/SYMPUTX就可以避免這種問(wèn)題。SYMPUTX的例子。 booktitle $call symputx(’lasttitle’,booktitle);datalines; Java MyEncyclopedia ofStrategicComputerProgrammingEveryday Etiquette%putThevalue ofmacro variableLASTTITLEis&lasttitle..;MACR127/MACR127/procfreq data=books.ytdsales noprint;tables section/out=sectname;data_null_; call symput(’name’ ||put(_n_,1.),section);call symputx(’n’ || put(_n_,1.),count);%put_user_;MACRMACR128/ statsection(section)procmeansdata=books.ytdsales noprint; section=”§ion”; saleprice out=sectionresults mean=avgsalepricemin=minsaleprice data_null_; sectionresultscal symputx(’average’,put(avgsalepricedollar8.2),’G’);call symputx(’min’,put(minsaleprice,dollar8.2),’G’);call symputx(’max’,put(maxsaleprice,dollar8.2),’G’)%* thi %* variabl %*STATSECTIONlocal table%put_local_;%mendstatsectionMACRMACR129/%statsection(Internettitle”Section Results for Sale Price:&average”;title2”MinimumSale Price:&min”;title3 umSale Price:&max”MACRMACR130/execute(’argument’calexecute(’argument’calMACRMACR131/MACR132/EXECUTE的%macrorep60k(section) report headline centernowd; section=”§ion”;title”Sales>$60,000Summaryfor§ion”publishernsalepricedefinepublisher/definen/”NumberofBooksSold” define saleprice/sumformat=dollar10.2”Sale Price” after/summarizedol;MACRMACR133/opti mprintprocmeansdata=books.ytdsalesnwaynoprint;class section; saleprice out=sectsale run;data_null_; sectsaleif totlsale>60000cal execute(cats(’%rep60k(’,section,’)’))MACRMACR134/%macrohighreport(section) report data=books.ytdsalesheadline centernowd; section=”§ion”title”Sales>$60,000Report Section§ion”;publishernsalepricedefinepublisher/definedefinen/”Numbersaleprice/ofBooks;format=dollar10.2 ”Sale Price” after/summarizedol;%mendhighreportMACRMACR135/ lowreport(section) report data=books.ytdsalesnowd; section=”§ion”;title”Sales<$35,000 forSection§ion”columndatesoldnsalepricedefinedatesold/groupformat=month Sold” width=6;definen/”NumberofBooksSold”;define saleprice/sumformat=dollar10.2”Sales Total” after/summarizedol;run;%mendlowreportMACRMACR136/procmeansdata=books.ytdsalesnwaynoprint;class section; saleprice out=sectsale sum=totlsect;run;data_null_; sectsalei totlsect<35000 calexecute(cats(’%lowreport(’,section,’)’));else if totlsect>60000then callexecute(cats(’%highreport(’,section,’)’));run;MACRMACR137/RESOLVE的函數(shù)。該函數(shù)可以用來(lái)解析宏變量或宏函resolvresolve(’argument’MACRMACR138/1122lengthlength textlabel $mcexp=’&label1’textlabel=resolve(macexp)=All theBooksSoldlabel%let33MACR139/MACR139/%let quartersale1=Holiday Clearnace%letquartersale2=2for Price %let quartersale3=Back School%letquartersale4=NewReleases; temp; books.ytdsaleslength

quarter=qtr(datesold);quartersalename= resolve(cats(’&quartersale’,put(quarter,1.)))MACRMACR140/ getsalename(quarter)%if &quarter=1%then%do;Holiday %else%i &quarter=2%then%do2for thePrice of%else%if &quarter=3%then%do;Backto %else%if &quarter=4%then%do;NewReleases%mendgetsalenameMACRMACR141/procmeansdata=books.ytdsalesclass datesold; saleprice out=quarterly datesold qtr.; quarterly

noprint quarterly(keep=datesold saleprice)length quartersalename=resolve(cats(’%getsalename(’put(datesold,qtr.),’)’)) print data=quarterly labeltitle ’Quarter?Sales?with?Quarter?Sale?Name’;label datesold=’Quarter’saleprice=’Total?Sales’quartersalename=’Sale?Name’;MACRMACR142/SAS程序進(jìn)行編寫(xiě),測(cè)試和調(diào)將第一步里寫(xiě)好的程序中,會(huì)變化的常數(shù)改為宏變量。把這些宏變量用%LET定義在過(guò)程步之外。檢測(cè)和調(diào)試改動(dòng)SYMBOLGEN選項(xiàng)。SASMPRINT和SYMBOLGEN用%IF-%THEN和%DO等語(yǔ)句結(jié)構(gòu)加強(qiáng)程序的通用性。MACR143/ MACRPAGE144/對(duì)于我們的數(shù)據(jù),銷售問(wèn)題。我們希望生成關(guān)于計(jì)算機(jī)類書(shū)籍的銷宏需要能夠分析以下變量:COST,LISTPRICE,SALEPRICEPROFITPROFIT:1COST、LISTPRICE、SALEPRICEPROFIT的分析情況;報(bào)告3:按類型和 商分析COST和PROFIT,然后將它輸出到RTF文件格式。COST、LISTPRICE、SALEPRICEPROFIT, titl ”Sal ”;title2”July 1,2007? August31,2007”; temp; books.ytdsales(where=(’01jul2007’dle le ’31aug2007’d));profit=saleprice?costattrib profit label=’Profit’ format=dollar10.2;run; tabulate listprice saleprice profittablesn*f=6.(cost listprice salepriceprofit)*sum=’Total’*f=dollar11.2;keylabeln=’Titles?Sold’;run;

MACR

146/ MACRPAGE147/SALEPRICEPROFIT的情optionstitl ”Sales Report”title2”January 1,2007? March31,2007”; temp; books.ytdsales(where=(’01jan2007’dldatesold le profit=saleprice?cost;attri profi label=’Profit format=dollar10.2; tabulate class section; saleprice profit;tables section all,n*f=6.(saleprice profit)*sum=’Total’*f=dollar11.2/rts=30;keylabel all=’Total?Salesn=’Titles?Sold’ title3 ”Sal for ”; section/type=sumsumvar=salepricecoutline=black percent=outside; section/type=sumsumvar=profitcoutline=black percent=outside;quit;下面的程序用于總結(jié)COST和PROFIT,結(jié)果輸出為RTF格式的文檔中,使用的是SASUSER.TMP T中的GEARS風(fēng)格。odslisting close; rtf style=gears;title ”Sales Report”;title2”January1,2007/November24,2007”; temp; le le profit=saleprice?cost;attrib profit label=’Profit’ format=dollar10.2;run; tabulate class section publisher; profit;tabl section*(publisher all alln*f=6.(cost profit)*sum*f=dollar11.2 rts=30;keylabel all=’Total?Sales’ n=’TitlMACR 149/MACR150/MACR150/%l%l%l%l%l

repyearstart=01jul&repyear;stop=31aug&repyear; listprice saleprice profittitlestart=%sysfunc(putn(”&start”d,worddate.))%let titlestop=%sysfunc(putn(”&stop”d,worddate.)) pageno=1symbolgen;title ”Sales Report”;title2 ”&titlestart?&titlestop”; temp; books.ytdsales(where=(”&start”d l le ”&stop”d));profit=saleprice?cost;attrib profit label=’Profit’ format=dollar10.2;run; tabulate &vars;tablesn*f=6.(&vars)*sum=’Total’*f=dollar11.2;keylabeln=’Titles?Sold’;MACRMACR151/%l%l%l%l%l%l

repyearstart=01jan&repyear;stop=31mar&repyear;classvar=section;vars=saleprice profittitlestart=%sysfunc(putn(”&start”d,worddate.))%let titlestop=%sysfunc(putn(”&stop”d,worddate.))optionspageno=1symbolgen;title ”Sales Report”;title2 ”&titlestart through&titlestop”; temp; books.ytdsales(where=(”&start”d ldatesold le ”&stop”d));profit=saleprice?cost;attri profi label=’Profit format=dollar10.2;

MACR

152/ tabulate class&classvar;var&varstables section all,n*f(&vars)*sum=’Total’*f=dollar11.2/ rts=30;keylabel all=’Total?Sales’n=’Titles?Sold’ title3 ”Sales for Quarter”pie&classvar /type=sumsumvar=%scan(&vars,1)coutline=black percent=outside;pie&classvar /type=sumsumvar=%scan(&vars,2)coutline=black percent=outside;quit;

MACR

153/ MACRPAGE154/%l%l%l%l%l%l%l%l

repyearstart=01jan&repyear;stop=&sysdate;classvar=section pub

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論