Data步与Base模块程序

    技术2022-05-20  51

    对同一个问题,论坛里面的大牛通常可以给出不同的解决方案。其中,DATA步和BASE模块程序就可以殊途同归。SAS Global Forum 2011中有篇文章对部分情况作了概括,《Choosing the Road Less Traveled: Performing Similar Tasks with either SAS® 

     DATA Step Processing or with Base SAS® Procedures》  ——Kathryn McLawhorn, SAS Institute Inc., Cary, NC

     这里摘录了文章的代码。

    1.CALCULATING THE NUMBER OF OBSERVATIONS IN A BY GROUP

    DATA STEP METHOD

    proc sort data=sashelp.class out=class(keep=age);   by age;run;data class1;   set class;   by age;   if first.age then count=0;      count+1;   if last.age then output;run;proc print noobs;run;

    PROC FREQ

    proc freq data=sashelp.class;   tables age / out=class2(drop=percent);run;proc print data=class2 noobs;run;

    2.COMPUTING A TOTAL FOR A BY GROUP

    DATA STEP

    data scores;   input id $ game1 game2;   datalines;A 2 3A 5 6B 1 2C 1 2C 4 5C 7 8;run;proc sort data=scores;   by id;run;data grandtot(drop=temp1 temp2);   set scores (rename=(game1=temp1 game2=temp2));   by id;   if first.id then do;      game1=0;   game2=0;      end;   game1+temp1;   game2+temp2;   if last.id then output;run;proc print data=grandtot noobs;run;

    PROC MEANSproc means data=scores noprint nway;   class id;   var game1 game2;   output out=new(drop=_type_ _freq_) sum=;run;proc print data=new noobs;run;

    PROC REPORTproc report data=scores nowd;   column id game1 game2;   define id / order ;   define game1 / analysis sum format=4.;   define game2 / analysis sum format=4.;   title 'proc report';run;

     

    3.COMPUTING AN AVERAGE FOR A BY GROUP

    DATA STEP

    data test;input I J X;cards;1 1 1231 1 32451 2 231 2 5431 2 871 3 902 1 882 1 86;run;proc sort data=test;   by i j;run;data jsubtot(keep=i j freq avg);   set test;   by i j;   retain jsub freq;   if first.j then  do;      jsub=0;      freq=0;   end;   jsub+x;   freq+1;   if last.j then do;      avg=jsub/freq;      output;   end;run;proc print data=jsubtot noobs;run;

    PROC MEANSproc means data=test noprint nway;   class i j;   var x;   output out=jsubtot2(drop=_type_ rename=(_freq_=freq)) mean=avg;run;proc print data=jsubtot2 noobs;run;   

    4.COMPUTING A PERCENTAGE FOR A BY GROUP

    DATA STEP

    data sales;   input @1 Region $char8. @10 Repid 4. @15 Amount 10. ;   format Amount dollar12.;   cards;NORTH    1001 1000000NORTH    1002 1100000NORTH    1003 1550000NORTH    1008 1250000NORTH    1005 900000SOUTH    1007 2105000SOUTH    1010 875000SOUTH    1012 1655000EAST     1051 2508000EAST     1055 1805000;run;proc sort data=sales;   by region;run;proc means data=sales noprint nway;   by region;   var amount;   output out=regtot(keep=regtotal region) sum=regtotal;run;data percent1;   merge sales regtot;   by region;   regpct=(amount/regtotal)*100;   format regpct 6.2 amount regtotal dollar10.;run;proc print data=percent1 noobs;run;

    proc tabulate

    proc tabulate data=sales out=percent2;   class region repid;   var amount;   table region*repid,amount*(sum*f=dollar10. pctsum<repid>);run;

    proc report proc report data=sales nowd out=percent3;   column region repid amount regpct;   define region / order;   define repid / display;   define amount / sum format=dollar10.;   define regpct / computed format=percent8.2;   compute before region;      hold=amount.sum;   endcomp;   compute regpct;      regpct=amount.sum/hold;   endcomp;run;

     

    5.CREATING A CUSTOM REPORT

    DATA STEP

    proc sort data=sashelp.class out=sorted;   by sex;run;data _null_;   set sorted;   by sex;   file print header=h notitles;   if first.sex thne put _page_;   put name 1-8 age 13-15 @20 sex +5 weight 5.1 +5 height;    return;      H:      put @35 "Page " n" of 2" //;   put @1 "Name" @13 "Age" @20 "Sex" @26 "Weight" @ 36 "Height";   put @1 "----" @13 "---" @20 "---" @26 "------" @ 36 "------";   return;run;

    PROC REPORTproc report data=sorted nowd;   column sex name age sex=sex1 weight height;   define sex / order noprint;   define name / order 'Name/--';   define age / order 'Age/--' width=4;   define sex1 / order 'Sex/--' width=3;   define weight / display 'Weight/--' width=7;   define height / display 'Height/--' width=7;   break after sex / page;run;

    还有很多类似的情况...........................................................................


    最新回复(0)