Forest plot in SAS - sas

I have downloaded this code from the web and I need to create a forest plot for each of the variables that have values as 'YES' and 'NO'. It works when each variable have different values however my data has same values for each of the variables. I tried several options but it is not working. Original code is downloaded from http://onsasprogramming.blogspot.com/2016/01/forest-plot-of-hazard-ratios-by-patient.html.
%let graphs='.';
%let dpi=100;
%let w=8in;
%let h=4.5in;
/*--Leading blanks in the subgroup variable must be non--blank spaces --*/
/*--Use character value 'A0', or copy from Windows System Character Map--*/
/*--Regular leading blanks will be stripped, losing the indentation --*/
data forest;
input Indent Subgroup $ HR LCL UCL ;
DATALINES;
0 Diabets . . .
2 YES 1.049419501 1.014598 1.085436
2 NO 1.149419501 1.114598 1.185436
0 OBESITY . . .
2 YES 1.000419048 0.979467 1.021819
2 NO 1.010419048 0.179467 1.121819
0 HTN . . . .
2 YES 0.790764015 0.737 0.988322
2 NO 0.790764015 0.637 0.988322
;
run;
/*--Replace '.' in subgroup with blank--*/
data forest2;
set forest;
subgroup=translate(subgroup, ' ', '.');
val=mod(_N_-1, 6);
indent=ifn(indent eq 2, 1, 0);
if val eq 1 or val eq 2 or val eq 3 then ref=subgroup;
run;
/*--Create font with smaller fonts for axis label, value and data--*/
proc template;
define style listingSF;
parent = Styles.Listing;
style GraphFonts from GraphFonts
"Fonts used in graph styles" /
'GraphDataFont' = (", ",7pt)
'GraphValueFont' = (", ",6pt)
'GraphLabelFont' = (", ",6pt, bold);
end;
run;
/*--Define templage for Forest Plot--*/
/*--Template uses a Layout Lattice of 6 columns--*/
proc template;
define statgraph Forest;
dynamic _show_bands _color _thk;
begingraph;
entrytitle 'Forest Plot of Hazard Ratios by Patient Subgroups ';
discreteattrmap name='text';
value '0' / textattrs=(weight=bold);
value other;
enddiscreteattrmap;
discreteattrvar attrvar=type var=indent attrmap='text';
layout lattice / columns=2 columnweights=(0.25 1.0);
/*--First Subgroup column, shows only the Y2 axis--*/
layout overlay / walldisplay=none xaxisopts=(display=none)
yaxisopts=(reverse=true display=none
tickvalueattrs=(weight=bold));
referenceline y=ref / lineattrs=(thickness=_thk color=_color);
axistable y=subgroup value=subgroup / indentweight=indent textgroup=type;
endlayout;
/*--Third column showing odds ratio graph--*/
layout overlay / xaxisopts=(TYPE=LOG label=' <---PCI Better---- ----Medical Therapy Better--->'
linearopts=(tickvaluepriority=true
tickvaluelist=(0.0 0.5 1.0 1.5 2.0 2.5)))
yaxisopts=(reverse=true display=none) walldisplay=none;
referenceline y=ref / lineattrs=(thickness=_thk color=_color);
scatterplot y=subgroup x=HR / xerrorlower=LCL xerrorupper=UCL
markerattrs=(symbol=squarefilled);
referenceline x=1;
endlayout;
endlayout;
entryfootnote halign=left textattrs=(size=7)
'The p-value is from the test statistic for testing the interaction between the '
'treatment and any subgroup variable';
entryfootnote halign=left 'This graph uses the new AXISTABLE plot to display the textual columns';
endgraph;
end;
run;
/*--Need format to show missing as blank--*/
proc format;
value misblank
. = ' ';
run;
/*----Create Graph-----*/
ods listing style=htmlblue gpath=&graphs image_dpi=&dpi;
ods graphics / reset noscale width=&w height=&h imagename='GTL_ForestPlot';
proc sgrender data=Forest2 template=Forest;
format pvalue group pcigroup misblank7.2;
dynamic _color='cxf0f0f0' _thk=12;
run;
The forest plot should give YES and NO under HTN and Obesity but it doesn't. Any tip would be appreciated.

Related

SAS - SGPLOT - adding XAXIS group label

I am looking for an option to add a group label (grouping value of Xaxis). I tried X2AXIS and REFLINE option but none is working exactly. (see attached image for reference - I would like to add G1, G2 and G3 with brackets)
As a starting point, consider
data have;
call streaminit(2021);
do x = 1 to 6;
do _n_ = 1 to 2+ rand('integer', 5);
y = 5 + rand('integer', 10);
group = cats ('G', int((x+1)/2));
output;
end;
end;
run;
ods html file='plot.html';
proc sgplot data=have;
vbox y / group=group category=x;
run;
ods html close;
which produces

Styling different groups in SAS SGPLOT

I am trying to produce a graph with multiple groupings. The sample data code is:
proc sort data=sashelp.cars out=cars;
by DriveTrain;
where n(Cylinders);
run;
I used dattrmap to add distinct colors to the different groups as follow:
data MyAttrMap;
length MARKERCOLOR CONTRASTCOLOR color $25;
ID='myreg'; value='All' ; MARKERCOLOR='red'; color='red'; MARKERSYMBOL = 'circle'; output;
ID='myreg'; value='Front'; MARKERCOLOR='blue'; color='blue'; MARKERSYMBOL = 'circle'; output;
ID='myreg1'; value='USA'; CONTRASTCOLOR='yellow'; color='yellow'; output;
ID='myreg1'; value='Europe'; CONTRASTCOLOR='black'; color='black'; output;
ID='myreg1'; value='Asia'; CONTRASTCOLOR='green'; color='green'; > > output;
run;
proc sgplot data=work.cars
dattrmap=MyAttrMap;
hbarparm category=enginesize response=horsepower/group=DriveTrain barwidth=.5 attrid=myreg name='dt';
scatter X=MPG_City Y=enginesize /group=origin name='origin' attrid=myreg1;
keylegend 'dt' / title='Drive Train' location=outside position=bottom;
keylegend 'origin' / title='Origin' location=outside position=bottom;
where DriveTrain in ('All' 'Front');
run;
The Attrmap was created with the intention of having different colors for Origin and DriveTrain however, when the output is created the same colors applied to Origin are applied to DriveTrain.
I also tried using Proc template to change the style as follow:
/*Different colors from the ones used above*/
proc template;
define style MyStyle;
parent = Styles.Listing;
STYLE graphdata1 /
MARKERSYMBOL = 'circle'
LINESTYLE = 1
CONTRASTCOLOR = liypk
COLOR = red
;
STYLE graphdata2 /
MARKERSYMBOL = 'circle'
LINESTYLE = 1
CONTRASTCOLOR = stybr
COLOR = yellow
;
STYLE graphdata3 /
MARKERSYMBOL = 'circle'
LINESTYLE = 1
CONTRASTCOLOR = mog
COLOR = green
;
STYLE graphdata4 /
MARKERSYMBOL = 'circle'
LINESTYLE = 1
CONTRASTCOLOR = brown
COLOR = pink
;
STYLE graphdata5 /
MARKERSYMBOL = 'circle'
LINESTYLE = 1
CONTRASTCOLOR = black
COLOR = grey
;
end;
run;
But still the same results were obtained. Could anyone please tell me what I'm doing wrong or how to get this to work? I'm using SAS 9.3.
Another issue I'm encountering is the sorting. I want to sort the bars so that the same origins appear together and by the horsepower. I sorted using sortkey=national and used grouporder=data as recommended by SAS but this didn't change the ordering in the output. Any help is appreciated.
.
Thanks.
You might find SGPANEL a better option for visually presenting the distributions of different groups.
ods html style=normal;
ods graphics / height=900px;
proc sgpanel data=sashelp.cars;
panelby origin
/ columns=3
;
hbar enginesize
/ group=drivetrain
groupdisplay=cluster
;
where
DriveTrain in ('Front', 'All')
and not missing(cylinders)
;
run;
Check your attribute map data set. Because you haven't specified the lengths for the Value and ID column they're truncated and don't match your data so they don't get assigned correctly.
Simplifying your problem, I assigned all the elements for testing:
I also assumed this was mocked up because of the errors in the log.
proc sort data=sashelp.cars out=cars;
by DriveTrain;
where n(Cylinders);
run;
data MyAttrMap;
length ID $10. linecolor MARKERCOLOR CONTRASTCOLOR fillcolor color value $25;
ID='myreg1';
value='USA';
contrastcolor='cxaf8dc3';
LINECOLOR='cxaf8dc3';
MARKERCOLOR='cxaf8dc3';
fillcolor='cxaf8dc3';
output;
ID='myreg1';
value='Europe';
contrastcolor='cx7fbf7b';
LINECOLOR='cx7fbf7b';
MARKERCOLOR='cx7fbf7b';
fillcolor='cx7fbf7b';
output;
ID='myreg1';
value='Asia';
contrastcolor='cx91bfdb';
LINECOLOR='cxfc8d59';
MARKERCOLOR='cxfc8d59';
fillcolor='cxfc8d59';
output;
run;
ods graphics / attrpriority=none;
proc sgplot data=work.cars dattrmap=MyAttrMap;
scatter X=MPG_City Y=enginesize /group=origin name='origin' attrid=myreg1;
where DriveTrain in ('All' 'Front');
run;

SAS---how to create histograms plot with specific bin in certain range

I attempt to crate histograms plot via proc univariate. The target is to crate the distribution with bins of 0.1 width from 0 to 1.5 and then all the remaining in one bin.
I applied the following code to identify the range from 0 to 1.5, while it cannot manage the rest. How can I correct the code?
proc univariate data=HAVE;
where pred between 0 and 1.5;
var pred;
histogram pred/ vscale=percent midpoints=0 to 2 by 0.1 normal (noprint);
run;
You can try something like the following code to combine two Histograms by creating two variables from one variable:
/*Temporary DS with values ranging from 01. to 2.0*/
data have;
do i=0.1 to 2.0 by 0.1;
output;
end;
rename i=pred;
run;
/*Creating two variables x(0.1-1.5) and y(1.6-2.0)*/
data have;
set have;
if pred<1.6 then x=pred;
else y=pred;
drop pred;
run;
/*Combine two Histograms*/
proc sgplot data=have;
histogram x / nbins=15 binwidth=0.1;
density x / type=normal;
histogram y / nbins=5 binwidth=1.0;
density y / type=normal;
keylegend / location=inside position=topright noborder across=2;
xaxis display=(nolabel) values=(0.1 to 2.5 by 0.1);
run;
Create your own groups
Create a format so it shows the way you'd like
Plot it with SGPLOT
*create your own groups for data, especially the last group;
data mileage;
set sashelp.cars;
mpg_group=floor(mpg_highway / 10);
if mpg_group in (5, 6, 7) then
mpg_group=5;
keep mpg_highway mpg_group;
run;
*format to control display;
proc format;
value mpg_fmt 1='0 to 10' 2='11 to 20' 3='21 to 30' 4='31 to 40' 5='40+';
run;
*plot the data;
proc sgplot data=mileage;
vbar mpg_group /stat=freq barwidth=1;
format mpg_group mpg_fmt.;
run;

SAS / PROC FREQ TABLES - can I suppress frequencies and percents if frequency is less than a given value?

I'm using tagsets.excelxp in SAS to output dozens of two-way tables to an .xml file. Is there syntax that will suppress rows (frequencies and percents) if the frequency in that row is less than 10? I need to apply that in order to de-identify the results, and it would be ideal if I could automate the process rather than use conditional formatting in each of the outputted tables. Below is the syntax I'm using to create the tables.
ETA: I need those suppressed values to be included in the computation of column frequencies and percents, but I need them to be invisible in the final table (examples of options I have considered: gray out the entire row, turn the font white so it doesn't show for those cells, replace those values with an asterisk).
Any suggestions would be greatly appreciated!!!
Thanks!
dr j
%include 'C:\Users\Me\Documents\excltags.tpl';
ods tagsets.excelxp file = "C:\Users\Me\Documents\Participation_rdg_LSS_3-8.xml"
style = MonoChromePrinter
options(
convert_percentages = 'yes'
embedded_titles = 'yes'
);
title1 'Participation';
title2 'LSS-Level';
title3 'Grades 3-8';
title4 'Reading';
ods noproctitle;
proc sort data = part_rdg_3to8;
by flag_accomm flag_participation lss_nm;
run;
proc freq data = part_rdg_3to8;
by flag_accomm flag_participation;
tables lss_nm*grade_p / crosslist nopercent;
run;
ods tagsets.excelxp close;
D.Jay: Proc FREQ does not contain any options for conditionally masking cells of it's output. You can leverage the output data capture capability of the ODS system with a follow-up Proc REPORT to produce the desired masked output.
I am guessing on the roles of the lss and grade_p as to be a skill level and a student grade level respectively.
Generate some sample data
data have;
do student_id = 1 to 10000;
flag1 = ranuni(123) < 0.4;
flag2 = ranuni(123) < 0.6;
lss = byte(65+int(26*ranuni(123)));
grade = int(6*ranuni(123));
* at every third lss force data to have a low percent of grades < 3;
if mod(rank(lss),3)=0 then
do until (grade > 2 or _n_ < 0.15);
grade = int(6*ranuni(123));
_n_ = ranuni(123);
end;
else if mod(rank(lss),7)=0 then
do until (grade < 3 or _n_ < 0.15);
grade = int(6*ranuni(123));
_n_ = ranuni(123);
end;
output;
end;
run;
proc sort data=have;
by flag1 flag2;
*where lss in ('A' 'B') and flag1 and flag2; * remove comment to limit amount of output during 'learning the code' phase;
run;
Perform the Proc FREQ
Only capture the data corresponding to the output that would have been generated
ods _all_ close;
* ods trace on;
/* trace will log the Output names
* that a procedure creates, and thus can be captured
*/
ods output CrossList=crosslist;
proc freq data=have;
by flag1 flag2;
tables lss * grade / crosslist nopercent;
run;
ods output close;
ods trace off;
Now generate output to your target ODS destination (be it ExcelXP, html, pdf, etc)
Reference output of which needs to be produced an equivalent having masked values.
* regular output of FREQ, to be compare to masked output
* of some information via REPORT;
proc freq data=have;
by flag1 flag2;
tables lss * grade / crosslist nopercent;
run;
Proc REPORT has great features for producing conditional output. The compute block is used to select either a value or a masked value indicator for output.
options missing = ' ';
proc format;
value $lss_report ' '= 'A0'x'Total';
value grade_report . = 'Total';
value blankfrq .b = '*masked*' ._=' ' other=[best8.];
value blankpct .b = '*masked*' ._=' ' other=[6.2];
proc report data=CrossList;
by flag1 flag2;
columns
('Table of lss by grade'
lss grade
Frequency RowPercent ColPercent
FreqMask RowPMask ColPMask
)
;
define lss / order order=formatted format=$lss_report. missing;
define grade / display format=grade_report.;
define Frequency / display noprint;
define RowPercent / display noprint;
define ColPercent / display noprint;
define FreqMask / computed format=blankfrq. 'Frequency' ;
define RowPMask / computed format=blankpct. 'Row/Percent';
define ColPMask / computed format=blankpct. 'Column/Percent';
compute FreqMask;
if 0 <= RowPercent < 10
then FreqMask = .b;
else FreqMask = Frequency;
endcomp;
compute RowPMask;
if 0 <= RowPercent < 10
then RowPMask = .b;
else RowPMask = RowPercent;
endcomp;
compute ColPMask;
if 0 <= RowPercent < 10
then ColPMask = .b;
else ColPMask = ColPercent;
endcomp;
run;
ods html close;
If you have to produce lots of cross listings for different data sets, the code is easily macro-ized.
When I've done this in the past, I've first generated the frequency to a dataset, then filtered out the N, then re-printed the dataset (using tabulate usually).
If you can't recreate the frequency table perfectly from the freq output, you can do a simple frequency, check which IDs or variables or what have you to exclude, and then filter them out from the input dataset and rerun the whole frequency.
I don't believe that you can with PROC FREQ, but you can easily replicate your code with PROC TABULATE and you can use a custom format there to mask the numbers. This example sets it to M for missing and N for less than 5 and with one decimal place for the rest of the values. You could also replace the M/N with a space (single space) to have no values shown instead.
*Create a format to mask values less than 5;
proc format;
value mask_fmt
. = 'M' /*missing*/
low - < 5='N' /*less than 5 */
other = [8.1]; /*remaining values with one decimal place*/
run;
*sort data for demo;
proc sort data=sashelp.cars out=cars;
by origin;
run;
ods tagsets.excelxp file='/folders/myfolders/demo.xml';
*values partially masked;
proc tabulate data=cars;
where origin='Asia';
by origin;
class make cylinders;
table make, cylinders*n*f=mask_fmt. ;
run;
ods tagsets.excelxp close;
This was tested on SAS UE.
EDIT: Forgot the percentage piece, so this likely will not work for that, primarily because I don't think you'll get the percentages the same as in PROC FREQ (appearance) so it depends on how important that is to you. The other possibility to accomplish this would be to modify the PROC FREQ template to use the custom format as above. Unfortunately I do not have time to mock this up for you but maybe someone else can. I'll leave this here to help get you started and delete it later on.

Order of data displayed in a SAS proc GCHART HBAR statement

I have the following, but I wish to control the order in which the data is displayed. Instead of displaying the bars in the order of A, B, C, D, E, F, I wish to display the bars based on a user-specified ordering. For example, I would like to be able to assign in a SAS dataset a value to a variable named rank that will control the order in which the bars are stacked.
How can I do this?
%let name=ex_17;
%let myfont=Albany AMT;
goptions reset=all;
goptions reset=(global goptions);
/*GOPTIONS DEVICE=png xpixels=800 ypixels=400;*/
goptions gunit=pct border cback=white colors=(blacks) ctext=black
htitle=4 htext=3.0 ftitle="&myfont" ftext="&myfont";
data mileage;
length factor $ 24;
input factor $ level $ value;
datalines;
C left -38.882
C right 39.068
D right 38.99
D left -38.97
E right 38.982
E left -38.975
F left -38.973
F right 38.979
B left -38.975
B right 38.975
A right 38.977
A left -38.973
;
/* base case: 38.975 */
data mileage;
set mileage;
if level="right" then value = value - 38.975;
if level="left" then value = -1*(38.975 - value*-1);
run;
data convert;
set mileage;
*if level='left' then value=-value;
run;
proc format;
picture posval low-high='000,009';
run;
data anlabels(drop=factor level value);
length text $ 24;
retain function 'label' when 'a' xsys ysys '2' hsys '3' size 2;
set convert;
midpoint=factor; subgroup=level;
*text=left(put(value, BEST6.3));
if level ='left' then position='>';
else position='<'; output;
run;
title1 'Sensitivity Analysis graph';
*footnote1 justify=left ' SAS/GRAPH' move=(+0,+.5) 'a9'x move=(+0,-.5) ' Software'
justify=right 'DRIVER ';
*title2 'by Daniel Underwood' h=3.0;
footnote1 'Estimates accurate within +/- 0.002';
*axis1 label=(justify=left 'Disutility') style=0 color=black;
axis1 label=(justify=left '') style=0 color=black;
*axis2 label=none value=(tick=3 '') minor=none major=none
width=3 order=(-10000 to 20000 by 10000) color=black;
axis2 label=none minor=none major=none value=(tick=3 '')
width=3 order=(-0.093 to 0.093 by 0.186) color=black;
pattern1 value=solid color=ltgray;
pattern2 value=solid color=ltgray;
/*
goption vpos=25;
goptions vsize=5in;
*/
proc gchart data=convert;
format value BEST6.3;
note move=(40,90) height=3 'Women' move=(+12,+0) 'Men';
hbar factor / sumvar=value discrete nostat subgroup=level
maxis=axis1 raxis=axis2 nolegend annotate=anlabels
coutline=same des='' space=2;
run;
quit;
The order of values displayed is controlled by the ORDER= option on either an AXIS statement (to order midpoints or the chart variable) or a LEGEND statement (to order values of a sub-group variable).
If you are asking for a way to use a variable named RANK to control the order for sub-group variables, here is a SAS sample program that does exactly that.