简体   繁体   中英

Modification of a SAS macro to print dichotomous variable information

I'm trying to modify the following SAS macro so that it includes includes percentages for the variable CHD when it is equal to both 0 and 1. Currently this macro is only set up to print out the results of baseline variables when the CHD (chronic heart disease) is equal to 1. I think the modification needs to occur within the data routfreq&i step but I'm not quite sure how to set it up. I would then also need an additional column to print out 'No Coronary Heart Disease * % (n)".

%macro categ(pred,i);
 proc freq data = heart;
 tables &pred * chd / chisq sparse outpct out = outfreq&i ;
 output out = stats&i chisq;
 run;
 proc sort data = outfreq&i;
 by &pred;
 run;


proc means data = outfreq&i noprint;
 where chd ne . and &pred ne .;
 by &pred;
 var COUNT;
 output out=moutfreq&i(keep=&pred total rename=(&pred=variable)) sum=total;
 run;

data routfreq&i(rename = (&pred = variable));
 set outfreq&i;
 length varname $20.;
if chd = 1 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;

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;


data _null_;
 set heart;
 call symput("fmt",vformat(&pred));
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;
 run;

data final&i;
 merge temp&i rstats&i;
 by index;
 length formats $20.;
 formats=put(variable,&fmt);
 if not first.index then do;
varname = " ";
p_value = " ";
 end;
 drop variable;
 run;
%mend;


%categ(gender,1);
%categ(smoke,2);
%categ(age_group,3);

%macro names(j,k,dataname);
 %do i=&j %to &k;
 &dataname&i
 %end;
 %mend names;

data categ_chd;
 set %names(1,3,final);
 label varname = "Demographic Characteristic"
 total = "Total"
pctnum = "Coronary Heart Disease * % (n)"
 p_value = "p-value * (2 sided)"
formats = "Category";
 run;

ods listing close;
ods rtf file = "c:\nesug\table1a.rtf" style = forNESUG;
proc report data = categ_chd nowd split = "*";
 column index varname formats total pctnum p_value;
 define index /group noprint;
compute before index;
 line ' ';
endcomp;
 define varname / order = data style(column) = [just=left] width = 40;
 define formats / order = data style(column) = [just=left];
 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 " NESUG PRESENTATION: TABLE 1A (NESUG 2004)";
 title2 " CROSSTABS OF CATEGORICAL VARIABLES WITH CORONARY HEART DISEASE OUTCOME";
run;
ods rtf close;
ods listing; 

Also, this code has the following error when it is run:

NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      1:2
NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
      3:111

I think this macro needs to be modified so that it doesn't crash when it runs with categorical/character variables.

The line

if chd = 1 and &pred ne .;

Is what is causing your output to only have CHD = "1".. You would change that to:

if chd = 1 and &pred ne .;

I do not understand your request for an additional column. Perhaps post an example of the current output and the output that you want?

As for the "errors" (actually notes as they do not cause the system to stop processing), the occur when a variable is automatically converted from numeric to character or vice-versa. It provides the code line where it is happening and how many times it happened. I prefer to eliminate these notes as often as possible to avoid unintended consequences of inappropriate coercion. To do this, you would make use of the PUT and INPUT functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM