Download presentation
Presentation is loading. Please wait.
Published byShavonne Bradley Modified over 6 years ago
1
Lesson 13 More SGPLOT examples MAP Plotting Questions
2
* 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/’; set t.tomhs; If you want to connect the points with a step-function then use the STEP statement. We saw this in the example of generating a life-table curve.
3
Note: Second question only answered if first question is answered yes.
* 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 Two question were asked in TOMHS related to cigarette smoking; the first was “Have you ever smoked cigarettes” and if “yes” then “Do you currently smoke cigarettes” We want a variable to indicate current smoking status that includes the never smokers. We can get that with the if-then-else statement shown here. If the variable EVERSMK equals 2 (never smoker) than set the (new) variable CURRSMK to 2, otherwise set CURRSMK to the variable NOWSMK. See if you can follow the logic for the different possible answers to the two questions. We now have our outcome variable defined: variable CURRSMK, = to 1 for current smokers and = 2 for current non-smokers. Note: Second question only answered if first question is answered yes.
4
SCATTER vs SERIES vs REG vs STEP
Obs trt month cd4 Want to plot mean CD4 levels over time for each of two groups. Here is the data we will use to illustrate the various line-plots you can generate with PROC SGPLOT. The data is mean CD4 counts by treatment group and months after starting medication for HIV.
5
* Scatter Plot for 2 groups;
proc sgplot; xaxis label = 'Months After Start of Therapy' values=(0 to 36 by 6); yaxis label = 'Mean CD4 Level'; title 'Mean CD4 After Start of Therapy by Treatment Type'; scatter x=month y=cd4/group=trt ; format trt trtF.; label trt = 'Treatment Group'; run; The first line plot you can generate is a scatter plot using the SCATTER statement. You simple use the keyword SCATTER followed by the X and Y variables to be plotted. We then add the GROUP option which tells SAS to display the points separately by treatment group. We use the x-axis and y-axis statements to provide labels and values for the axis. Lastly, we use a format and label statement for the treatment group which will be used by the legend.
6
series x=month y=cd4/group=trt markers ; run;
* Series plot connects the points, marker option needed to plot symbols at each visit; proc sgplot; series x=month y=cd4/group=trt markers ; run; If you want to connect the points then use the SERIES statement rather than the SCATTER statement. The MARKERS option tells SAS to “mark” the points with a symbol.
7
* Regression plot does scatter plot and adds regression line;
proc sgplot; reg x=month y=cd4/group=trt ; run; If you want to display the best fitted regression line to each set of points use the REG statement. This will produce a scatter plot with a fitted line for each group.
8
* Step plot connects points with a step function; proc sgplot;
step x=month y=cd4/group=trt ; run; If you want to connect the points with a step-function then use the STEP statement. We saw this in the example of generating a life-table curve.
10
SAS MAPS: Choropleth Map
11
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).
12
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 data associated with sub-regions of plot.
13
This is dataset with margin of victory (1-6) for each county.
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. Values 1-6 Needs to be on each dataset SAS supplied dataset that draws map
14
Has data to draw all county lines in US. State=27 is MN
data mn_county; set maps.county; where state = 27; run; proc print data=mn_county (obs=10); Obs STATE SEGMENT COUNTY X Y
15
proc gproject data=mn_county out=mn_county; id county; run;
Obs X Y STATE SEGMENT COUNTY
16
Linking Data with Map Obs county county_name wincat AITKIN ANOKA BECKER BELTRAMI BENTON BIG STONE BLUE EARTH BROWN CARLTON CARVER CASS CHIPPEWA CHISAGO CLAY CLEARWATER COOK COTTONWOOD CROW WING DAKOTA DODGE Coded 1-6 dependent on level of difference between Romney and Obama
17
This is dataset with margin of victory (1-6) for each county.
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
18
Obama versus Romney: 2012
19
Trump versus Clinton: 2016 19
20
SAS Program to create map
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; 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; SAS Program to create map
21
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; proc gproject data=mn_county out=mn_county; id county; filename gsf “election2012.png"; proc gmap map=mn_county data=county all ; choro wincat/ discrete ; format wincat wincat.; label wincat = 'Margin' ; quit;
23
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 I hoped you have enjoyed the class and use some of what you learned in your research or other areas. Before I go let me give you some suggestions of where to find SAS help. First of all, there are SAS manuals. These can be costly and there are many of them. You may want to invest in just some of them, perhaps like SAS procedures. More recently SAS has made available online and in the help within PC SAS essentially every manual. The online help is entirely free. The web site for the current version of SAS is listed here. This is where I go for SAS help the most. Lastly, UCLA has a web site that has several free tutorials you can try. SAS institute and some independent groups give classes periodically on several topics. One of the regional headquarters is in downtown Minneapolis. You can find the schedule of classes on the SAS support web site. Lastly, please provide feedback on what you thought of this class and how it may be improved. Have a great rest of the Summer.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.