5
*chapter 11; *Define and execute a macro.; *1; proc print data =sasuser.all; var course_number student_name student_company; run ; %macro printnum ; proc print data=sasuser.all label noobs n; where course_number=# var student_name student_company; title "Enrollment for Course &num" ; run; %mend ; *2.; options mcompilenote =all; *3.; %let num=6; % printnum *Define and execute a macro, and use debugging options.; *1; %macro printnum ; /*macro definition*/ proc print data=sasuser.all label noobs n; where course_number=# % *subsetting the data using where clause; var student_name student_company; /*displaying two columns only*/ title "Enrollment for Course &num" ; run; %mend ; options symbolgen mprint mlogic ; %let num=8; % printnum *Define and execute a macro that includes positional parameters. *1; %macro attend1(opts,start,stop); %let start= %upcase (&start); %let stop= %upcase (&stop); proc freq data=sasuser.all; where begin_date between "&start"d and "&stop"d ; table location / &opts; title1 "Enrollment from &start to &stop" ; run; %mend ; *2;

Chapter 11 Final

Embed Size (px)

DESCRIPTION

SAS material

Citation preview

*chapter 11;*Define and execute a macro.;*1;

proc print data=sasuser.all;var course_number student_name student_company;run;%macro printnum;proc print data=sasuser.all label noobs n; where course_number=# var student_name student_company; title "Enrollment for Course &num";run;%mend;*2.;options mcompilenote=all;

*3.;%let num=6;%printnum

*Define and execute a macro, and use debugging options.;*1;%macro printnum;/*macro definition*/ proc print data=sasuser.all label noobs n; where course_number=# %*subsetting the data using where clause; var student_name student_company; /*displaying two columns only*/ title "Enrollment for Course &num"; run;%mend;options symbolgen mprint mlogic;%let num=8;%printnum*Define and execute a macro that includes positional parameters.*1;%macro attend1(opts,start,stop);%let start=%upcase(&start);%let stop=%upcase(&stop);proc freq data=sasuser.all; where begin_date between "&start"d and "&stop"d; table location / &opts; title1 "Enrollment from &start to &stop";run;%mend;*2;options mlogic mprint symbolgen;%attend1(nocum,'01jan2001'd,'31dec2001'd)

*3;%attend1(,'01jan2001'd,'31dec2001'd)*Define and execute a macro that includes keyword parameters.;%macro attend2(opts,start='01jan2001'd,stop='31dec2001'd);%let start=%upcase(&start);%let stop=%upcase(&stop);proc freq data=sasuser.all; where begin_date between "&start"d and "&stop"d; table location / &opts; title1 "Enrollment from &start to &stop";run;%mend;%attend2()%attend2(opts=nocum nopercent,stop='30jun2001'd)*Define and execute a macro that includes a mixed parameter list.;*1.;

%macro attend2(opts,start='01jan2001'd,stop='31dec2001'd);%let start=%upcase(&start);%let stop=%upcase(&stop);proc freq data=sasuser.all; where begin_date between "&start"d and "&stop"d; table location / &opts; title1 "Enrollment from &start to &stop";run;%mend;*2;%attend2(nocum)options mprint mlogic;*3;%attend2(,start='01oct2001'd)*Call one macro from another in order to create multiple local symbol tables.;*1;%macro prtrost(num=1); data _null_; call symput('today', (put(today(),mmddyy10.))); run;

proc print data=sasuser.all label noobs n; where course_number=# var student_name student_company city_state; title1 "Course &num Enrollment as of &today"; run;%mend prtrost;*2;%macro datemvar(frmt=date9.) data _null_; call symput('today', (put(today(),&frmt))); run; %mend datemvar;*3;%macro prtrost(num=1); %local today; %datemvar(frmt=mmddyy10.) proc print data=sasuser.all label noobs n; where course_number=# var student_name student_company city_state; title1 "Course &num Enrollment as of &today"; run;%mend prtrost;*4;options mprintnest mlogicnest;*Define and execute a macro in order to conditionally process SAS language steps.;title;footnote;proc print data=sasuser.all;run;data current(drop=diff); set sasuser.all; if year(begin_date)=2001; diff=year(today())-year(begin_date); begin_date=begin_date+(365*diff);run;proc print data=current;run;*2;%macro reports;proc sort data=work.current out=thisweek; where put(begin_date,monyy7.)= "%substr(&sysdate9,3,7)" and begin_date ge "&sysdate9"d; by begin_date location course_title;run;proc print data=thisweek noobs n; by begin_date location course_title; var student_name student_company paid; title "Course Registration as of &sysdate";run;%mend reports;

%reports*3;%macro reports1;proc sort data=work.current out=thisweek; where put(begin_date,monyy7.)= "%substr(&sysdate9,3,7)" and begin_date le "&sysdate9"d; by begin_date location course_title;run;proc means data=thisweek maxdec=0 sum; by begin_date location course_title; var fee; class paid; title "Revenue for Courses as of &sysdate9";run;%mend reports1;%reports1*4;options mlogic mlogicnest mprint;%reports1*Use a macro loop to generate data-dependent SAS code that is based on information in the Sashelp.Vstabvw data view.;*1;proc print data=sashelp.vstabvw(obs=5);run;%macro printlib(lib=SASUSER,obs=5); %let lib=%upcase(&lib); data _null_; set sashelp.vstabvw end=final; where libname="&lib"; call symput('dsname'||left(_n_),trim(memname)); if final then call symput('totaldsn',_n_); run;%mend printlib;%printlib()*2;options mprint mlogic symbolgen;%macro printlib(lib=SASUSER,obs=5); %let lib=%upcase(&lib); data _null_; set sashelp.vstabvw end=final; where libname="&lib"; call symput('dsname'||left(_n_),trim(memname)); if final then call symput('totaldsn',_n_); run; *%put totaldsn; %let f=input(&totaldsn,1.); %put &f; %local i; %do i=1 %to &f; proc print data=&lib..&&dsname&i (obs=&obs); title "Listing of &lib..&&dsname&i Data Set"; run; %end;%mend printlib;%printlib()*Use the %EVAL function and the %SYSEVALF function.;*1;%let sal1=25000;%let sal2=27050.45;%let saldiff=%sysevalf(&sal2-&sal1);%put The salary difference is &saldiff;

*2;