<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Multiple proc freq- proc report -order of categories in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiple-proc-freq-proc-report-order-of-categories/m-p/524034#M142454</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;The following code (macro) obtain multiple &amp;nbsp;chi-square test .&lt;/P&gt;
&lt;P&gt;I want to ask please about the proc report.&lt;/P&gt;
&lt;P&gt;I want that the order of categories will be as following:&lt;/P&gt;
&lt;P&gt;Smoking Status- non smoker, light, moderate,heavy,very heavy&lt;/P&gt;
&lt;P&gt;blood pressure status-optimal,normal,high&lt;/P&gt;
&lt;P&gt;weight status-underweight ,normal,overweight&lt;/P&gt;
&lt;P&gt;How can I change the order of the categories?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro mmacro1(pred,i); 
proc freq data=sashelp.heart;  
tables &amp;amp;pred*Status/chisq sparse outpct out=outfreq&amp;amp;i ;
output out=stats&amp;amp;i chisq;
run;

proc sort data = outfreq&amp;amp;i; 
by &amp;amp;pred; 
run;

/*calculate Totals for each category of predict var*/
proc means data=outfreq&amp;amp;i noprint; 
by &amp;amp;pred;  
var COUNT; 
output out=moutfreq&amp;amp;i(keep=&amp;amp;pred total rename=(&amp;amp;pred=variable)) sum=total;
run;

/*display  the  percentage  and  number  of  individuals  in  each  group*/
data routfreq&amp;amp;i(rename = (&amp;amp;pred = variable));   
set outfreq&amp;amp;i; 
length varname $20.; 
IF status='Dead' and &amp;amp;pred ne '';
rcount = put(count,8.);  
rcount = "(" || trim(left(rcount)) || ")"; 
pctnum = round(pct_row,0.1) || " " || (rcount);
index = &amp;amp;i; 
varname = vlabel(&amp;amp;pred); 
keep &amp;amp;pred pctnum index varname;
run;

/*We wish to present precise p-values with significant values indicated.*/
data rstats&amp;amp;i;  
set stats&amp;amp;i; 
length p_value $8.; 
if P_PCHI &amp;lt;= 0.05 then do;  
p_value = round(P_PCHI,0.0001) || "*"; 
if P_PCHI &amp;lt; 0.0001 then p_value = "&amp;lt;0.0001" || "*"; 
end;   else p_value = put(P_PCHI,8.4); 
keep p_value index;   index = &amp;amp;i;
run;


proc sort data = moutfreq&amp;amp;i;
by variable;
run;

proc sort data = routfreq&amp;amp;i; 
by variable; 
run;   

data temp&amp;amp;i;  
merge moutfreq&amp;amp;i routfreq&amp;amp;i; 
by variable; 
IF variable ne '' or variable ne . then output;
run; 

data final&amp;amp;i; 
merge temp&amp;amp;i rstats&amp;amp;i; 
by index;  
if not first.index then do; 
varname = " "; 
p_value = " "; 
end;     
run; 
%mend;
%mmacro1(sex,1);
%mmacro1(Chol_Status,2);
%mmacro1(Weight_Status,3);
%mmacro1(Smoking_Status,4);
%mmacro1(BP_Status,5);
%let number_Pred_Vars=5;

/*Combine data sets*/
%macro sset(j,k,dataname);
%do i=&amp;amp;j %to &amp;amp;k; 
&amp;amp;dataname&amp;amp;i   %end;
%mend; 



data categ_chdd; 
length variable $100;
set %sset(1,&amp;amp;number_Pred_Vars.,final);
label total="Freq" 
variable='Variable' 
p_value="p-value*(2 sided)" ;   
run; 



proc report data = categ_chdd nowd split = "*";
column index varname variable  total pctnum p_value;
define index /group noprint; 
compute before index; 
line ' '; 
endcomp; 
define varname / order = data style(column) = [just=left] width = 40; 
define variable / order = data style(column) = [just=left] width = 40; 
define total   / order = data style(column) = [just=center]; 
define pctnum  / order = data style(column) = [just=center]; 
define p_value / order = data style(column) = [just=center];  
title1 "TABLE 1A";  
title2 "CROSSTABS OF CATEGORICAL VARIABLES WITH CORONARY HEART DISEASE OUTCOME"; 
run; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 02 Jan 2019 06:55:59 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2019-01-02T06:55:59Z</dc:date>
    <item>
      <title>Multiple proc freq- proc report -order of categories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-proc-freq-proc-report-order-of-categories/m-p/524034#M142454</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;The following code (macro) obtain multiple &amp;nbsp;chi-square test .&lt;/P&gt;
&lt;P&gt;I want to ask please about the proc report.&lt;/P&gt;
&lt;P&gt;I want that the order of categories will be as following:&lt;/P&gt;
&lt;P&gt;Smoking Status- non smoker, light, moderate,heavy,very heavy&lt;/P&gt;
&lt;P&gt;blood pressure status-optimal,normal,high&lt;/P&gt;
&lt;P&gt;weight status-underweight ,normal,overweight&lt;/P&gt;
&lt;P&gt;How can I change the order of the categories?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro mmacro1(pred,i); 
proc freq data=sashelp.heart;  
tables &amp;amp;pred*Status/chisq sparse outpct out=outfreq&amp;amp;i ;
output out=stats&amp;amp;i chisq;
run;

proc sort data = outfreq&amp;amp;i; 
by &amp;amp;pred; 
run;

/*calculate Totals for each category of predict var*/
proc means data=outfreq&amp;amp;i noprint; 
by &amp;amp;pred;  
var COUNT; 
output out=moutfreq&amp;amp;i(keep=&amp;amp;pred total rename=(&amp;amp;pred=variable)) sum=total;
run;

/*display  the  percentage  and  number  of  individuals  in  each  group*/
data routfreq&amp;amp;i(rename = (&amp;amp;pred = variable));   
set outfreq&amp;amp;i; 
length varname $20.; 
IF status='Dead' and &amp;amp;pred ne '';
rcount = put(count,8.);  
rcount = "(" || trim(left(rcount)) || ")"; 
pctnum = round(pct_row,0.1) || " " || (rcount);
index = &amp;amp;i; 
varname = vlabel(&amp;amp;pred); 
keep &amp;amp;pred pctnum index varname;
run;

/*We wish to present precise p-values with significant values indicated.*/
data rstats&amp;amp;i;  
set stats&amp;amp;i; 
length p_value $8.; 
if P_PCHI &amp;lt;= 0.05 then do;  
p_value = round(P_PCHI,0.0001) || "*"; 
if P_PCHI &amp;lt; 0.0001 then p_value = "&amp;lt;0.0001" || "*"; 
end;   else p_value = put(P_PCHI,8.4); 
keep p_value index;   index = &amp;amp;i;
run;


proc sort data = moutfreq&amp;amp;i;
by variable;
run;

proc sort data = routfreq&amp;amp;i; 
by variable; 
run;   

data temp&amp;amp;i;  
merge moutfreq&amp;amp;i routfreq&amp;amp;i; 
by variable; 
IF variable ne '' or variable ne . then output;
run; 

data final&amp;amp;i; 
merge temp&amp;amp;i rstats&amp;amp;i; 
by index;  
if not first.index then do; 
varname = " "; 
p_value = " "; 
end;     
run; 
%mend;
%mmacro1(sex,1);
%mmacro1(Chol_Status,2);
%mmacro1(Weight_Status,3);
%mmacro1(Smoking_Status,4);
%mmacro1(BP_Status,5);
%let number_Pred_Vars=5;

/*Combine data sets*/
%macro sset(j,k,dataname);
%do i=&amp;amp;j %to &amp;amp;k; 
&amp;amp;dataname&amp;amp;i   %end;
%mend; 



data categ_chdd; 
length variable $100;
set %sset(1,&amp;amp;number_Pred_Vars.,final);
label total="Freq" 
variable='Variable' 
p_value="p-value*(2 sided)" ;   
run; 



proc report data = categ_chdd nowd split = "*";
column index varname variable  total pctnum p_value;
define index /group noprint; 
compute before index; 
line ' '; 
endcomp; 
define varname / order = data style(column) = [just=left] width = 40; 
define variable / order = data style(column) = [just=left] width = 40; 
define total   / order = data style(column) = [just=center]; 
define pctnum  / order = data style(column) = [just=center]; 
define p_value / order = data style(column) = [just=center];  
title1 "TABLE 1A";  
title2 "CROSSTABS OF CATEGORICAL VARIABLES WITH CORONARY HEART DISEASE OUTCOME"; 
run; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jan 2019 06:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-proc-freq-proc-report-order-of-categories/m-p/524034#M142454</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-01-02T06:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple proc freq- proc report -order of categories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-proc-freq-proc-report-order-of-categories/m-p/524035#M142455</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;The following code (macro) obtain multiple &amp;nbsp;chi-square test .&lt;/P&gt;
&lt;P&gt;I want to ask please about the proc report.&lt;/P&gt;
&lt;P&gt;I want that the order of categories will be as following:&lt;/P&gt;
&lt;P&gt;Smoking Status- non smoker, light, moderate,heavy,very heavy&lt;/P&gt;
&lt;P&gt;blood pressure status-optimal,normal,high&lt;/P&gt;
&lt;P&gt;weight status-underweight ,normal,overweight&lt;/P&gt;
&lt;P&gt;How can I change the order of the categories?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please post an extract of the dataset used by proc report. Normally you don't have text in categorical variables, but an id and the text is displayed by applying a format. If you define a format in the required order and use "notsorted" option, see doc of proc format for details (&lt;A href="https://documentation.sas.com/?docsetId=proc&amp;amp;docsetTarget=p1upn25lbfo6mkn1wncu4dyh9q91.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?docsetId=proc&amp;amp;docsetTarget=p1upn25lbfo6mkn1wncu4dyh9q91.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;)&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jan 2019 07:53:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-proc-freq-proc-report-order-of-categories/m-p/524035#M142455</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-01-02T07:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple proc freq- proc report -order of categories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-proc-freq-proc-report-order-of-categories/m-p/524038#M142457</link>
      <description>&lt;P&gt;In this example in column &amp;nbsp;"Variable" there are categories from multiple origin variables&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jan 2019 08:25:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-proc-freq-proc-report-order-of-categories/m-p/524038#M142457</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-01-02T08:25:11Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple proc freq- proc report -order of categories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-proc-freq-proc-report-order-of-categories/m-p/524044#M142461</link>
      <description>&lt;P&gt;I want to perform multiple proc freq's and order the categories for each varaible by the proc format .&lt;/P&gt;
&lt;P&gt;I get error&lt;/P&gt;
&lt;P&gt;May anyone find the problem and provide a code&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value $Fm1t ( notsorted)
'Male'='M'
'Female'='F';
Run;

proc format;
value $Fm2t ( notsorted)
'Desirable'='Desirable'
'Borderline'='Borderline'
'High'='High';
Run;

proc format;
value $Fm3t ( notsorted)
'Underweight'='Underweight'
'Normal'='Normal weight'
'Overweight'='Overweight';
Run;

proc format;
value $Fm4t ( notsorted)
'Non-smoker'='Non-smoker'
'Light (1-5)'='Light Smoker (1-5)'
'Moderate (6-15)'='Moderate Smoker(6-15)'
'Heavy (16-25)'='Heavy Smoker(16-25)'
'Very Heavy (&amp;gt; 25)'='Very Heavy Smoker(&amp;gt; 25)';
Run;

proc format;
value $Fm5t ( notsorted)
'Optimal'='Optimal Blood Pressure'
'Normal'='Normal Blood Pressure'
'High'='High Blood Pressure';
Run;

Data heart;
set sashelp.heart;
Format sex $Fm1t.
Chol_Status $Fm2t.
Weight_Status $Fm3t.
Smoking_Status $Fm4t.
BP_Status $Fm5t.;
Run;



%macro mmacro1(pred,i); &lt;BR /&gt;proc freq data=heart order=formatted; &lt;BR /&gt;tables &amp;amp;pred*Status/chisq sparse outpct out=outfreq&amp;amp;i ;&lt;BR /&gt;output out=stats&amp;amp;i chisq;&lt;BR /&gt;format &amp;amp;pred $Fm&amp;amp;i.t;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;%mmacro1(sex,1);&lt;BR /&gt;%mmacro1(Chol_Status,2);&lt;BR /&gt;%mmacro1(Weight_Status,3);&lt;BR /&gt;%mmacro1(Smoking_Status,4);&lt;BR /&gt;%mmacro1(BP_Status,5);
 &lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Jan 2019 09:11:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-proc-freq-proc-report-order-of-categories/m-p/524044#M142461</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-01-02T09:11:46Z</dc:date>
    </item>
  </channel>
</rss>

