Hello
The following code (macro) obtain multiple chi-square test .
I want to ask please about the proc report.
I want that the order of categories will be as following:
Smoking Status- non smoker, light, moderate,heavy,very heavy
blood pressure status-optimal,normal,high
weight status-underweight ,normal,overweight
How can I change the order of the categories?
%macro mmacro1(pred,i);
proc freq data=sashelp.heart;
tables &pred*Status/chisq sparse outpct out=outfreq&i ;
output out=stats&i chisq;
run;
proc sort data = outfreq&i;
by &pred;
run;
/*calculate Totals for each category of predict var*/
proc means data=outfreq&i noprint;
by &pred;
var COUNT;
output out=moutfreq&i(keep=&pred total rename=(&pred=variable)) sum=total;
run;
/*display the percentage and number of individuals in each group*/
data routfreq&i(rename = (&pred = variable));
set outfreq&i;
length varname $20.;
IF status='Dead' and &pred ne '';
rcount = put(count,8.);
rcount = "(" || trim(left(rcount)) || ")";
pctnum = round(pct_row,0.1) || " " || (rcount);
index = &i;
varname = vlabel(&pred);
keep &pred pctnum index varname;
run;
/*We wish to present precise p-values with significant values indicated.*/
data rstats&i;
set stats&i;
length p_value $8.;
if P_PCHI <= 0.05 then do;
p_value = round(P_PCHI,0.0001) || "*";
if P_PCHI < 0.0001 then p_value = "<0.0001" || "*";
end; else p_value = put(P_PCHI,8.4);
keep p_value index; index = &i;
run;
proc sort data = moutfreq&i;
by variable;
run;
proc sort data = routfreq&i;
by variable;
run;
data temp&i;
merge moutfreq&i routfreq&i;
by variable;
IF variable ne '' or variable ne . then output;
run;
data final&i;
merge temp&i rstats&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=&j %to &k;
&dataname&i %end;
%mend;
data categ_chdd;
length variable $100;
set %sset(1,&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;
@Ronein wrote:
Hello
The following code (macro) obtain multiple chi-square test .
I want to ask please about the proc report.
I want that the order of categories will be as following:
Smoking Status- non smoker, light, moderate,heavy,very heavy
blood pressure status-optimal,normal,high
weight status-underweight ,normal,overweight
How can I change the order of the categories?
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 (https://documentation.sas.com/?docsetId=proc&docsetTarget=p1upn25lbfo6mkn1wncu4dyh9q91.htm&docsetVer...)
In this example in column "Variable" there are categories from multiple origin variables
I want to perform multiple proc freq's and order the categories for each varaible by the proc format .
I get error
May anyone find the problem and provide a code
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 (> 25)'='Very Heavy Smoker(> 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);
proc freq data=heart order=formatted;
tables &pred*Status/chisq sparse outpct out=outfreq&i ;
output out=stats&i chisq;
format &pred $Fm&i.t;
run;
%mend;
%mmacro1(sex,1);
%mmacro1(Chol_Status,2);
%mmacro1(Weight_Status,3);
%mmacro1(Smoking_Status,4);
%mmacro1(BP_Status,5);
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.