230 likes | 245 Views
Lesson 13. More SGPLOT examples MAP Plotting Questions. * How to start a SAS program; * Starting with raw (text based) data ; data tomhs; infile ‘/folders/myfolders/tomhs.dat’; input statement with variables and formats; * Add new variables here; run; * Starting with SAS dataset;
E N D
Lesson 13 More SGPLOT examples MAP Plotting Questions
* How to start a SAS program; * Starting with raw (text based) data ; data tomhs; infile ‘/folders/myfolders/tomhs.dat’; input statement with variables and formats; * Add new variables here; run; * Starting with SAS dataset; libname t ‘/folders/myfolders/’; data tomhs; set t.tomhs; * Add new variables here; run;
* Coding for smoker (0/1); if eversmk = 2 then smoker = 0; else smoker = 2-nowsmk; Did you ever smoke cigarettes? 1 = yes, 2= no Do you now smoke cigarettes? 1 = yes, 2= no Var: eversmk Var: nowsmk Note: Second question only answered if first question is answered yes.
SCATTER vs SERIES vs REG vs STEP Obs trt month cd4 1 1 0 102 2 1 1 177 3 1 2 192 4 1 3 195 5 1 6 217 6 1 9 232 7 1 12 252 8 1 15 270 9 1 18 287 10 1 21 305 11 1 24 323 12 1 27 337 13 1 30 343 14 1 33 360 15 1 36 376 16 2 0 102 17 2 1 184 18 2 2 194 19 2 3 202 20 2 6 223 21 2 9 249 22 2 12 273 23 2 15 299 24 2 18 323 25 2 21 339 26 2 24 363 27 2 27 386 28 2 30 392 29 2 33 394 30 2 36 418 Want to plot mean CD4 levels over time for each of two groups.
* Scatter Plot for 2 groups; procsgplot; xaxislabel = 'Months After Start of Therapy'values=(0to36by6); yaxislabel = 'Mean CD4 Level'; title'Mean CD4 After Start of Therapy by Treatment Type'; scatterx=month y=cd4/group=trt ; format trt trtF.; label trt = 'Treatment Group'; run;
* Series plot connects the points, marker option needed to plot symbols at each visit; procsgplot; seriesx=month y=cd4/group=trt markers ; run;
* Regression plot does scatter plot and adds regression line; procsgplot; regx=month y=cd4/group=trt ; run;
* Step plot connects points with a step function; procsgplot; stepx=month y=cd4/group=trt ; run;
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, length of growing season, stroke rates).
SAS Tools • SAS has map datasets with latitude and longitude coordinates • PROCGPROJECT projects the map dataset so points will plot properly on 2D image. • PROCGMAP generates map with data associated with sub-regions of plot.
pattern1c=CXAAAAFF ; pattern2c=CX6F6FFF ; pattern3c=CX3333FF ; pattern4c=CXEEA6A6 ; pattern5c=CXE26262 ; pattern6c=CXCD2626 ; * Choro is a choropleth map; procgmapmap=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. Values 1-6 Needs to be on each dataset SAS supplied dataset that draws map
Has data to draw all county lines in US. State=27 is MN data mn_county; set maps.county; where state = 27; run; procprintdata=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
procgprojectdata=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
Linking Data with Map Obs 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
pattern1c=CXAAAAFF ; pattern2c=CX6F6FFF ; pattern3c=CX3333FF ; pattern4c=CXEEA6A6 ; pattern5c=CXE26262 ; pattern6c=CXCD2626 ; procgmapmap=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
libname e '~/Election/2012/'; data election; set e.president_county_2012; where candidate_id in('0301','0401'); county = county_id*2 - 1; drop state pctvote; run; proc sort; by county_id candidate_id; data county; set election; by county_id; retain RomneyVotes ObamaVotes ; if first.county_id then do; RomneyVotes = . ; ObamaVotes = . ; end; if candidate_id = '0301' then RomneyVotes = votes_candidate; if candidate_id = '0401' then ObamaVotes = votes_candidate; if last.county_id then output; drop votes_candidate candidate_id; run; data county; set county; RomneyPct = 100*RomneyVotes / totvotes; ObamaPct = 100*ObamaVotes / totvotes; votedifpct = RomneyPct - ObamaPct ; if votedifpct < -15 then wincat = 3; else if votedifpct < - 5 then wincat = 2; else if votedifpct < 0 then wincat = 1; else if votedifpct < 5 then wincat = 4; else if votedifpct < 15 then wincat = 5; else if votedifpct >=15 then wincat = 6; run; SAS Program to create map
proc format; value wincat 1 = 'Obama 0-5' 2='Obama 5-15' 3='Obama 15+' 4 = 'Romney 0-5' 5='Romney 5-15' 6='Romney 15+'; proc print; format wincat wincat.; run; goptions reset=all border device=png gsfname=gsf; pattern1 c=CXAAAAFF ; pattern2 c=CX6F6FFF ; pattern3 c=CX3333FF ; pattern4 c=CXEEA6A6 ; pattern6 c=CXCD2626 ; pattern5 c=CXE26262 ; data mn_county; set maps.county; where state = 27; run; proc gproject data=mn_county out=mn_county; id county; run; filename gsf “election2012.png"; proc gmap map=mn_county data=county all ; id county; choro wincat/ discrete ; format wincat wincat.; label wincat = 'Margin' ; run; quit;
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://it.umn.edu/list-courses (free online classes) • UCLA website www.ats.ucla.edu/stat/sas