lesson 13 another macro example map plotting. macro example goal of macro named summary: for a given...

22
Lesson 13 Another MACRO Example MAP Plotting

Upload: erin-mckenzie

Post on 23-Dec-2015

245 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

Lesson 13

Another MACRO Example

MAP Plotting

Page 2: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

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.

Page 3: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

%macro summary ( dataset=, mvar=_numeric_,fvar = _character_,print=N,pvar=_all_);

Parameters to Macro = defaultsName of macro

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

Page 4: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

%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;proc print data=&dataset; var &pvar;%end;%mend summary;

Parameters to MacroName of macro

This will generate the proc print code only if the macro variable print equals Y.

Page 5: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

CALL TO MACRO:

libname here '~/PH6420/data/';

data state; set here.pop;run;

option mprint;%summary (dataset=state);

Code Generated:

MPRINT(SUMMARY): proc contents data=state varnum;MPRINT(SUMMARY): run;MPRINT(SUMMARY): proc means data=state;MPRINT(SUMMARY): var _numeric_;MPRINT(SUMMARY): run;MPRINT(SUMMARY): proc freq data=state;MPRINT(SUMMARY): tables _character_;MPRINT(SUMMARY): run;

* 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;

Page 6: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

CALL TO MACRO:

libname here '~/PH6420/data/';

data state; set here.pop;run;

option mprint;%summary (dataset=state, print=Y);

Code Generated:

MPRINT(SUMMARY): proc contents data=state varnum;MPRINT(SUMMARY): run;MPRINT(SUMMARY): proc means data=state;MPRINT(SUMMARY): var _numeric_;MPRINT(SUMMARY): run;MPRINT(SUMMARY): proc freq data=state;MPRINT(SUMMARY): tables _character_;MPRINT(SUMMARY): run;MPRINT(SUMMARY): proc print data=state;MPRINT(SUMMARY): var _all_;MPRINT(SUMMARY): run;

* 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;

Page 7: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

CALL TO MACRO:

libname here '~/PH6420/data/';

data state; set here.pop;run;

option mprint;%summary (dataset=state,fvar=state

statename);

Code Generated:

MPRINT(SUMMARY): proc contents data=state varnum;MPRINT(SUMMARY): run;MPRINT(SUMMARY): proc means data=state;MPRINT(SUMMARY): var _numeric_;MPRINT(SUMMARY): run;MPRINT(SUMMARY): proc freq data=state;MPRINT(SUMMARY): tables state statename;MPRINT(SUMMARY): run;

* 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;

Page 8: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

SAS MAPS: Choropleth Map

Page 9: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

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 sub-division (e.g. population, ethnicity, election results).

Page 10: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

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.

Page 11: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

pattern1 c=CXAAAAFF ;pattern2 c=CX6F6FFF ;pattern3 c=CX3333FF ;pattern4 c=CXEEA6A6 ;pattern5 c=CXE26262 ;pattern6 c=CXCD2626 ;

* Choro is a choropleth map;proc gmap map=mn_county data=county all; id county; choro wincat/ discrete ; format wincat wincat.; label wincat = 'Margin' ;run;

Sets colors for 6 margin of victory levels. 1-3 are shades of blue, 4-6 are shades of red.

This is dataset with margin of victory (1-6) for each county.

Needs to be on each dataset

SAS supplied dataset that draws map

Page 12: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

data mn_county; set maps.county; where state = 27;run;proc print data=mn_county (obs=10);run;

Obs STATE SEGMENT COUNTY X Y

1 27 1 1 1.63672 0.81313 2 27 1 1 1.63669 0.81686 3 27 1 1 1.63668 0.82083 4 27 1 1 1.62412 0.82076 5 27 1 1 1.62422 0.81623 6 27 1 1 1.62411 0.81016 7 27 1 1 1.62409 0.80561 8 27 1 1 1.63068 0.80554 9 27 1 1 1.63066 0.80714 10 27 1 1 1.63705 0.80709

Has data to draw all county lines in US. State=27 is MN

Page 13: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

proc gproject data=mn_county out=mn_county; id county;run;

Obs X Y STATE SEGMENT COUNTY

1 -.004946566 0.002584 27 1 1 2 -.004906222 0.006315 27 1 1 3 -.004879097 0.010286 27 1 1 4 0.003679749 0.010210 27 1 1 5 0.003628907 0.005679 27 1 1 6 0.003728457 -0.000393 27 1 1 7 0.003760163 -0.004944 27 1 1 8 -.000803074 -0.005021 27 1 1 9 -.000787884 -0.003421 27 1 1 10 -.005206215 -0.003457 27 1 1

Page 14: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

proc gmap map=mn_county data=mn_county; id county; choro county/nolegend discrete;run;

Page 15: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

proc gmap map=mn_county data=mn_county; id county; choro county/nolegend levels=1;run;

Page 16: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

Linking Data with CountyObs county county_name wincat

1 1 AITKIN 4 2 3 ANOKA 4 3 5 BECKER 5 4 7 BELTRAMI 2 5 9 BENTON 5 6 11 BIG STONE 4 7 13 BLUE EARTH 2 8 15 BROWN 6 9 17 CARLTON 3 10 19 CARVER 6 11 21 CASS 5 12 23 CHIPPEWA 1 13 25 CHISAGO 5 14 27 CLAY 2 15 29 CLEARWATER 5 16 31 COOK 3 17 33 COTTONWOOD 6 18 35 CROW WING 5 19 37 DAKOTA 1 20 39 DODGE 5

Coded 1-6 dependent on level of difference between Romney and Obama

Page 17: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

pattern1 c=CXAAAAFF ;pattern2 c=CX6F6FFF ;pattern3 c=CX3333FF ;pattern4 c=CXEEA6A6 ;pattern5 c=CXE26262 ;pattern6 c=CXCD2626 ;

proc gmap map=mn_county data=county all; id county; choro wincat/ discrete ; format wincat wincat.; label wincat = 'Margin' ;run;

Sets colors for 6 margin of victory levels. 1-3 are shades of blue, 4-6 are shades of red.

This is dataset with margin of victory (1-6) for each county.

Needs to be on each dataset

Page 18: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

SAS MAPS

Page 19: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

* 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;

Page 20: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,
Page 21: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,
Page 22: Lesson 13 Another MACRO Example MAP Plotting. Macro Example Goal of Macro named Summary: For a given dataset give summary statistics using PROC CONTENTS,

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