I want to create multiple plots by category. Currently my code is as follows:
proc gplot data=data;
plot (a b)*week
*by category;
/vaxis=axis3 haxis=axis3 legend=legend1 overlay skipmiss;
title font='HELVETICA' height=1.2 "Volumes";
run;
but this includes all the categories. How do I create distinct charts for different categories? Also the chart here is a scatter plot. How do I create a line chart?
A fellow SAS 9.1.x user? Assuming that you require a gplot-based example:
proc summary data = sashelp.class nway;
var height;
class sex age;
output out = class mean=;
run;
symbol1 interpol = join;
proc gplot data = class;
by sex;
plot height * age;
run;
quit;
Here proc summary conveniently produces a sorted output dataset without any duplicate y-values, allowing gplot to produce a pair of reasonable line charts via the by statement. I'm sure there are much nicer-looking alternatives via proc sgplot if you have a more recent version of SAS, but some of us have to make do with gplot.
Related
From annual data :
I would like to create the the data per day but I can't use the proc expand because the SAS ETS is not available.
Thank you for your suggestions.
Something like this is a basic approach perhaps:
create a list of dates for interpolation
merge have data (shown above, not included in code below)
Plot to see if linear pattern, (looks somewhat exponential/curved)
run linear regression, saving predicted values
plot interpolated values against actual values
data years;
do date='30Jun2017'd to '30Jun2022'd;
output;
end;
run;
data have;
merge years have;
by date;
format date date9.;
run;
proc sgplot data=have;
series x=date y=px_last;
run;
proc reg data=have plots;
model px_last = date;
output out=pred p=predicted_value;
run;
proc sgplot data=pred;
series x=date y=predicted_Value;
scatter x=date y=px_last;
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;
How can I automate this for each variable in a dataset without having to type the code below out several times? Is it then possible to print all the graphs to a pdf document?
For example, the table class has 4 columns: sex, age, height, weight
PROC UNIVARIATE DATA = sashelp.class NOPRINT;
HISTOGRAM weight / NORMAL;
RUN;
Use the key word _all_.
PROC UNIVARIATE DATA = sashelp.class NOPRINT;
HISTOGRAM _all_ / NORMAL;
RUN;
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;
I want to turn the entire row red for people whose names begin with 'J'. Is this possible using proc print?
ods html file=odsout style=htmlblue ;
proc print data=sashelp.class noobs label;
var name age;
run;
ods html close;
I don't believe it's possible with PROC PRINT. PROC REPORT can generate the identical output but with the rows red, however.
Identical:
proc report data=sashelp.class nowd;
columns name age;
run;
With red:
proc report data=sashelp.class nowd;
columns name age;
compute name;
if substr(name,1,1)='J' then
call define(_row_, "style", "style=[backgroundcolor=red]");
endcomp;
run;
I would consider it somewhat cleaner to use a style definition of course but for a one-off sort of thing this is easy.