SAS Conditional row highlighting with ODS and Proc Print - sas

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.

Related

SAS how to color specific rows with ODS and proc report

I plan to color some rows with specific conditions (male and name starts with 'J') in a table class:
ods excel close;
ods excel file='c://class.xlsx';
data class; set sashelp.class;
if substr(name,1,1)='J' and sex='M' then tt=1;
run;
proc report data=class nowd;
columns sex height weight name age tt;
compute tt;
if tt=1 then call define(_row_, "style", "style=[backgroundcolor=yellow]");
endcomp;
run;
ods _all_ close;
it does not work, and just wonder how to fix it?
Since you do not define tt, the default use will be a analysis column. Thus tt will not be available in the compute, instead it would be tt.<statistic>.
Regardless, add a define statement to indicate tt is for display and the conditional styling will be applied correctly.
proc report data=class ;
columns sex height weight name age tt;
define tt / display; /* explicitly specify column usage */
compute tt;
if tt=1 then call define(_row_, "style", "style=[backgroundcolor=yellow]");
endcomp;
run;

Proc Report - Rows of two tables

I have to export some data in the format of two tables per row, with multiple rows. So far I have got as far as building a report with multiple tables stacked on top of each other, but with only one per row by using multiple proc report statements in a code statement as so:
proc report data = mydata; title 'My Title:'; run;
proc report data = mydata; title 'My Title:'; run;
proc report data = mydata; title 'My Title:'; run;
What do I need to add/amend so that I get two tables per row in my report?
Thanks
Some destinations support this directly in the ODS statement; like ODS PDF.
ods pdf file="test.pdf" columns=2 ;
proc print data=sashelp.class;
run;
proc freq data=sashelp.class;
tables age;
run;
ods pdf close;
However, HTML doesn't. For those you'll want to use ODS LAYOUT.
ods html file="test.html";
ods layout gridded
columns=2;
ods region;
proc print data=sashelp.class;
run;
ods region;
proc freq data=sashelp.class;
tables age;
run;
ods layout end;
ods html close;
See the ODS Layout Tip Sheet for more details.
ODS LAYOUT won't work with ODS EXCEL, sadly. You can use this macro to do something similar, if you prefer, or perhaps use PROC DOCUMENT to get the tables together, but I'm not sure exactly how that would work.
If you want more than one table in each column, then you can either have one ODS REGION per table (they'll end up being alternated l-r-l-r) or you can just add more to the two ODS REGIONs here if you don't need them to be gridded properly.
IE:
ods html file="test.html";
ods layout gridded
columns=2;
ods region;
proc print data=sashelp.class;
run;
ods region;
proc freq data=sashelp.class;
tables age;
run;
ods layout end;
ods html close;
Those just have two columns with 2 tables in each column, but they're not aligned.
ods html file="test.html";
ods layout gridded
columns=2;
ods region;
proc print data=sashelp.class;
run;
ods region;
proc freq data=sashelp.class;
tables age;
run;
ods region;
proc print data=sashelp.cars;
run;
ods region;
proc freq data=sashelp.cars;
tables origin;
run;
ods layout end;
ods html close;
This has a proper grid layout.
I resolved this in the end by tweaking the above as so:
ods layout gridded columns=2 rows=2;
ods region;
proc print data=sashelp.class;
run;
ods region;
proc freq data=sashelp.class;
tables age;
run;
ods region;
proc print data=sashelp.cars;
run;
ods region;
proc freq data=sashelp.cars;
tables origin;
run;
ods layout end;
Obviously, rows and columns will change depending on your needs. This produces a SAS report, that I then exported as a step in the project as a HTML file.
Thanks

How do I put conditions around Proc Freq statements in SAS?

I have the following statement
Proc Freq data =test;
tables gender;
run;
I want this to generate an output based on a condition applied to the gender variable. For example - if count of gender greater than 2 then output.
How can I do this in SAS?
Thanks
If you mean an output dataset, you can put a where clause directly in the output dataset options.
Proc Freq data =sashelp.class;
tables sex/out=sex_freq(where=(count>9));
run;
I'm not aware of how you can accomplish this only using proc freq but you can redirect the output to a data set and then print the results.
proc freq data=test;
tables gender / noprint out=tmp;
run;
proc print data=tmp;
where count > 2;
run;
Alternatively you could use proc summary, but this still requires two steps.
proc summary data=test nway;
class gender;
output out=tmp(where=(_freq_ > 2));
run;
proc print data=tmp;
run;

Hide 'by' group titles in ODS output

I'm trying to hide the titles for each by group in my ODS output. Code:
ods noproctitle;
ods html ;
proc sort data=sashelp.class out=class;
by sex;
run;
proc freq data=class;
by sex;
tables sex / list;
run;
Specifically, the titles I'm trying to hide are:
Sex=F
and
Sex=M
Look at the BYLINE/NOBYLINE option.
options nobyline;
Here's the documentation reference
http://support.sas.com/documentation/cdl/en/lesysoptsref/69799/HTML/default/viewer.htm#n1hr2wb7h5g6twn1gtt5r4zjsg39.htm

BY-variable not shown in Univariate Frequencies?

Is it usual that the Univariate Frequencies does not display the BY-variable while Univariate BasicMeasures does show the BY-variable?
In the example below I load in some data and want to show gas prices by zipcode. The output for PROC FREQ shows the BY-variable (zipcode) in the output as does the UNIVARIATE BasicMeasures. But the UNIVARIATE Frequencies is not showing the BY-variable in the output.
Am I doing something wrong? I've even set the templates to default, with the ODS PATH statement, in case the templates got messed up by other code (or other coders using same account).
DATA prices;
INPUT zipcode price;
DATALINES;
90066 3.10
90066 3.17
90066 3.26
98101 2.99
98101 3.06
98101 3.16
;
run;
proc sort;
by zipcode;
run;
ods path sashelp.tmplmst(read) ;
ods pdf file = "gasprices.pdf";
PROC FREQ data = prices;
tables price;
by zipcode;
run;
ods select Frequencies;
PROC UNIVARIATE data = prices freq;
var price;
by zipcode;
run;
ods select BasicMeasures;
PROC UNIVARIATE data = prices;
var price;
by zipcode;
run;
ods pdf close;
You can specify more than one object in the ODS SELECT, so you could pull both tables out of the same PROC FREQ like this:
ods pdf file = "gasprices.pdf";
ods select BasicMeasures Frequencies;
PROC UNIVARIATE data = prices freq;
var price;
by zipcode;
run;
ods pdf close;
I know that doesn't exactly solve your problem, but it looks to me like the BY-variable display just isn't properly linked to the PROC UNIVARIATE frequency tables (e.g., try ODS SELECT Moments - works fine). Might be worth reporting to SAS.