Lesson 13

advertisement
Lesson 13
Another MACRO Example
MAP Plotting
Macro Example
Goal of Macro named Summary:
For a given dataset give summary statistics using PROC
CONTENTS, MEANS and FREQ and (optionally) display the
data using PROC PRINT.
Instead of having to write the code each time, write a macro.
Name of
macro
Parameters to Macro = defaults
%macro summary (
dataset=,
mvar=_numeric_,
fvar = _character_,
print=N,
pvar=_all_);
Dataset: Name of dataset used
Mvar:
List of variables to run for PROC MEANS (default is all numeric var)
Fvar:
List of variables to run for PROC FREQ (default is all character var)
Print:
If set to Y then run PROC PRINT (default is N)
Pvar:
List of variables to run for PROC PRINT
Remember: SAS Macros generate SAS code when you call it
Name of
macro
Parameters to Macro
%macro summary ( dataset=,
mvar=_numeric_,
fvar = _character_,
print=N,
pvar=_all_);
proc contents data=&dataset varnum;
run;
proc means data=&dataset;
var &mvar;
run;
proc freq data=&dataset;
tables &fvar;
run;
%if &print = Y %then %do; This will generate the proc print code
proc print data=&dataset;
only if the macro variable print equals Y.
var &pvar;
%end;
%mend summary;
CALL TO MACRO:
libname
here '~/PH6420/data/';
data state;
set here.pop;
run;
option mprint;
%summary (dataset=state);
* This is the macro;
proc contents data=&dataset varnum;
run;
proc means data=&dataset;
var &mvar;
run;
proc freq data=&dataset;
tables &fvar;
run;
%if &print = Y %then %do;
proc print data=&dataset;
var &pvar;
%end;
Code Generated:
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
proc contents data=state varnum;
run;
proc means data=state;
var _numeric_;
run;
proc freq data=state;
tables _character_;
run;
CALL TO MACRO:
libname
here '~/PH6420/data/';
data state;
set here.pop;
run;
option mprint;
%summary (dataset=state, print=Y);
* This is the macro;
proc contents data=&dataset varnum;
run;
proc means data=&dataset;
var &mvar;
run;
proc freq data=&dataset;
tables &fvar;
run;
%if &print = Y %then %do;
proc print data=&dataset;
var &pvar;
%end;
Code Generated:
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
proc contents data=state varnum;
run;
proc means data=state;
var _numeric_;
run;
proc freq data=state;
tables _character_;
run;
proc print data=state;
var _all_;
run;
CALL TO MACRO:
libname
here '~/PH6420/data/';
data state;
set here.pop;
run;
option mprint;
%summary (dataset=state,fvar=state
statename);
* This is the macro;
proc contents data=&dataset varnum;
run;
proc means data=&dataset;
var &mvar;
run;
proc freq data=&dataset;
tables &fvar;
run;
%if &print = Y %then %do;
proc print data=&dataset;
var &pvar;
%end;
Code Generated:
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
MPRINT(SUMMARY):
proc contents data=state varnum;
run;
proc means data=state;
var _numeric_;
run;
proc freq data=state;
tables state statename;
run;
SAS MAPS: Choropleth Map
Map Design
• There is an overall region (like country or
state)
• There are sub-divisions of the region (like
counties within states or states within
country)
• Use color-coding to show data by subdivision (e.g. population, ethnicity, election
results).
SAS Tools
• SAS has map datasets with latitude and
longitude coordinates
• PROC GPROJECT projects the map
dataset so points will plot properly on 2D
image.
• PROC GMAP generates map with
(optional) data associated with sub-regions
of plot.
pattern1
pattern2
pattern3
pattern4
pattern5
pattern6
c=CXAAAAFF
c=CX6F6FFF
c=CX3333FF
c=CXEEA6A6
c=CXE26262
c=CXCD2626
;
;
;
;
;
;
Sets colors for 6 margin of
victory levels. 1-3 are shades of
blue, 4-6 are shades of red.
* Choro is a choropleth map;
proc gmap map=mn_county data=county all;
id county;
choro wincat/ discrete ;
format wincat wincat.;
label wincat = 'Margin' ;
This is dataset with
run;
margin of victory (1-6)
for each county.
Needs to be on each
dataset
SAS supplied dataset
that draws map
data mn_county;
Has data to draw all county
set maps.county;
lines in US. State=27 is MN
where state = 27;
run;
proc print data=mn_county (obs=10);
run;
Obs
1
2
3
4
5
6
7
8
9
10
STATE
27
27
27
27
27
27
27
27
27
27
SEGMENT
1
1
1
1
1
1
1
1
1
1
COUNTY
1
1
1
1
1
1
1
1
1
1
X
Y
1.63672
1.63669
1.63668
1.62412
1.62422
1.62411
1.62409
1.63068
1.63066
1.63705
0.81313
0.81686
0.82083
0.82076
0.81623
0.81016
0.80561
0.80554
0.80714
0.80709
proc gproject data=mn_county out=mn_county;
id county;
run;
Obs
1
2
3
4
5
6
7
8
9
10
X
-.004946566
-.004906222
-.004879097
0.003679749
0.003628907
0.003728457
0.003760163
-.000803074
-.000787884
-.005206215
Y
0.002584
0.006315
0.010286
0.010210
0.005679
-0.000393
-0.004944
-0.005021
-0.003421
-0.003457
STATE
27
27
27
27
27
27
27
27
27
27
SEGMENT
1
1
1
1
1
1
1
1
1
1
COUNTY
1
1
1
1
1
1
1
1
1
1
proc gmap map=mn_county
data=mn_county;
id county;
choro county/nolegend discrete;
run;
proc gmap map=mn_county
data=mn_county;
id county;
choro county/nolegend levels=1;
run;
Linking Data with County
Obs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
county
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
county_name
AITKIN
ANOKA
BECKER
BELTRAMI
BENTON
BIG STONE
BLUE EARTH
BROWN
CARLTON
CARVER
CASS
CHIPPEWA
CHISAGO
CLAY
CLEARWATER
COOK
COTTONWOOD
CROW WING
DAKOTA
DODGE
wincat
4
4
5
2
5
4
2
6
3
6
5
1
5
2
5
3
6
5
1
5
Coded 1-6
dependent on level
of difference
between Romney
and Obama
pattern1
pattern2
pattern3
pattern4
pattern5
pattern6
c=CXAAAAFF
c=CX6F6FFF
c=CX3333FF
c=CXEEA6A6
c=CXE26262
c=CXCD2626
;
;
;
;
;
;
Sets colors for 6 margin of
victory levels. 1-3 are shades of
blue, 4-6 are shades of red.
proc gmap map=mn_county data=county all;
id county;
choro wincat/ discrete ;
format wincat wincat.;
label wincat = 'Margin' ;
run;
This is dataset with
margin of victory (1-6)
for each county.
Needs to be on each
dataset
SAS MAPS
* Draw map of US;
data states;
set maps.states;
if state in(2,72,15) then delete;
run;
proc gproject data=states out=states;
id state;
run;
proc gmap map=states data=states;
id state;
choro state/ discrete nolegend ;
run;
Where to get help for SAS?
•
•
•
•
•
•
•
Google
Help within PC SAS
SAS Documentation on web:
http://support.sas.com/documentation/onlinedoc/bookshelf/93/
Take a class from SAS
Take a class from OIT at U of M
http://uttc.umn.edu/training/courses/description/?designator=SAS001
(free online classes)
• UCLA website www.ats.ucla.edu/stat/sas
Download