proc template color loess by group - sas

I am trying to set the colors for the lines generated by proc template by group.
PROC TEMPLATE;
Define statgraph LOESS_BAR;
begingraph;
Layout Lattice/ columns=1 columnweights=(1) ;
Layout overlay/ xaxisopts=(griddisplay=off Label = 'Days' Linearopts=viewmin=-90 viewmax=365 tickvaluesequence=(start=-390 end=390 increment=30)))
yaxisopts=(griddisplay=off label= 'Score' linearopts=(viewmin=0 viewmax=100));
loessplot x=days y=measure /group=groupnm index=rank smooth=0.5 name='LOESS';
ENDlayout;
EndLayout;
endgraph;
end;
run;
My data has the columns:
Days - days from the event (x-axis)
Measure - the data points being used for the LOESS curve (y-axis)
MarkerColor - the color I would like to use for the line
groupnm - distinguishes the groups to generate each line

Related

SAS: How to get a Pie Chart to display the MEAN of a specific column?

I have created a simple pie chart, but I want the pie chart to display the MEAN (average) of a specific column, which is the "PRICE" attribute from my dataset. My dataset has two attributes - PRODUCT and PRICE.
Below, shows my SAS code used to create the pie chart - but I need some help on how to get the pie chart to display the MEAN for my "PRICE" attribute.
Thanks - any help is much appreciated.
PROC TEMPLATE;
DEFINE STATGRAPH MyPieChart;
BEGINGRAPH;
ENTRYTITLE 'AVERAGE PRICE OF PRODUCTS';
LAYOUT REGION;
PIECHART CATEGORY=PRODUCTS RESPONSE=PRICE; // I THINK THE CODE NEEDS CHANGING HERE?
ENDLAYOUT;
ENDGRAPH;
END;
RUN;
PROC SGRENDER TEMPLATE=MyPieChart DATA=WORK.IMPORT;
RUN;
Add option / STAT=MEAN.
Lots more info in the Graphics Template Language (GTL) PIECHART statement options documentation.
Example:
data have; input
products $ price; datalines;
A 1
A 2
A 3
B 11
B 12
B 13
C 5
C 7
C 8
C 10
;
ods html file = 'pie.html';
PROC TEMPLATE;
DEFINE STATGRAPH MyPieChart;
BEGINGRAPH;
ENTRYTITLE 'AVERAGE PRICE OF PRODUCTS';
LAYOUT REGION;
PIECHART
CATEGORY=PRODUCTS
RESPONSE=PRICE
/
STAT=MEAN
;
ENDLAYOUT;
ENDGRAPH;
END;
RUN;
PROC SGRENDER TEMPLATE=MyPieChart DATA=WORK.HAVE;
RUN;
ods html close;

By group controlling line colors/where clause

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.

Add Title To SAS Output Graphs

I'm using template and sgrender in SAS to create heatmaps based on a different class variable. I'd like the output to update the title based off of the class variable each time to what the value of the class variable. So far, my code is like this (it prints a string title if I tell it to, but i can't get it to vary depending on the variable):
proc template;
define statgraph heatmapparm;
begingraph;
entrytitle 'INSERT TITLE HERE'; *Update title here based on classVar;
layout overlay;
heatmapparm x=magX2 y=magZ2 colorresponse=percent / colormodel=(blue yellow red)
name="heatmapparm" xbinaxis=false ybinaxis=false datatransparency=0;
continuouslegend "heatmapparm" / location=outside valign=bottom;
endlayout;
endgraph;
end;
run;
title #byval(classVar);
proc sgrender data=dataSet template=heatmapparm;
by classVar;
run;
Thank you all!
Use Macro variables to alter the titles. Here is an example
%let classVar=VARIABLEVALUE1;
title &classVar.;
proc sgrender data=dataSet template=heatmapparm;
by &classVar.;
run;
You can always write cleaner code by putting the proc sgrender code inside a SAS MACRO.

Color options in GPLOT in SAS

I have a temporal series with a variable in the horizontal axis that is the year. Once i have drawn it with gplot procedure I want to divide the graphic in years painting each year in different color. I have tried to do an if statemente inside gplot procedure when defining the color inside symbol options like this
symbol
if year=2006 then c=red;
(this is very simplified, it would depend on much more years and all this stuff)
but this desnt work.
EDITED:
Thanks everybody but I think i didint explain myself properly. I
have this code
PROC GPLOT DATA = work.Datosipppa
;
PLOT IPPPA * date /
OVERLAY
VAXIS=AXIS1
HAXIS=AXIS2
FRAME LEGEND=LEGEND1
href='01jun2006'd '01jun2007'd
;
PLOT2 tasaParoMensual * date = 2 /
OVERLAY
VAXIS=AXIS3
OVERLAY
LEGEND=LEGEND1
;
run;
quit;
and i want to colored each of the years in different colour.
I want to show you my graph but i cant if idont have 10 of reputation :(
IN FACT I WANT TO DO SOMETHNG EQUAL TO THIS EXAMPLE
http://support.sas.com/documentation/cdl/en/graphref/63022/HTML/default/viewer.htm#a003259878.htm
BUT INSTEAD OF IN THIS PROCEDURE IN GPLOT
One straightforward approach is to create a list colors in the GOPTIONS statement, like this:
goptions reset=all colors=(red yellow green blue purple black);
symbol value=dot;
proc gplot data=sashelp.cars;
plot horsepower * enginesize = type;
run;
quit;
You will need to review the output carefully that the years match the colors you want.
Another way is to specify separate symbol statements for each group you are plotting. Try this example below that is a stripped down version of your code. You will need to create a YEAR variable and include that in the PLOT statement so each year will be assigned to a different symbol statement / color.
goptions reset=all;
*** GENERATE TEST DATA ***;
data have;
do date = '01Jun2005'd to '01aug2007'd;
ipppa = ranuni(123456);
tasaParoMensual = 10 + rannor(123456) ;
year = year(date);
output;
end;
run;
*** SYMBOLS 1-3 ARE USED IN THE FIRST PLOT STATEMENT TO SYMBOLIZE THE THREE YEARS IN THE DATA ***;
symbol1 value=dot color=red;
symbol2 value=dot color=green;
symbol3 value=dot color=yellow;
*** SYMBOLS 4 IS USED IN THE PLOT2 STATEMENT ***;
symbol4 value=star color=black i=join;
proc gplot data=have;
plot ipppa * date = year /
href='01jun2006'd '01jun2007'd
;
plot2 tasaParoMensual * date ;
run;
quit;
Hope that helps.

Binned Bar chart using SAS

I'm trying to make a bar chart using SAS. I have multiple salaries data and I'd like to show a bar chart with the frequencies of these salaries. I've made this:
ODS GRAPHICS ON;
PROC FREQ DATA=WORKERS.SORT ORDER=INTERNAL;
TABLES salaries / NOCUM SCORES=TABLE plots(only)=freq;
RUN;
ODS GRAPHICS OFF;
It works, the problem is, that now I can see all (hundreds) of the salaries on the x-axis. I'd like to have just intervals of these salaries (like 20) so that I can get a more readable chart. I just can't find out how to do it. I've also tried it with this:
PROC CHART DATA=WORK.SORT;
vbar salaries;
RUN;
but that's a text representation of the chart, so I can't use it.
You can create a format and apply the format to the variable you want to group into buckets. Here's an example:
proc format ;
value myfmt
low - 13 = '13 and Under'
14 - high = '14 and Above';
run;
ODS GRAPHICS ON;
PROC FREQ DATA=sashelp.class ORDER=INTERNAL;
format age myfmt.;
TABLES age / NOCUM SCORES=TABLE plots(only)=freq;
RUN;
ODS GRAPHICS OFF;
Use PROC UNIVARIATE with the HISTOGRAM statement. http://support.sas.com/documentation/cdl/en/procstat/66703/HTML/default/viewer.htm#procstat_univariate_toc.htm
ods html;
proc univariate data=sashelp.cars noprint;
var msrp;
histogram;
run;
There are options for specifying bin size:
ods html;
proc univariate data=sashelp.cars noprint;
var msrp;
histogram / midpoints=30000 to 180000 by 30000;
run;
And just for completeness, I'll add another solution in case you want more control over the chart's appearance. Using the Graphics Template Language you can create some very nice looking charts.
The proc template statement defines how the chart will look. The sgrender runs the chart against the specified dataset. There's all kinds of options that are best explored in the online doc: http://support.sas.com/documentation/cdl/en/grstatgraph/65377/HTML/default/viewer.htm#p1sxw5gidyzrygn1ibkzfmc5c93m.htm
I've just taken the sample they provided and added the / nbins=20 option to have it automatically group into 20 bins. It also has options for start and end bin, bin size, etc..
proc template;
define statgraph histogram;
begingraph;
entrytitle "Histogram of Vehicle Weights";
layout overlay /
xaxisopts=(label="Vehicle Weight (LBS)")
yaxisopts=(griddisplay=on);
histogram weight / nbins=20;
endlayout;
endgraph;
end;
run;
proc sgrender data=sashelp.cars template=histogram;
run;