Help using Base SAS procedures

proc report - binary variable

Accepted Solution Solved
Reply
Contributor L_L
Contributor
Posts: 56
Accepted Solution

proc report - binary variable

Dear all

I’ve a dataset with a binary variable named ‘Healthy’ (values Yes, No) and a stratification variable named “Treatment”  (values A, B).

To report the percentage of Healthy*Treatment I’m using a PROC TABULATE and the output is:

                      Treatment                   Total

Healthy            A                  B          

                  N    %            N    %           N    %

Yes            xx    xx.x%     xx   xx.x        xx   xx.x

No             xx    xx.x%     xx   xx.x        xx   xx.x

Total         xx   xx.x%       xx   xx.x%     xx   xx.x%

I would like to report only the occurrence Helthy=Yes (N and %) with a PROC REPORT. Is it possible? How can I do it?

Thanks in advance for any help


Accepted Solutions
Solution
‎09-06-2011 06:41 AM
Respected Advisor
Posts: 3,777

Re: proc report - binary variable

I thought that might be your next question. :smileyshocked:  You may need to transpose your data to get this question response format but that would best suite PROC REPORT.  You will also need to address the order of the questions, as I'm sure that will be an issue. 

proc plan seed=340564315;

   factors subjid=40 ordered trt=1 of 2 question=2 ordered resp=1 of 2 / noprint;

   output out=binary question cvals=('Healthy' 'Emerg') resp nvals=(0 1);

   run;

   quit;

proc print;

   run;

proc report data=binary nowd list headline;

   columns question trt, (resp=hsum resp=hmean) ('Total' '--'  resp=hsum2 resp=hmean2);

   define question / group width=10;

   define hsum / sum '--' 'N' format=5.;

   define hmean / mean '--' '%' format=percentn7.1;

   define hsum2  / sum 'N' format=5.;

   define hmean2 / mean '%' format=percentn7.1;

   define trt / across format=1. 'Treatment' '--';

   run;

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: proc report - binary variable

For binary (0,1) variable SUM=N of 1s.  Mean=proportion of 1s.  This code may be helpful.

proc plan seed=340564315;

   factors subjid=40 ordered healthy=1 of 2 / noprint;

   treatments trt=2;

   output out=binary healthy nvals=(0 1);

   run;

   quit;

proc print;

   run;

proc report data=binary nowd list headline;

   columns trt, (healthy=hsum healthy=hmean) ('Total' '--'  healthy=hsum2 healthy=hmean2);

   define hsum / sum '--' 'N' format=5.;

   define hmean / mean '--' '%' format=percent7.1;

   define hsum2  / sum 'N' format=5.;

   define hmean2 / mean '%' format=percent7.1;

   define trt / across format=1. 'Treatment' '--';

   run;

Super User
Posts: 9,662

Re: proc report - binary variable

You can do it by using data set option where=:

proc tabulate data=binary(where=(Helthy='Yes'));

proc report also can do it easily. If you can post some sample data.

Or Null's code maybe is what you want.

Ksharp

Contributor L_L
Contributor
Posts: 56

proc report - binary variable

I' m sorry I was not accurate

Sample data:

data demog;  infile cards;

input patid $1-3 treat $5 healthy $7-9 Emerg $11-13;

cards;

001 A Yes No

002 B No  No

003 A No  Yes

004 B Yes Yes

005 A No  Yes

;

run;

I would like to report only the occurrence Helthy=Yes (N and %) and Emerg=Yes (N and %) in the same table.. I think by proc tabulate is not possible. What about using the proc report?Thank in advance

Super User
Posts: 9,662

proc report - binary variable

What is your ouput destionation? listing PDF?

Maybe you can set the color of font white (as the same with your background).

Such as style={foreground=white}

If you do not want this, then It will be more complicated.

data demog; 
infile datalines;
input patid  $  treat $ 5 healthy $ 7-9 Emerg $ 11-13;
datalines;
001 A Yes No
002 B No  No
003 A No  Yes
004 B Yes Yes
005 A No  Yes
;
run;
options missing=' ';
proc report data=demog nowd out=test;
column healthy _heal treat,(n pctn _n _pctn) ;
define healthy /group noprint;
define _heal/computed "Healthy";
define treat/across ;
define n /noprint;
define pctn/noprint;
define _n/computed format=best8.;
define _pctn/computed format=percent8.2;
compute _heal/character length=4 ;
 if healthy='Yes' then _heal=healthy;
  else _heal=' ';
endcomp;
compute _n;
 if healthy='Yes' then do;_c5_=_c3_; _c6_=_c4_;end;
  else do; _c5_=.; _c4_=.;end;
endcomp;
compute _pctn;
   if healthy='Yes' then do;_c9_=_c7_; _c10_=_c8_;end;
  else do; _c9_=.; _c10_=.;end;
endcomp;
 

run;

Ksharp

Contributor L_L
Contributor
Posts: 56

proc report - binary variable

Thank you so much

Destination output is rtf file

The code is perfect for one variable (ex. Healthy).

Is it possible to have a table with two or more variable as the table below?

                                   

Healthy                   A                              B

Yes                      xx  (xx.x%)                 xx (xx.x%)

Emerg

Yes                     xx (xx.x%)                   xx (xx.x%)

Solution
‎09-06-2011 06:41 AM
Respected Advisor
Posts: 3,777

Re: proc report - binary variable

I thought that might be your next question. :smileyshocked:  You may need to transpose your data to get this question response format but that would best suite PROC REPORT.  You will also need to address the order of the questions, as I'm sure that will be an issue. 

proc plan seed=340564315;

   factors subjid=40 ordered trt=1 of 2 question=2 ordered resp=1 of 2 / noprint;

   output out=binary question cvals=('Healthy' 'Emerg') resp nvals=(0 1);

   run;

   quit;

proc print;

   run;

proc report data=binary nowd list headline;

   columns question trt, (resp=hsum resp=hmean) ('Total' '--'  resp=hsum2 resp=hmean2);

   define question / group width=10;

   define hsum / sum '--' 'N' format=5.;

   define hmean / mean '--' '%' format=percentn7.1;

   define hsum2  / sum 'N' format=5.;

   define hmean2 / mean '%' format=percentn7.1;

   define trt / across format=1. 'Treatment' '--';

   run;

Contributor L_L
Contributor
Posts: 56

Re: proc report - binary variable

Dear data _null_

I' m sorry but I'm new in proc report..

hereafter you'll find my code (thanks to you):

data demog;

infile datalines;

input patid  $  treat $ 5 healthy $ 7-9 Emerg $ 11-13;

datalines;

001 A Yes No

002 B No  No

003 A No  Yes

004 B Yes Yes

005 A No  Yes

;

run;

data binary;
set demog;
keep patid treat healthy Emerg;
array HH [2] healthy Emerg;
do i=1 to 2;
  if HH ='Yes' then HH= 1;
  if HH ='No' then HH= 0;
end;
run;

data bin (drop=healthy Emerg);
set binary;
array HH [2] healthy Emerg;
do i=1 to 2;
hea = HH ;
output;
end;
run;

data bin (drop=hea);
set bin;
    HEAL=hea*1;
run;

proc format;
value hea       1 = 'Healthy'
                         2 = 'Emerg'     
;
run;

proc report data=bin nowd list headline;
   columns i treat,  (heal heal=h_heal);
   define i / group width=10 ' ' format= hea. order=freq left;
   define heal / sum 'N';
   define h_heal / mean '%' format=percentn7.1;
   define treat / across format=$tipo. 'Patients' '--';
  
  
run;

I would like to have a column with the 'Total' ..what's missing in the code?

Thanks very much in advanced

Respected Advisor
Posts: 3,777

Re: proc report - binary variable

I think this address your questions.

data demog;

   infile datalines;

   input patid  $  treat $ 5 Healthy $ 7-9 Emerg $ 11-13;

   datalines;

001 A Yes No

002 B No  No

003 A No  Yes

004 B Yes Yes

005 A No  Yes

;;;;

   run;

proc freq;

   tables (healthy emerg)*treat;

   run;

proc transpose data=Demog out=binary;

   by patid treat;

   var Healthy Emerg;

   run;

data binary;

   set binary;

   resp = ifN(col1 eq: 'Y',1,0);

   run;

proc format;

   value $hea(notsorted)

      'Healthy' = 'Healthy'

      'Emerg'   = 'Emerg'  

   ;

   run;

proc report data=binary nowd list headline;

   columns _name_ treat,(resp resp=respPct) ('Total' '--' resp resp=respPct);

   define _name_ / group width=10 ' ' format= $hea. order=data left preloadfmt;

   define resp / sum 'N';

   define respPct / mean '%' format=percentn7.1;

   define treat / across /*format=$tipo.*/ 'Patients' '--';

   run;

Contributor L_L
Contributor
Posts: 56

Re: proc report - binary variable

Many thanks. It works well

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 239 views
  • 6 likes
  • 3 in conversation