Suppress "Frequency" text in PROC FREQ - sas

How can I get rid of the box inside the red circle
code:
data mydata;
input x y;
datalines;
1 2
3 4
1 5
3 2
;
run;
proc freq data=mydata;
tables x*y /nopercent norow nocol;
run;

As always with SAS there are multiple paths to the desirred result.
If you are willing to step away from proc freq you can achieve the required output using proc tabulate. This provides a simpler (and more concise) solution and does not require digging through the long and often confusing proc template documents.
proc tabulate data = mydata;
/* Specify the variables to investigate */
class x y;
table
/* Put x in the rows suppress its title and add a total row */
x = "" all = "Total",
/* Put y in the columns, request a total, request frequency count*/
(y all = "Total") * n = "" /
/* Put "x" in the top left box */
box = "x"
/* Put 0 instead of missing */
misstext = "0";
run;

data mydata;
input x y;
datalines;
1 2
3 4
1 5
3 2
;
run;
/*Write new template to work folder */
ods path work.templat(update) sashelp.tmplmst(read);
/*Remove the "Frequency" header from the template */
proc template;
edit Base.Freq.CrossTabFreqs;
edit frequency;
header="";
end;
end;
run;
proc freq data=mydata;
tables x*y /nopercent norow nocol;
run;

The only way I know how to do this is to edit the PROC FREQ template. Since 9.3 or so, PROC FREQ has been rewritten as a basic table via PROC TEMPLATE, which is nice because it lets us edit nearly all aspects of it.
What I do is right click on the Results tree header in display manager - like so:
Then select "Templates". This opens up the template list, which includes all templates you have installed (both style templates and table templates - most of the PROCs have a table template, though msot do not give you as much control as PROC FREQ does).
Then navigate to Sashelp.Tmplmst (Which is the major template store for most preinstalled templates), and open up Base, and then Freq. This gives you a list of table templates that make up PROC FREQ.
In your case, you want to open CrossTabFreqs, which is what you're producing (A crosstab). You could identify this by running ods trace on; then running your code; it will tell you in the log what the name of the template it's using is.
Open that and copy the contents of the template browser to the clipboard, then paste in a new window. We will modify it, save it in a temporary location, and use it.
Add to the top of the program this line:
ods path work.templat(update) sashelp.tmplmst(read);
This tells SAS two things: first, when we update or write to the template store, do that in work; and two, to look in work first then in sashelp for templates. This way when we create our new CrossTabFreqs template, it will come from work.
Now, look in the program for the location where it defines the Frequency block. This is what you want to modify, since this is the instructions for SAS telling it what to print when it is showing frequencies (as opposed to percents or whatnot). It will read as so:
define Frequency;
header = "Frequency";
format = BEST7.;
label = "Frequency Count";
print;
data_format_override;
end;
You want to remove the header. You can do that one of two ways: either remove the header line entirely, or my preferred option, change it to
define Frequency;
header = " ";
format = BEST7.;
label = "Frequency Count";
print;
data_format_override;
end;
Then run the entire program (including the ods path statement). Now, when you use PROC FREQ in this SAS session, it will have no text in the frequency table header.

Related

I need help printing multiple confidence intervals in sas

I am being asked to provide summary statistics including corresponding confidence interval (CI) with its width for the population mean. I need to print 85% 90% and 99%. I know I can either use univariate or proc means to return 1 interval of your choice but how do you print all 3 in a table? Also could someone explain the difference between univariate, proc means and proc sql and when they are used?
This is what I did and it only printed 85% confidence.
proc means data = mydata n mean clm alpha = 0.01 alpha =0.1 alpha = 0.15;
var variable;
RUN;
To put all three values in one table you can execute your step three times and put the results in one table by using an append step.
For shorter code and easier usage you can define a macro for this purpose.
%macro clm_val(TAB=, VARIABLE=, CONF=);
proc means
data = &TAB. n mean clm
alpha = &CONF.;
ods output summary=result;
var &VARIABLE.;
run;
data result;
length conf $8;
format conf_interval percentn8.0;
conf="&CONF.";
conf_interval=1-&CONF.;
set result;
run;
proc append data = result
base = all_results;
quit;
%mend;
%clm_val(TAB=sashelp.class, VARIABLE=age, CONF=0.01);
%clm_val(TAB=sashelp.class, VARIABLE=age, CONF=0.1);
%clm_val(TAB=sashelp.class, VARIABLE=age, CONF=0.15);
The resulting table looks like this:

SAS ODS RTF - Best option to embed Title/Footnote into Table

Looking for advice to embed title/footnote as part of the table (see below - making it easier to copy & paste into the different document)
Options explored so far
1) PROC REPORT - COMPUTE PAGE BEFORE (COMPUTE doesn't support justification option and did not find any reliable option to right-align "page x of y" text in title1 e.g. calculating and inserting BLANK space. In addition, I have a need to center align the title)
2) ODS RTF - BODYTITLE and BODYTITLE_AUX option (displays title/footnote as part of the body but not exactly as part of the table - not easy to select as one object)
The SAS ODS inline styling directive ^{PAGEOF} will produce Page x of y output in the output file. Since the output is a Word field you might need to Ctrl-A, F9 to compute the field values when the document is opened.
The RTF destination renders TITLE and FOOTNOTE in the header and footer part of the documents, so tricks are needed to produce per-table 'titles' and 'footers'. From my perspective as a document reader, the best place for PAGEOF would be in the header section and not as part of a table header.
ods escapechar = '^';
title justify=right '^{PAGEOF}';
ODS TEXT= can be used to add paragraphs before and after a Proc TABULATE that does statistical reporting. ODS TEXT= will process inline ODS formatting directives (including PAGEOF). See SAS® 9.4 Output Delivery System: User’s Guide, Fifth Edition, ODS ESCAPECHAR Statement for more info.
ods text='
Narrative before tabular output via ODS TEXT=
^{NEWLINE} Inline styling directives will be processed.
^{NEWLINE} ^{STYLE [color=green]Mix and match}
^{NEWLINE} Let''s see why.
';
ods text='Here you are at ^{STYLE ^{PAGEOF}}';
* This might require Word field computation or print preview to get proper numbers;
Depending on the destination STYLE=, such as ods rtf style=plateau … ODS TEXT might have some outlining or other styling artifacts.
If you are rtf hardcore, ODS TEXT= can also inject rtf codes directly into the destination stream to produce any rtf possible production. In the following example:
legacy style function ^S{attr-name=attr-value} is used to force the text container to be 100% of the page width.
current style function ^{RAW function is used to introduce raw rtf coding. Each { of the actual rtf is introduced using {RAW. Note how the RAWs can be (and are) nested.
ods text = '^S={outputwidth=100% just=c} ^{RAW \rtf1\ansi\deff0^{RAW \fonttbl^{RAW \f0 Arial;}}
\qc\f0\fs20\i\b
This output created with ODS TEXT=\line
Injecting raw RTF coding\line
Such as ^{RAW \cf11\ul colored and underlined}\line
Not for the casual coder.
}';
Some procedures, such as Proc PRINT have style options such as style(table)=[ … ] in which a pretext= and posttext= can be specified, and such texts will be rendered before and after the rtf table -- the texts are not part of the table and would not be 'picked up' in a single click of Word's 'table select' icon (). Also, unfortunately, the pretext= and posttext= values are not processed for ODS styling directives. However, the values can be raw rtf!
PRETEXT demonstrating inline styling is not honored:
proc print
data=sashelp.class (obs=3)
noobs
style(table)=[
pretext='^{STYLE [color=Red]Above 1 ^{NEWLINE}Above 2 - Pretext= is unprocessed ODS directives}'
posttext='^{STYLE [color=Green] Below}'
]
;
run;
PRETEXT demonstrating as raw rtf passed through (when first character is {)
proc print
data=sashelp.class (obs=3)
noobs
style(table)=[
pretext='{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}}
\qc\f0\fs20\i\b This output created with SAS PRETEXT=
\line Injecting raw RTF coding
\line Not for the casual coder.
}'
posttext='{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}}
\qc\f0\fs20\i\b This output created with SAS POSTTEXT=
\line Injecting raw RTF coding
\line Not for the casual coder.
}'
]
;
run;
Proc TABULATE does not have a style(table) option, but the TABLE statement does have options:
/ CAPTION= (rendered when OPTION ACCESSIBLETABLE; active)
Note: Caption value is rendered in the page dimension container, so the caption value be overwritten if your TABLE statement has a page dimension in it's crossings.
/ STYLE=[PRETEXT='...' POSTTEXT='...']
Same caveats as mentioned earlier
TABULATE does not:
have any feature that will let you annotate a column header with a row statistic (in this case your (N=###) as part of the category value). A precomputation step that summarizes the crossings to be tabulated will allow you to place a statistic there.
provide any mechanism for inserting a blank or label row that spans the table (such as the LINE statement in Proc REPORT)
Consider this tabulate example with accessibletable on for some medical data. Some of the variables are for:
categorical demographics (such as sex),
continuous measures (such as age cost),
binary flags about state.
data have;
call streaminit(123);
do _n_ = 1 to 1e3 - 300 + rand('uniform',600);
patientId + 1;
category = ceil(rand('uniform',4));
age = 69 + floor(rand('uniform',20));
cost = 500 + floor(rand('uniform',250));
length sex $1;
sex = substr('MF', 1+rand('uniform',2));
array flags flag1-flag3; * some flags asserting some medical state is present;
do over flags; flags = rand('uniform', 4) < _i_; end;
output;
end;
label age = 'Age' cost = 'Cost' sex = 'Sex';
run;
* Precomputation step;
* Use SQL to compute the N= crossings and make that part of a
* new variable that will be in the tabulation column dimension;
proc sql;
create table for_tabulate as
select
*
, catx(' ', 'Category', category, '( n =', count(*), ')')
as category_counted_columnlabel
from have
group by category
;
quit;
Tabulation report
options accessibletable;
proc tabulate data=for_tabulate ;
class
category_counted_columnlabel
sex
/
missing style=[fontsize=18pt]
;
var age cost flag1-flag3;
table
/* row dimension */
age * ( mean std min max median n*f=8.) * [style=[cellwidth=0.75in]]
N='Sex' * sex
cost * ( mean std min max median n*f=8. )
(flag1 - flag3) * mean = '%' * f=percent5.1
,
/* column dimension */
category_counted_columnlabel = ''
/
/* options */
nocellmerge
caption = "This is caption text is ^{STYLE [color=green]from Mars}^{NEWLINE}Next line"
;
run;
For SAS versions before 9.4M6 you can add a page dimension variable (whose value is inlined ODS stylized text to be the caption) to the output of the precomputation step and specify that variable in the table statement. Something like
, '^{NEWLINE}title1' /* SQL step */
|| '^{NEWLINE}title2'
|| '^{NEWLINE}title3'
|| '^{NEWLINE}title4'
as pagedim_title
and
/* tabulate step */
class pagedim_title / style = [background=lightgray fontfamily=Arial textalign=right];
table pagedim_title, …, category_counted_columnlabel = '' … ;
It might be easier to edit the RTF.
read the file and count the occurrences of cf1{Page
{\field{*\fldinst { PAGE }}} of {\field{*\fldinst { NUMPAGES
}}}\cell}
then read again and write modify those lines as you write the new
file.

PROC FREQ on multiple variables combined into one table

I have the following problem. I need to run PROC FREQ on multiple variables, but I want the output to all be on the same table. Currently, a PROC FREQ statement with something like TABLES ERstatus Age Race, InsuranceStatus; will calculate frequencies for each variable and print them all on separate tables. I just want the data on ONE table.
Any help would be appreciated. Thanks!
P.S. I tried using PROC TABULATE, but it didn't not calculate N correctly, so I'm not sure what I did wrong. Here is my code for PROC TABULATE. My variables are all categorical, so I just need to know N and percentages.
PROC TABULATE DATA = BCanalysis;
CLASS ERstatus PRstatus Race TumorStage InsuranceStatus;
TABLE (ERstatus PRstatus Race TumorStage) * (N COLPCTN), InsuranceStatus;
RUN;
The above code does not return the correct frequencies based on InsuranceStatus where 0 = insured and 1 = uninsured, but PROC FREQ does. Also doesn't calculate correctly with ROWPCTN. So any way that I can get PROC FREQ to calculate multiple variables on one table, or PROC TABULATE to return the correct frequencies, would be appreciated.
Here is a nice image of my output in a simplified analysis of only ERstatus and InsuranceStatus. You can see that PROC FREQ returns 204 people with an ERstatus of 1 and InsuranceStatus of 1. That's correct. The values in PROC TABULATE are not.
OUTPUT
I'll answer this separately as this is answering the other possible interpretation of the question; when it's clarified I'll delete one or the other.
If you want this in a single printed table, then you either need to use proc tabulate or you need to normalize your data - meaning put it in the form of variable | value. PROC FREQ is not capable of doing multiple one-way frequencies in a single table.
For PROC TABULATE, likely your issue is missing data. Any variable that is on the class statement will be checked for missingness, and if any rows are missing data for any of the class variables, those rows are entirely excluded from the tabulation for all variables.
You can override this by adding the missing option on the class statement, or in the table statement, or in the proc tabulate statement. So:
PROC TABULATE DATA = BCanalysis;
CLASS ERstatus PRstatus Race TumorStage InsuranceStatus/missing;
TABLE (ERstatus PRstatus Race TumorStage) * (N COLPCTN), InsuranceStatus;
RUN;
This will result in a slightly different appearance than on your table, though, as it will include the missing rows in places you probably do not want them, and they'll be factored against the colpctn when again you probably don't want them.
Typically some manipulation is then necessary; the easiest is to normalize your data and then run a tabulation (using PROC TABULATE or PROC FREQ, whichever is more appropriate; TABULATE has better percentaging options though) against that normalized dataset.
Let's say we have this:
data class;
set sashelp.class;
if _n_=5 then call missing(age);
if _n_=3 then call missing(sex);
run;
And we want these two tables in one table.
proc freq data=class;
tables age sex;
run;
If we do this:
proc tabulate data=class;
class age sex;
tables (age sex),(N colpctn);
run;
Then we get an N=17 total for both subtables - that's not what we want, we want N=18. Then we can do:
proc tabulate data=class;
class age sex/missing;
tables (age sex),(N colpctn);
run;
But that's not quite right either; I want F to have 8/18 = 44.44% and M 10/18 = 55.55%, not 42% and 53% with 5% allocated to the missing row.
The way I do this is to normalize the data. This means you get a dataset with 2 variables, varname and val, or whatever makes sense for your data, plus whatever identifier/demographic/whatnot variables you might have. val has to be character unless all of your values are numeric.
So for example here I normalize class with age and sex variables. I don't keep any identifiers, but you certainly could in your data, I imagine InsuranceStatus would be kept there if I understand what you're doing in that table. Once I have the normalized table, I just use those two variables, and carefully construct a denominator definition in proc tabulate to have the right basis for my pctn value. It's not quite the same as the single table before - the variable name is in its own column, not on top of the list of values - but honestly that looks better in my opinion.
data class_norm;
set class;
length val $2;
varname='age';
val=put(age,2. -l);
if not missing(age) then output;
varname='sex';
val=sex;
if not missing(sex) then output;
keep varname val;
run;
proc tabulate data=class_norm;
class varname val;
tables varname=' '*val=' ',n pctn<val>;
run;
If you want something better than this, you'll probably have to construct it in proc report. That gives you the most flexibility, but is the most onerous to program in also.
You can use ODS OUTPUT to get all of the PROC FREQ output to one dataset.
ods output onewayfreqs=class_freqs;
proc freq data=sashelp.class;
tables age sex;
run;
ods output close;
or
ods output crosstabfreqs=class_tabs;
proc freq data=sashelp.class;
tables sex*(height weight);
run;
ods output close;
Crosstabfreqs is the name of the cross-tab output, while one-way frequencies are onewayfreqs. You can use ods trace to find out the name if you forget it.
You may (probably will) still need to manipulate this dataset some to get the structure you want ultimately.

SAS Proc Report changing header style attributes for an across variable

A sample SAS Proc report code is below. I want to change the color of the header background and foreground for one value of the across variable. I used a compute block to change the column background to light gray using the absolute column references c4 and c5. How do I change the header style attributes for c4 and c5 to background=gainboro and foreground=black?
data test;
length name $ 10 disease $ 10.;
infile datalines dsd;
input name $ disease cases rate;
datalines;
State,Fever,4847,25.16
State,Cold,25632,131.5
State,Flu,103825,535.82
Lincoln,Fever,3920,44.17
Lincoln,Cold,16913,190.18
Lincoln,Flu,62965,735.39
Washington,Fever,827,56.56
Washington,Cold,3609,234.26
Washington,Flu,16610,1078.8
Kings,Fever,1026,37.45
Kings,Cold,4984,181.85
Kings,Flu,18388,694.33
Sussex,Fever,1411,78.38
Sussex,Cold,5515,300.46
Sussex,Flu,13881,813.11
Queens,Fever,616,26.03
Queens,Cold,2496,107.75
Queens,Flu,12518,558.09
;
run;
proc report data=test nowd headline headskip
STYLE(Header)={background=charcoal foreground=white }
style(column)={background=gray foreground=black}
style(report)=[rules=rows bordercolor=white];
columns (name disease,(cases rate));
define name/group order=data 'County' style(column)={background=lighttgray} style(header)=[bordertopcolor=gainsboro background=gainsboro foreground=black];
define disease/across '' order=data ;
define cases/'Cases' format=comma9. ;
define rate/'Rate' format=comma12.1 ;
compute cases;
call define('_c4_','style','style={background=lighttgray}');
call define('_c5_','style','style={background=lighttgray}');
endcomp;
run;
quit;
run;
You can use formats to do something close to what you're asking, but I'm not sure it's possible to do what you're asking - maybe Cynthia Zender on communities.sas.com might?
data test;
length name $ 10 disease $ 10.;
infile datalines dsd;
input name $ disease cases rate;
datalines;
State,Fever,4847,25.16
State,Cold,25632,131.5
State,Flu,103825,535.82
Lincoln,Fever,3920,44.17
Lincoln,Cold,16913,190.18
Lincoln,Flu,62965,735.39
Washington,Fever,827,56.56
Washington,Cold,3609,234.26
Washington,Flu,16610,1078.8
Kings,Fever,1026,37.45
Kings,Cold,4984,181.85
Kings,Flu,18388,694.33
Sussex,Fever,1411,78.38
Sussex,Cold,5515,300.46
Sussex,Flu,13881,813.11
Queens,Fever,616,26.03
Queens,Cold,2496,107.75
Queens,Flu,12518,558.09
;
run;
proc format;
value $headerbackf
'Cold' = 'gainsboro'
other = 'charcoal';
value $headerforef
'Cold' = 'black'
other = 'white'
;
quit;
proc report data=test nowd headline headskip
STYLE(Header)={background=charcoal foreground=white }
style(column)={background=gray foreground=black}
style(report)=[rules=rows bordercolor=white];
columns (name disease,(cases rate));
define name/group order=data 'County' style(column)={background=lightgray} style(header)=[bordertopcolor=gainsboro background=gainsboro foreground=black];
define disease/across '' order=data style(header)={background=$HEADERBACKF. foreground=$HEADERFOREF.};
define cases/'Cases' format=comma9. style(header)=inherit;
define rate/'Rate' format=comma12.1 ;
compute cases;
call define('_c4_','style','style={background=lighttgray}');
call define('_c5_','style','style={background=lighttgray}');
endcomp;
run;
That gets that top row formatted, but, doesn't actually get the row you're asking for. I'm not sure it's possible to.
It's possible as #ChrisJ noted that you might be able to do this with CSS styles and nth child selection. It's also possible you can't, unfortunately, due to how SAS does things with PROC REPORT - in particular, in PROC REPORT everything gets shoved inside <tr>s including the header rows, so nth-child and sibling selectors are impossible due to the headers not being children or siblings of each other.
Here's an example of a kludgey version of this, using sashelp.cars as an example.
CSS: (save in a .css file on your drive somewhere, say "c:\temp\test.css"):
#import 'base.css';
/* Red the second (really third) column header value */
.table thead tr:nth-child(2) th:nth-child(3) {
color:red
}
/* Yellow background for the mpg headers under Europe */
.table thead tr:nth-child(3) th:nth-child(4),
.table thead tr:nth-child(3) th:nth-child(5)
{
background-color:yellow
}
/* Green the mpg-city values */
.table thead tr:nth-child(3) th:nth-child(even) {
color:green
}
SAS program: (assumes the above-saved CSS file)
ods html file='example.html' cssstyle='c:\temp\test.css'(html);
ods pdf file='example.pdf' cssstyle='c:\temp\test.css'(print);
proc sort data=sashelp.cars out=cars; by origin;
run;
proc report data=cars nowd;
columns type origin,(mpg_city mpg_highway);
define origin/across;
define type/group;
define mpg_City / analysis mean;
define mpg_highway / analysis mean;
run;
ods _all_ close;
This is partially based on Kevin Smith's Unveiling the power of Cascading Style Sheets (CSS) in ODS.
Unfortunately, we can't in any way identify a cell that has "MPG(City)" in it except by knowing they'll be even column numbers. We similarly can't identify a cell under a "Europe" except by knowing what cells those will be.
Try adding a dummy column _c to the end of your columns statement, and add a define & compute to go with it.
Also, ensure your colour names are actually valid, e.g. lighttgray is invalid and will not work.
columns ... _c ;
define _c / computed noprint ;
compute _c ;
call define('_c4_','style','style={background=lightgray}');
call define('_c5_','style','style={background=lightgray}');
endcomp ;

Using Tabulate for 3-way table

I am trying to output a three way frequency table. I am able to do this (roughly) with proc freq, but would like the control for variable to be joined. I thought proc tabulate would be a good way to customize the output. Basically I want to fill in the cells with frequency, and then customize the percents at a later time. So, have count and column percent in each cell. Is that doable with proc tabulate?
Right now I have:
proc freq data=have;
table group*age*level / norow nopercent;
run;
that gives me e.g.:
What I want:
Here is the code I am using:
proc tabulate data=ex1;
class age level group;
var age;
table age='Age Category',
mean=' '*group=''*level=''*F=10./ RTS=13.;
run;
Thanks!
You can certainly get close to that. You can't really get in 'one' cell, it needs to write each thing out to a different cell, but theoretically with some complex formatting (probably using CSS) you could remove the borders.
You can't use VAR and CLASS together, but since you're just doing percents, you don't need to use MEAN - you should just use N and COLPCTN. If you're dealing with already summarized data, you may need to do this differently - if so then post an example of your dataset (but that wouldn't work in PROC FREQ either without a FREQ statement).
data have;
do _t = 1 to 100;
age = ceil(3*rand('Uniform'));
group = floor(2*rand('Uniform'));
level = floor(5*rand('Uniform'));
output;
end;
drop _t;
run;
proc tabulate data=have;
class age level group;
table age='Age Category',
group=''*level=''*(n='n' colpctn='p')*F=10./ RTS=13.;
run;
This puts N and P (n and column %) in separate adjacent cells inside a single level.