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.
%macro summary (dataset=, mvar=_numeric_, fvar = _character_, print=N, pvar=_all_); Parameters to Macro = defaults Name 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
%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 Macro Name of macro This will generate the proc print code only if the macro variable print equals Y.
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;
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;
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;
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 sub- division (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 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
data mn_county; set maps.county; where state = 27; run; proc print data=mn_county (obs=10); run; Obs STATE SEGMENT COUNTY X Y Has data to draw all county lines in US. State=27 is MN
proc gproject data=mn_county out=mn_county; id county; run; Obs X Y STATE SEGMENT COUNTY
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 county county_name wincat 1 1 AITKIN ANOKA BECKER BELTRAMI BENTON BIG STONE BLUE EARTH BROWN CARLTON CARVER CASS CHIPPEWA CHISAGO CLAY CLEARWATER COOK COTTONWOOD CROW WING DAKOTA DODGE 5 Coded 1-6 dependent on level of difference between Romney and Obama
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
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: Take a class from SAS Take a class from OIT at U of M (free online classes) UCLA website