Docsity

Analysis of One-Sample Confidence Intervals using SAS - Prof. A. John Bailer, Study notes of Statistics

Toggle sidebar
bg1
Week 10+ Class Activities
File: week-19-02nov07-MACRO.doc
Directory (hp):
C:\baileraj\Classes\Fall 2007\sta402\handouts
Directory: \\Muserver2\USERS\B\\baileraj\Classes\sta402\handouts
* run the programs/macros to see the plots
MACRO LANGUAGE
* Why use macros?
* Macro variables – system-defined and user-defined
* Macros
* Macro parameters
* Macro functions
* Conditional execution and DO loops
* CALL SYMPUT
References:
Burlew, M.M. (1998) SAS Macro Programming made easy.
SAS Institute Inc. Cary, NC.
Carpenter, A. (1998) Carpenter’s Complete Guide to the
SAS Macro Language. SAS Institute Inc. Cary, NC.
Other refs …
Look at SAS Trainer kits for macro
Acknowledgments:
Thanks to Jim Deddens (UC) for sharing handouts that served
as the starting place for a number of the illustrations
contained herein (Deddens Handouts #17-20)
Macro variables
* reference anywhere in SAS program other than data lines
* use either inside or outside macro programs
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35

Partial preview of the text

Download Analysis of One-Sample Confidence Intervals using SAS - Prof. A. John Bailer and more Study notes Statistics in PDF only on Docsity!

Week 10+ Class Activities

File: week-19-02nov07-MACRO.doc

Directory (hp): C:\baileraj\Classes\Fall 2007\sta402\handouts Directory: \Muserver2\USERS\B\baileraj\Classes\sta402\handouts

  • run the programs/macros to see the plots

MACRO LANGUAGE

  • Why use macros?

  • Macro variables – system-defined and user-defined

  • Macros

  • Macro parameters

  • Macro functions

  • Conditional execution and DO loops

  • CALL SYMPUT

References: Burlew, M.M. (1998) SAS Macro Programming made easy. SAS Institute Inc. Cary, NC.

Carpenter, A. (1998) Carpenter’s Complete Guide to the SAS Macro Language. SAS Institute Inc. Cary, NC.

Other refs …

Look at SAS Trainer kits for macro

Acknowledgments: Thanks to Jim Deddens (UC) for sharing handouts that served as the starting place for a number of the illustrations contained herein (Deddens Handouts #17-20)

Macro variables

  • reference anywhere in SAS program other than data lines

  • use either inside or outside macro programs

  • can by created by SAS (AUTOMATIC MACRO VARIABLES) or by program(USER-DEFINED MACRO VARIABLES)

  • stored in GLOBAL or LOCAL symbol table

  • macros are TEXT

  • macro variables are valid SAS names

  • double quotes (“ “) to resolve macro variable ref – single quotes will not (‘ ‘)

“options” for macro programming

  • symbolgen = show values of macro variables = trace resolution of indirect macro references

  • mprint = enable printing or processing messages in macros

  • mlogic = traces execution of macro programs (debugging)

Creating/Assigning/Displaying macro variable values

%LET macro-var-name = value ;

E.g. %LET nobs = 10;

Referencing macro variable in program

&macro-var-name

Text before macro variable reference

text&macro-var-name

Text after macro variable reference

&macro-var-name.text

Displaying the values of macro variables

%PUT &macro-var-name; %PUT automatic; %PUT user; %PUT all;

SYMBOLGEN: Macro variable NOBS resolves to 10 173 &varname = ranexp(&seed); SYMBOLGEN: Macro variable VARNAME resolves to Ran_var SYMBOLGEN: Macro variable SEED resolves to 0 174 output; 175 end; 176 drop ii; 177

NOTE: The data set WORK.RAN_EXP_10 has 10 observations and 1 variables. NOTE: DATA statement used: real time 0.02 seconds cpu time 0.02 seconds

178 proc print; SYMBOLGEN: Macro variable MYTITLE resolves to Random Exponential Data 179 title "&mytitle"; 180 run;

NOTE: There were 10 observations read from the data set WORK.RAN_EXP_10. NOTE: PROCEDURE PRINT used: real time 0.01 seconds cpu time 0.01 seconds

From OUTPUT file...

Random Exponential Data

Obs Ran_var

1 1. 2 0. 3 0. 4 0. 5 4. 6 1. 7 3. 8 2. 9 0. 10 0.

/* 3 simple MACRO motivating examples from M. Hughes */

/* EXAMPLE 1: Using LET to define macro variables */

options symbolgen;

data ds1; input y x @@; cards; 20 15 23 19 22 19 28 25 ;

%let dataname=ds1; %let response=y; %let datadescript=a completely PHONY data set;

proc reg data=&dataname; model &response=x; title "Regression analysis of &datadescript"; run;

/* EXAMPLE 2: A simple MACRO PROGRAM */

options symbolgen;

data ds2; input y1 x1 @@; cards; 20 11 23 19 22 13 32 25 ;

data ds3; input y2 x2 @@; cards; 21 12 29 16 22 15 22 25 ;

*definition of the macro program; %macro myregression(dataname=,response=,predictor=,datadescript=); proc reg data=&dataname; model &response=&predictor; title "Regression analysis of dataset &dataname: &datadescript"; run; %mend myregression;

*two invocations of macro code; %myregression(dataname=ds2,response=y1,predictor=x1,datadescript=a completely PHONY data set) %myregression(dataname=ds3,response=y2,predictor=x2,datadescript=yet ANOTHER completely phony data set)

quit;

--------------------------------------------------------------; *MORE EFFICIENT CODE;

/* EXAMPLE 3: Processing multiple datasets with macro looping */

options symbolgen;

data ds1; input y x @@; cards; 20 15 23 19 22 19 28 25 ; data ds2; input y x @@; cards;

proc sort ; by &&&var&n; proc print ; by &&&var&n; var nstudents; run ;

From SASLOG file...

231 %LET n=1; 232 %LET var = class; 233 %LET class1 = statistics_class; 234 options symbolgen; 235 236 data; 237 input statistics_class $ nstudents; 238 datalines;

NOTE: The data set WORK.DATA4 has 4 observations and 2 variables. NOTE: DATA statement used: real time 0.01 seconds cpu time 0.01 seconds

243 ; 244 proc sort; SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable VAR resolves to class SYMBOLGEN: Macro variable N resolves to 1 SYMBOLGEN: Macro variable CLASS1 resolves to statistics_class 244! by &&&var&n;

NOTE: There were 4 observations read from the data set WORK.DATA4. NOTE: The data set WORK.DATA4 has 4 observations and 2 variables. NOTE: PROCEDURE SORT used: real time 0.15 seconds cpu time 0.06 seconds

245 proc print; SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable VAR resolves to class SYMBOLGEN: Macro variable N resolves to 1 SYMBOLGEN: Macro variable CLASS1 resolves to statistics_class 245! by &&&var&n; 246 var nstudents; 247 run;

NOTE: There were 4 observations read from the data set WORK.DATA4. NOTE: PROCEDURE PRINT used: real time 0.04 seconds cpu time 0.04 seconds

From OUTPUT file...

statistics_class=regressi

Obs nstudents

1 14 2 21

statistics_class=sampling

Obs nstudents

3 5 4 4

Macro Programs

%macro prog-name; %mend prog-name;

  • execute by submitting reference to macro program

%prog-name

  • no semi-colon needed – NOT a SAS statement

Passing parameters to macro programs

  1. Positional

%macro prog-name(var-pos-1, var-pos-2,... , var-pos-k)

< text >

%mend prog-name;

  1. Keyword

%macro prog-name(var1=default-value1, var2=default-value3,... vark=default-valuek)

< text >

%mend prog-name;

data &out; set temp; lower_m=xbar-tinv(.975,num-1)sqrt(ssquare/num); upper_m=xbar+tinv(.975,num-1)sqrt(ssquare/num);

icover = (lower_m<&mu<upper_m);

%MEND one_sample_ci_sim;

/* so the macro is now defined... let’s try it out */

%one_sample_ci_sim(out=norm_sim) %one_sample_ci_sim(Distn=Exponential,out=exp_sim) %one_sample_ci_sim(Distn=Uniform,out=unif_sim)

options formdlim = "-";

proc means data=norm_sim mean; title “T-based CI for one-sample”; title2 “Normal populations -- # observations = &gnobs”; title3 “(based on &gnsims simulation conditions)”; var icover; run;

proc means data=exp_sim mean; title “T-based CI for one-sample”; title2 “Exponential populations -- # observations = &gnobs”; title3 “(based on &gnsims simulation conditions)”; var icover; run;

proc means data=unif_sim mean; title “T-based CI for one-sample”; title2 “Uniform populations -- # observations = &gnobs”; title3 “(based on &gnsims simulation conditions)”; var icover; run;

SAS OUTPUT


T-based CI for one-sample

Normal populations -- # observations = 15 (based on 4000 simulation conditions)

The MEANS Procedure

Analysis Variable : icover

Mean ƒƒƒƒƒƒƒƒƒƒƒƒ

ƒƒƒƒƒƒƒƒƒƒƒƒ


T-based CI for one-sample Exponential populations -- # observations = 15 (based on 4000 simulation conditions)

The MEANS Procedure

Analysis Variable : icover

Mean ƒƒƒƒƒƒƒƒƒƒƒƒ

ƒƒƒƒƒƒƒƒƒƒƒƒ


T-based CI for one-sample Uniform populations -- # observations = 15 (based on 4000 simulation conditions)

The MEANS Procedure

Analysis Variable : icover

Mean ƒƒƒƒƒƒƒƒƒƒƒƒ

ƒƒƒƒƒƒƒƒƒƒƒƒ

SASLOG

624 %one_sample_ci_sim(out=norm_sim) MLOGIC(ONE_SAMPLE_CI_SIM): Beginning execution. MLOGIC(ONE_SAMPLE_CI_SIM): Parameter OUT has value norm_sim

NOTE: There were 60000 observations read from the data set WORK.SIMULATE. NOTE: The data set WORK.TEMP has 4000 observations and 6 variables. NOTE: PROCEDURE MEANS used: real time 0.10 seconds cpu time 0.10 seconds

MPRINT(ONE_SAMPLE_CI_SIM): data norm_sim; MPRINT(ONE_SAMPLE_CI_SIM): set temp; MPRINT(ONE_SAMPLE_CI_SIM): lower_m=xbar-tinv(.975,num-1)sqrt(ssquare/num); MPRINT(ONE_SAMPLE_CI_SIM): upper_m=xbar+tinv(.975,num-1)sqrt(ssquare/num); SYMBOLGEN: Macro variable MU resolves to 0 MPRINT(ONE_SAMPLE_CI_SIM): icover = (lower_m<0<upper_m); MLOGIC(ONE_SAMPLE_CI_SIM): Ending execution. 625 %one_sample_ci_sim(Distn=Exponential,out=exp_sim) MLOGIC(ONE_SAMPLE_CI_SIM): Beginning execution. MLOGIC(ONE_SAMPLE_CI_SIM): Parameter DISTN has value Exponential MLOGIC(ONE_SAMPLE_CI_SIM): Parameter OUT has value exp_sim MLOGIC(ONE_SAMPLE_CI_SIM): Parameter NSIMS has value 4000 MLOGIC(ONE_SAMPLE_CI_SIM): Parameter NOBS has value 15 MLOGIC(ONE_SAMPLE_CI_SIM): Parameter SEED has value 0 MLOGIC(ONE_SAMPLE_CI_SIM): %GLOBAL GNOBS MLOGIC(ONE_SAMPLE_CI_SIM): %LET (variable name is GNOBS) SYMBOLGEN: Macro variable NOBS resolves to 15 MLOGIC(ONE_SAMPLE_CI_SIM): %GLOBAL GNSIMS MLOGIC(ONE_SAMPLE_CI_SIM): %LET (variable name is GNSIMS) SYMBOLGEN: Macro variable NSIMS resolves to 4000

NOTE: There were 4000 observations read from the data set WORK.TEMP. NOTE: The data set WORK.NORM_SIM has 4000 observations and 9 variables. NOTE: DATA statement used: real time 0.24 seconds cpu time 0.24 seconds

MPRINT(ONE_SAMPLE_CI_SIM): data simulate; SYMBOLGEN: Macro variable NSIMS resolves to 4000 MPRINT(ONE_SAMPLE_CI_SIM): do i=1 to 4000; SYMBOLGEN: Macro variable NOBS resolves to 15 MPRINT(ONE_SAMPLE_CI_SIM): do k=1 to 15; SYMBOLGEN: Macro variable DISTN resolves to Exponential MLOGIC(ONE_SAMPLE_CI_SIM): %IF condition &Distn=Normal is FALSE SYMBOLGEN: Macro variable DISTN resolves to Exponential MLOGIC(ONE_SAMPLE_CI_SIM): %IF condition &Distn=Exponential is TRUE SYMBOLGEN: Macro variable SEED resolves to 0

MPRINT(ONE_SAMPLE_CI_SIM): X = RANEXP(0);

MLOGIC(ONE_SAMPLE_CI_SIM): %LET (variable name is MU) MPRINT(ONE_SAMPLE_CI_SIM): output; MPRINT(ONE_SAMPLE_CI_SIM): end; MPRINT(ONE_SAMPLE_CI_SIM): end;

NOTE: The data set WORK.SIMULATE has 60000 observations and 3 variables. NOTE: DATA statement used: real time 0.68 seconds cpu time 0.10 seconds

MPRINT(ONE_SAMPLE_CI_SIM): proc sort data=simulate; MPRINT(ONE_SAMPLE_CI_SIM): by i;

NOTE: There were 60000 observations read from the data set WORK.SIMULATE. NOTE: The data set WORK.SIMULATE has 60000 observations and 3 variables. NOTE: PROCEDURE SORT used: real time 0.17 seconds cpu time 0.17 seconds

MPRINT(ONE_SAMPLE_CI_SIM): proc means data=simulate noprint; MPRINT(ONE_SAMPLE_CI_SIM): by i; MPRINT(ONE_SAMPLE_CI_SIM): var x; MPRINT(ONE_SAMPLE_CI_SIM): output out=temp mean=xbar var=ssquare n=num; SYMBOLGEN: Macro variable OUT resolves to exp_sim NOTE: There were 60000 observations read from the data set WORK.SIMULATE. NOTE: The data set WORK.TEMP has 4000 observations and 6 variables. NOTE: PROCEDURE MEANS used: real time 0.09 seconds cpu time 0.09 seconds

MPRINT(ONE_SAMPLE_CI_SIM): data exp_sim; MPRINT(ONE_SAMPLE_CI_SIM): set temp; MPRINT(ONE_SAMPLE_CI_SIM): lower_m=xbar-tinv(.975,num-1)sqrt(ssquare/num); MPRINT(ONE_SAMPLE_CI_SIM): upper_m=xbar+tinv(.975,num-1)sqrt(ssquare/num); SYMBOLGEN: Macro variable MU resolves to 1 MPRINT(ONE_SAMPLE_CI_SIM): icover = (lower_m<1<upper_m); MLOGIC(ONE_SAMPLE_CI_SIM): Ending execution. 626 %one_sample_ci_sim(Distn=Uniform,out=unif_sim) MLOGIC(ONE_SAMPLE_CI_SIM): Beginning execution. MLOGIC(ONE_SAMPLE_CI_SIM): Parameter DISTN has value Uniform MLOGIC(ONE_SAMPLE_CI_SIM): Parameter OUT has value unif_sim MLOGIC(ONE_SAMPLE_CI_SIM): Parameter NSIMS has value 4000

cpu time 0.17 seconds

MPRINT(ONE_SAMPLE_CI_SIM): proc means data=simulate noprint; MPRINT(ONE_SAMPLE_CI_SIM): by i; MPRINT(ONE_SAMPLE_CI_SIM): var x; MPRINT(ONE_SAMPLE_CI_SIM): output out=temp mean=xbar var=ssquare n=num; SYMBOLGEN: Macro variable OUT resolves to unif_sim NOTE: There were 60000 observations read from the data set WORK.SIMULATE. NOTE: The data set WORK.TEMP has 4000 observations and 6 variables. NOTE: PROCEDURE MEANS used: real time 0.09 seconds cpu time 0.09 seconds

MPRINT(ONE_SAMPLE_CI_SIM): data unif_sim; MPRINT(ONE_SAMPLE_CI_SIM): set temp; MPRINT(ONE_SAMPLE_CI_SIM): lower_m=xbar-tinv(.975,num-1)sqrt(ssquare/num); MPRINT(ONE_SAMPLE_CI_SIM): upper_m=xbar+tinv(.975,num-1)sqrt(ssquare/num); SYMBOLGEN: Macro variable MU resolves to 0. MPRINT(ONE_SAMPLE_CI_SIM): icover = (lower_m<0.5<upper_m); MLOGIC(ONE_SAMPLE_CI_SIM): Ending execution. 627 628

NOTE: There were 4000 observations read from the data set WORK.TEMP. NOTE: The data set WORK.UNIF_SIM has 4000 observations and 9 variables. NOTE: DATA statement used: real time 0.23 seconds cpu time 0.23 seconds

629 proc means data=norm_sim mean; 630 title "T-based CI for one-sample"; SYMBOLGEN: Macro variable GNOBS resolves to 15 631 title2 "Normal populations -- # observations = &gnobs"; SYMBOLGEN: Macro variable GNSIMS resolves to 4000 632 title3 "(based on &gnsims simulation conditions)"; 633 var icover; 634 run;

NOTE: There were 4000 observations read from the data set WORK.NORM_SIM. NOTE: PROCEDURE MEANS used: real time 0.01 seconds cpu time 0.01 seconds

636 proc means data=exp_sim mean; 637 title "T-based CI for one-sample"; SYMBOLGEN: Macro variable GNOBS resolves to 15 638 title2 "Exponential populations -- # observations = &gnobs"; SYMBOLGEN: Macro variable GNSIMS resolves to 4000 639 title3 "(based on &gnsims simulation conditions)"; 640 var icover; 641 run;

NOTE: There were 4000 observations read from the data set WORK.EXP_SIM. NOTE: PROCEDURE MEANS used: real time 0.01 seconds cpu time 0.01 seconds

642 643 proc means data=unif_sim mean; 644 title "T-based CI for one-sample"; SYMBOLGEN: Macro variable GNOBS resolves to 15 645 title2 "Uniform populations -- # observations = &gnobs"; SYMBOLGEN: Macro variable GNSIMS resolves to 4000 646 title3 "(based on &gnsims simulation conditions)"; 647 var icover; 648 run;

NOTE: There were 4000 observations read from the data set WORK.UNIF_SIM. NOTE: PROCEDURE MEANS used: real time 0.01 seconds cpu time 0.01 seconds

649 options formdlim = "-"; 650 651 proc means data=norm_sim mean; 652 title "T-based CI for one-sample"; SYMBOLGEN: Macro variable GNOBS resolves to 15 653 title2 "Normal populations -- # observations = &gnobs"; SYMBOLGEN: Macro variable GNSIMS resolves to 4000 654 title3 "(based on &gnsims simulation conditions)"; 655 var icover; 656 run;

NOTE: There were 4000 observations read from the data set WORK.NORM_SIM. NOTE: PROCEDURE MEANS used:

(e.g. %IF-%THEN... %DO... )

**Step 5: store macro for later use

Example – subsampling a data set using a Macro program

%macro sample_ds(dsn=,pct=,outtest=,outver=); /* samples a specified percentage (&pct) of a dataset (&dsn) storing the observations selected in &outtest and the observations not selected in &outver */

%global gpct; %let gpct = &pct; %global gdsn; %let gdsn = &dsn;

data temp maxi; set &dsn end=eof; unif_index = ranuni( 0 ); nobs + 1 ; output temp; if eof then do; maxnobs = nobs; output maxi; end;

data maxi; set maxi; do i = 1 to maxnobs; nobs = i; keep nobs maxnobs; output; end;

proc print data=maxi; title "DEBUG: maxnobs calculation check"; run;

data temp; merge temp maxi; by nobs;

proc print data=temp; title "DEBUG: checking merged data"; run;

proc sort data=temp; by unif_index; run;

data temp; set temp; cutpt = ceil(maxnobs*&pct/ 100 );

proc print;

title "DEBUG: checking cut point calculation"; run;

data &outtest &outver; set temp; retain nobs; put "nobs = " nobs; put "cutpt = " cutpt; if n <= cutpt then output &outtest; else output &outver; drop cutpt nobs;

%mend sample_ds;

  • -------------------- test sampling macro ------------------------------;

data tester; do i = 1 to 150 ; x = i; y = 3 + 4.5 *x + 10 *rannor( 0 ); output; end;

options symbolgen mprint mlogic; % sample_ds (dsn=tester,pct= 33 ,outtest=outt,outver=outv)

options nocenter nodate; proc print data=outt; title "Selected data containing &gpct of data set &gdsn"; run ;

proc print data=outv; title "NOT Selected data from data set &gdsn"; run ;

proc reg data=outt; title "Regression using the test data set"; model y = x; run ;

Example – adequacy plots for simple linear regression

%macro linreg_plots(dsn=&syslast, title=, resp_var=, pred_var=);

proc reg data=&dsn; title “&title”; model &resp_var = &pred_var; output out=mout p=yhat r=resid;

proc gplot data=mout;