How can have a title of a graph with multiple lines? I would like to have to title in the first line and then a paragraph underneath that title to explain the graph.
My attempt is:
proc sgplot data= maindata.small_medium_big_firms;
title "Number of big, medium and small firms"
title1 " this is to explain the graph .........";
series x=year y=group_1/lineattrs=(color=red) legendlabel= "small";
series x=year y=group_2/lineattrs=(color=blue) legendlabel= "medium";
series x=year y=group_3/lineattrs=(color=black) legendlabel= "big";
YAXIS LABEL = 'Number of firms';
XAXIS LABEL = 'Year';
run;
Title and Title1 are the same command. By design, if you submit a new TITLE statement it overwrites any other TITLE statements of the same number and higher numbers.
http://support.sas.com/documentation/cdl/en/grstatproc/69716/HTML/default/viewer.htm#n1ukd9sqgqiwwhn1mrx4c1rbse1j.htm
This uses SASHELP data set to run, so anyone with SAS should be able to run the code correctly.
proc sgplot data= sashelp.stocks;
title1 "My Title - Title1" ;
title2 "Other Text - title2";
where stock='IBM';
series x=date y=open/lineattrs=(color=red) legendlabel= "Open";
series x=date y=close/lineattrs=(color=blue) legendlabel= "Close";
series x=date y=high/lineattrs=(color=black) legendlabel= "High";
YAXIS LABEL = 'Stock Price';
XAXIS LABEL = 'Date';
run;
Related
proc sgplot data=WORK.CUSTOMERDATA;
title height=14pt "Bar Chart of Gender";
vbar Gender / fillattrs= (color=CX024ae6) datalabel;
yaxis grid;
run;
ods graphics / reset;
title;
how do I change the colour of individual bars ? I have tried fill= and styleattrs datacolors= but it doesn't seem to work..
Should you wish to have one color for the graduated and another for the ones that did not, the following should provide the desired output
proc sgplot data=customerdata;
title height=14pt "Bar Chart of Graduated";
styleattrs datacolors=(blue red) ;
vbar Graduated / group=Graduated filltype=solid datalabel;
yaxis grid;
run;
I want to create a line graph that includes the overall trend of a disease rate and the specific trends for males and females. I use the following code for to create the group by trends. How to add he average trend to this line graph. Thanks for your help.
proc sgplot data=have ;
vline year/response=disease_rate group=sex stat=mean datalabel=disease_rate ;
yaxis values=(0,1) label="Percentage";
run;
Here's an example of summarizing it and then displaying it on the graph. There are more than one way to do this though, this is just one.
data have;
set sashelp.heart(in=a);
year=round(2021-ageAtStart, 10);
disease_rate= status="Dead";
run;
proc means data=have mean noprint;
class sex year;
types sex sex*year;
var disease_rate;
output out=summary_stats mean=average_value;
run;
proc sort data=summary_stats;
by sex year;
run;
data graph_data;
merge summary_stats(where=(_type_=2) rename=average_value=mean_sex_year)
summary_stats(where=(_type_=3) rename=average_value = mean_sex);
by sex;
format mean_sex: percent12.1;
run;
proc sgplot data=graph_data ;
*where year > 1990;
vline year/response=mean_sex_year group=sex stat=mean datalabel=mean_sex_year ;
vline year/response=mean_sex group=sex stat=mean datalabel=mean_sex ;
run;
Use series instead of vline so that you can overlay a regression on top of it to get an average trend line. For example:
proc sql;
create table have as
select date
, region
, sum(sale) as sale
from sashelp.pricedata
group by region, date
order by region, date
;
quit;
proc sgplot data=have;
series x=date y=sale / group=region;
reg x=date y=sale / group=region;
xaxis fitpolicy=rotatethin;
run;
I am using ODS to output an rtf file with graphs for each warehouse, using the warehouse ID as the by value. I need to organize the graphs in ascending order of rejected products per 100 produced. I was reordering the graphs manually in Word, but now I am being asked to produce this for the top 20 warehouses instead of the top 5 and would prefer not to do this manually. I've created a variable (norder) to indicate the ranking of each warehouse, but if I sort by this or try to use it as a by value, it messes up the graphs. I have also tried the NOTSORTED option, but this did not work. Is there an easy way to do this? Below is my code:
proc transpose data=WIDE_war
out=Long_war(rename=(Col1=Value))
name=source;
by norder ID name county zipcode date;
var defect1 defect2;
run;
proc sort data=long_war; by ID name zipcode county; run;
OPTIONS orientation=landscape nodate;
ods rtf file="C:\Users\....Top_10_Warehouse.rtf" STYLE=Styles.rtf bodytitle STARTPAGE=NO;;
ods listing close;
ods noproctitle ;
ODS ESCAPECHAR='^';
title; footnote;
title;
ods graphics on / height=7in width=9in;
ods graphics/noborder;
/*Table*/
/*List name, ID, number of products produced, number of defects, the rate of defects per 100 units produced */
/*sort by descending order of the last number */
ods rtf text= "^{style[fontweight=bold just=c fontsize=14pt]Warehouses with the greatest number of defects per 100 units produced}";
proc sort data=req4; by descending rejrat2; run;
proc print data=req4 noobs label;
var ID name ZIPcode County;
var mean_prod newdefects rejrat2 /style(data)={ width=1in};
run;
* time trend plots;
ods rtf startpage=NOW;
ods rtf text= "^{style[fontweight=bold just=c fontsize=14pt] Trend plot of the warehouses listed in the above table since June 01, 2020}";
proc sgplot data=long_war;
by ID name county ;
title1 "ID= #byval(ID) ";
title2 "Name: #byval(name) ";
title3 "County: #byval(county) ";
vbar date/ response=Value group=source groupdisplay=stack grouporder=data NOOUTLINE;
xaxis type=linear thresholdmin=0 label="Date"
values=('01jun20'd to '13dec20'd by 7)
labelattrs=(size=12pt weight=bold)
valueattrs=(size=13pt);
yaxis min=0 label="Count" INTEGER grid
labelattrs=(size=12pt weight=bold)
values=(0 to 35 by 5)
valueattrs=(size=13pt) fitpolicy=thin offsetmin=0 ;
label Source = "Defects by type"
Value = "Count";
options NOBYLINE;
run;
ods rtf close;
ods listing ;
I need to create 3 graphs for each facility and output these onto 1 page. I have 600 facilities to do this for so I will have a 600 page document. I have created my graphs using the code below. If I specify a "where ID=X" in the proc sgplot statement, it outputs everything fine, but only for facility X. If I don't, it prints Graph 1 for every facility before going to the next graph. I'm guessing I need a macro... does anyone have any advice?
OPTIONS orientation=vertical nodate;
ods rtf file="C:\Users\filename.rtf" STYLE=Styles.rtf;
ods listing close;
ods noproctitle ;
ODS ESCAPECHAR='^';
title ; footnote;
*First graph;
ods graphics on / height=2.7 in width=8in;
ods rtf startpage=NOW;
ods rtf text= "^{style[fontweight=bold fontsize=11pt textalign=c] Employees}";
ods graphics/noborder;
proc sort data=clean4; by ID warehouse county; run;
proc sgplot data=clean4;
by pfi name;
title2 "ID= #byval(ID) ";
title3 "Name: #byval(warehouse) ";
title4 "County: #byval(county) ";
series x=date y=emp / markers markerattrs=(symbol=CircleFilled color=blue) lineattrs=(color=blue thickness=2 pattern=1 ) legendlabel='Number of Employees' dataskin=pressed;
yaxis label='Count' valueattrs=(size=11pt) labelattrs=(size=11pt weight=bold) offsetmin=0 integer;
xaxis label='Date' valueattrs=(size=11pt) labelattrs=(size=11pt weight=bold) ;
option NOBYLINE;
run;
*Second graph;
ods graphics on / height=2.7 in width=8in;
ods rtf startpage=NO;
ods rtf text=' ';
ods rtf text= "^{style[fontweight=bold fontsize=12pt textalign=c] Hats used daily}";
ods graphics/noborder;
proc sort data=clean4; by ID; run;
proc sgplot data=clean4;
by ID;
title2; title3;
series x=date y=hats / markers markerattrs=(symbol=CircleFilled color=red)
lineattrs=(color=red thickness=2 pattern=1 ) legendlabel='Number of hats used' dataskin=pressed;
yaxis label='Count' valueattrs=(size=11pt) labelattrs=(size=11pt weight=bold) fitpolicy=thin
offsetmin=0 integer;
xaxis label='Date' valueattrs=(size=11pt) labelattrs=(size=11pt weight=bold) ;
run;
*Third graph;
ods graphics on / height=2.7 in width=8in;
ods rtf startpage=NO;
ods rtf text=' ';
ods rtf text= "^{style[fontweight=bold fontsize=11pt textalign=c] LOESS}";
ods graphics/noborder;
proc sort data=clean4; by ID; run;
proc sgplot data=clean4;
by ID;
loess y=var1 x=date/ legendlabel="LOESS" lineattrs=(color=blue)
FILLEDOUTLINEDMARKERS MARKERFILLATTRS=(color=black);
yaxis label='LOESS Plot' valueattrs=(size=11pt) labelattrs=(size=11pt weight=bold) offsetmin=0;
xaxis label='Date' valueattrs=(size=11pt) labelattrs=(size=11pt weight=bold) THRESHOLDMIN=0
THRESHOLDMAX=0 ;
option NOBYLINE;
run;
ods rtf close;
ods listing ;
Because you are using the same data set clean4 for producing output in three different ways for each ID you need to change only a minimal amount of code when you convert to macro (macroize) the existing code.
Two steps
macroize existing code to operate on a single 'ID' value (the do'er)
run the macro for each ID (the run'er)
Do'er
%macro doReport(ID=);
* move the sort to the top;
* only need to sort the data once (for your situation);
proc sort data=clean4 out=clean4_oneID;
by ID warehouse county;
where ID = "&ID";
run;
* Place all the graphing code here;
* change all the 'clean4' data set references to 'clean4_oneID';
%mend;
Run'er
* Place ODS RTF and settings here;
* obtain list of each id;
proc sort nodupkey data=clean4 out=id_list; by id; run;
* 'stackingly' invoke macro for each id;
data _null_;
set id_list;
call execute (cats('%nrstr(%doReport(ID=',id,'))');
run;
* stacked execute code will now be submitted by SAS supervisor;
* close the RTF here;
I want to plot Y by X plot where I group by year, but color code year based on different variable (dry). So each year shows as separate line but dry=1 years plot one color and dry=0 years plot different color. I actually figured one option (yeah!) which is below. But this doesn't give me much control.
Is there a way to put a where clause in the series statement to select specific categories so that I can specifically assign a color (or other format)? Or is there another way? This would be analogous to R where one can use multiple line statements for different subsets of data.
Thanks!!
This code works.
proc sgplot data = tmp;
where microsite_id = "&msit";
by microsite_id ;
yaxis label= "Pct. Stakes" values = (0 to 100 by 20);
xaxis label= 'Date' values = (121 to 288 by 15);
series y=tpctwett x=jday / markers markerattrs=(symbol=plus) group = year grouplc=dry groupmc=dry;
format jday tadjday metajday jdyfmt.;
label tpctwett='%surface water' tadval1='breed' metaval1='meta';
run;
Use an Attribute map, see the documentation
You can use the DRY variable to set the specific colours. For each year, assign the colour using the DRY variable in a data step.
proc sort data=tmp out=attr_data; by year; run;
data attrs;
set attr_data;
id='year';
if dry=0 then linecolor='green';
if dry=1 then linecolor='red';
keep id linecolor;
run;
Then add the dattrmap=attrs in the PROC SGPLOT statement and the attrid=year in the SGPLOT options.
ods graphics / attrpriority=none;
proc sgplot data = tmp dattrmap=attrs;
where microsite_id = "&msit";
by microsite_id ;
yaxis label= "Pct. Stakes" values = (0 to 100 by 20);
xaxis label= 'Date' values = (121 to 288 by 15);
series y=tpctwett x=jday / markers markerattrs=(symbol=plus) group = year grouplc=dry groupmc=dry attrid=year;
format jday tadjday metajday jdyfmt.;
label tpctwett='%surface water' tadval1='breed' metaval1='meta';
run;
Note that I tested and edited this post so it should work now.