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.
Related
New to SAS so please bear with me. :) I'm trying to graph an output table with three y variables and one x. I've tried gplot and plot, however, I'm still getting iffy results and I can't figure out how to make them all show in one graph either :( I think my table is too large for pasting here so I uploaded to office.com and hopefully, someone smarter than I can figure this out.
https://1drv.ms/u/s!AnxXzVHJV4pKghj1MoJoWOQxzYTd?e=cJ1J5y
Use three SERIES statements in SGPLOT
Example:
data have;
do x = -10 to 10 by .1;
y1 = x**2 / 10;
y2 = 4 * sin ( x / 5 );
y3 = x;
output;
end;
run;
ods html file='plot.html' style=plateau;
proc sgplot data=have;
series x=x y=y1;
series x=x y=y2;
series x=x y=y3;
run;
ods html close;
Output:
The old school Proc GPLOT would use the PLOT / OVERLAY option:
goptions reset=all;
symbol value=none interpol=join;
proc gplot data=have;
plot (y1-y3) * x / overlay; /* ( list of y-variables ) * x-variable */
run;
proc sgplot might be what you are looking for.
https://support.sas.com/resources/papers/proceedings10/154-2010.pdf
It is not clear from the link or question what exactly you are looking for but if I had to guess I would say this would certainly help.
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.
I want to make a simple time series line plot without highlighting any dots on the line. I can plot var1 and var2 using the following code.
title "Title";
proc gplot data=test;
plot var1 *var2 /overlay grid hminor=0 ;
run;
quit;
However I want to add another variable into the plot. I tried the following code. Because the scale of var1 and var3 are quite large, so var3 are not properly scaled in the graph. Can anyone teach me how to use different scale for var1 and var3 please.
title "Title";
proc gplot data=Test;
plot var1 *var2 Var3*var2 /overlay grid hminor=0 ;
run;
quit;
Additionally, may I ask whether sas can do subplot as matlab please. Essentially, I got one big graph with two separate sub-graph. If possible, please teach me how to achieve this. I tried vpercent = 50, but it seems there are something wrong in my code.
proc gplot data=Test vpercent=50;
plot VAR1 *VAR2 VAR3*VAR2 /overlay grid hminor=0 ;
run;
quit;
With Thanks
Assuming I understand what you mean, if you have access to SGPLOT you can specify that X3 should be on a different axis. Here's an example with the SASHELP.STOCKS data which plots the open price on one Y axis and then the trade volume on the second Y axis.
proc sgplot data=sashelp.stocks;
where stock='IBM';
series x=date y=open;
series x=date y=volume/y2axis;
run;quit;
Here is some SAS code that builds on Reeza's excellent example and suggestion to use SGPANEL. See the PANELBY statement and the options used there.
*** SUBSET DATA AND SORT ***;
proc sort data=sashelp.stocks out=ibm;
where stock='IBM';
by date;
run;
*** TRANSPOSE DATA FROM "SHORT-AND-WIDE" TO "LONG-AND-THIN" ***;
proc transpose data=ibm out=ibm_t;
by date;
var open volume;
run;
proc sgpanel data=ibm_t;
*** ROW LATTICE OPTION STACKS PLOTS ***;
*** UNISCALE OPTION LETS EACH PANEL HAVE IT'S OWN SCALE ***;
*** NOVARNAME SUPPRESSES LABEL FOR THE Y-AXIS ON THE RIGHT SIDE ***;
panelby _name_ / layout=rowlattice uniscale=column novarname;
series x=date y=col1;
*** SUPPRESS LABEL FOR THE Y-AXIS ON THE LEFT SIDE ***;
rowaxis display=(nolabel);
run;
I'm not very experienced in SAS yet.
My problem is that I need to add number of observations to a boxplot (I'm using proc boxplot). I tried insetgroup option, but I don't like the result, I need something prettier.
I have found this
http://support.sas.com/resources/papers/wusspaper.pdf
I need something like this, with numbers in the inner margin
It's great they have code there, but I don't get where are these numbers (No. of subjects at visit) are taken from, if they are calculated separately, where they are in a dataset, etc. It's a pity the initial dataset is not shown.
Any help and any other ideas how to add numbers of patients will be very appreciated.
Below is some SAS code where the Ns are added to proc boxplot using annotate. In general for annotate, you need to be careful about setting up the coordinate system you want, read the documentation regarding annotate and xsys/ysys for a detailed explaination.
Hope this helps.
proc sort data=sashelp.class out=work.class;
by sex;
run;
*** GET COUNTS FOR EACH GROUP ***;
proc freq data=class;
tables sex / out=stats;
run;
*** CREATE ANNOTATE DATASET ***;
data anno_stats;
set stats (drop=percent);
xsys='2';
ysys='1';
position='5';
function='label';
text='N=' || strip( put(count, 3.));
*** X COORDINATE IS THE GROUP VARIBLE IN THE BOXPLOT ***;
*** USE VARIABLE XC INSTEAD OF X SINCE THIS IS A CHARACTER VARIABLE IN THIS EXAMPLE ***;
xc=sex;
*** Y COORDIANTE IS 3% ABOVE X-AXIS, BASED ON YSYS=1 ***;
y=3;
run;
proc boxplot data=class anno=anno_stats;
plot height * sex;
run;
I wrote the following code in sas, but I did not get result!
The result histogram in grey and the range of data is not as I specified! what is the problem?
I got the following warning too: WARNING: The MIDPOINTS= list was extended to accommodate the data
what about color?
axis1 order=(0 to 100000 by 50000);
axis2 order=(0 to 100 by 5);
run;
proc capability data=HW2 noprint;
histogram Mvisits/midpoints=0 to 98000 by 10000
haxis=axis1
cfill=blue;
run;
.......................................
I have the same problem with boxplot, for example I got the following plot and I want to change the distances, then I could see the plot better, but I could not.
The below is for proc univariate rather than proc capability, I do not have access to SAS/QC to test, but the user guide shows very similar syntax for the histogram statements. Hopefully, you'll be able to translate it back.
It looks like you are having problems with the colour due to your output system. Your graphs are probably delivered via ODS, in which case the cfill option does not apply (see here and not the Traditional Graphics tag).
To change the colour of the histogram bars in ODS output you can use proc template:
proc template;
define style styles.testStyle;
parent = styles.htmlblue;
style GraphDataDefault /
color = green;
end;
run;
ods listing style = styles.testStyle;
proc univariate data = sashelp.cars;
histogram mpg_city;
run;
An example explaining this can be found here.
Alternatively you can use proc sgplot to create a histogram with more control of the colour as follows:
proc sgplot data = sashelp.cars;
histogram mpg_city / fillattrs = (color = red);
run;
As to your question of truncating the histogram. It doesn't really make a great deal of sense to ignore the extreme values as it will give you an erroneous image of the distribution, which somewhat defeats the purpose of the histogram. That said, you can achieve what you are asking for with bit of a hack:
data tempData;
set sashelp.cars;
tempClass = 1;
run;
proc univariate data = tempData noprint;
class tempClass;
histogram mpg_city / maxnbin = 5 endpoints = 0 to 25 by 5;
run;
In the above a dummy class tempClass is created and then comparative histograms are requested using the class statement. maxnbins will limit the number of bins displayed only in a comparative histogram.
Your other option is to exclude (or cap) your extreme points before creating the histogram, but this will lead to slightly erroneous frequency counts/percentages/bar heights.
data tempData;
set sashelp.cars;
mpg_city = min(mpg_city, 20);
run;
proc univariate data = tempData noprint;
histogram mpg_city / endpoints = 0 to 25 by 5;
run;
This is a possible approach to original question (untested as no SAS/QC or data):
proc capability data = HW2 noprint;
histogram Mvisits /
midpoints = 0 to 300000 by 10000
noplot
outhistogram = histData;
run;
proc sgplot data = histData;
vbar _MIDPT_ /
response = _OBSPCT_
fillattrs = (color = blue);
where _MIDPT_ <= 100000;
run;