DATA Step, Macro, Functions and more

Demographic chart

Reply
Super Contributor
Posts: 1,040

Demographic chart

Hi Team,

If a procedure variable  is  "Y" it can take the values of "b" or "h" in the type variable

Could someone help me produce these results


pt   Procedure   type

1     y                 b

  2    y                 h 

   3   y                b

   4    y              h

    5   y              h

   6     y             b       

  5     n            n/a

  6     n            n/a

sevaral hundreds of records

;

run

I WANT THE OUTPUT LIKE THIS with Percentages in braces for categorical variables. There are many variable like this sample shown below

                          Type(b/h)                  Total           Type n/a    TOTAL

                          b              h                                  

Age, years          n=?        n=?               n= ?            n=?                 

Mean (SD)           

Median           

Min-Max           13-45      45-60          

BMI

Mean (SD)           

Median           

Min-Max            

Diabetes           

Smoking        

current          40(40%)   60(60%)

Former          60(60%)    40(40%)     

Gender

Male

Female           

HTN

yes

no           

Super Contributor
Posts: 1,040

Re: Demographic chart

Hi Team,

I have a macro givven by one of our members. Its also for Demographics. But I am not knowing how to make the modifications to work for this kind of data.

I have used this macro before but not when the variable is further subdivided like the shown above...

Any help is highly appreciated

Regards

*options mprint symbolgen;
%macro table_char(dsetin, cont, cat, bin, dsetout);


*delete old dataset;

proc datasets nodetails nolist;
delete &dsetout;
quit;

/****************************************************************
Handle Categorical Variables
****************************************************************/

*loop through variable list;
%let i=1;
%do %while (%scan(&cat, &i, " ") ^=%str());
%let var=%scan(&cat, &i, " ");

*Get format for variable;
data _null_;
set &dsetin;
call symput("var_fmt", vformat(&var));
run;

proc freq data=&dsetin noprint;
table &var/missing out=tab_var;
run;

data temp1;
length categorical $200.; format categorical $200.;
length value $200.; format value $200.;

set tab_var;
percent=percent/100;
categorical=put(&var., &var_fmt.);
  if _n_=1 then do;
  value=put(count, 8.)||"("||compress(put(percent, percent8.1))||")";
  order=2;
  output;
  order=1;
  value='';
  categorical=propcase(vlabel(&var.));
   output;
end;
else do;
  order=2;
  value=put(count, 8.)||"("||compress(put(percent, percent8.1))||")";
  output;

end;

keep categorical value order;
run;

proc sort data=temp1 out=temp2 (drop=order); by order categorical; run;

proc append base=&dsetout data=temp2;
run;

*clean up;

proc datasets nodetails nolist;
delete tab_var temp1 temp2;
run; quit;

*Increment counter;
%let i=%eval(&i+1);
%end; *Categorical;

/****************************************************************
Handle Continuous Variables
****************************************************************/

%let i=1;
%do %while (%scan(&cont, &i, " ") ^=%str());
%let var=%scan(&cont, &i, " ");

proc means data=&dsetin (rename=&var=vn) noprint;
var vn;
output out=table_var n= nmiss= mean= min= max= std= median= p25= p75= p90=/autoname;
run;

*get label of variable for clean reporting;
data _null_;
set &dsetin;
call symput("var_label", vlabel(&var));
run;


data temp1;
length categorical $200.; format categorical $200.;
  format value $200.; length value $200.;

set table_var;

categorical="&var_label.";
value=.;
output;

categorical='Count(Missing)';
value=put(vn_n, 5.)||"("||compress(put(vn_nmiss, 5.))||")";
  output;

categorical='Mean (SD)';
value=put(vn_mean, 8.1)||"("||compress(put(vn_stddev, 8.1))||")";
output;

categorical='Median (IQR)';
value=put(vn_median, 8.1)||"("||compress(put(vn_p25, 8.1))||" - "||compress(put(vn_p75, 8.1))||")";
  output;

categorical='Range';
value=put(vn_min, 8.1)||" - "||compress(put(vn_max, 8.1));
output;

categorical='90th Percentile';
value=put(vn_p90, 8.1);
output;

 

keep categorical value;
run;


proc append base=&dsetout data=temp1;
run;

*clean up;

proc datasets nodetails nolist;
delete table_var temp1;
run; quit;

*Increment counter;
%let i=%eval(&i+1);
%end; *Continuous;

/*****************************************************************
Handle Binary Variables (only report 1s)
*****************************************************************/

%let i=1;
%do %while (%scan(&bin, &i, " ") ^=%str());
%let var=%scan(&bin, &i, " ");

proc freq data=&dsetin noprint;
table &var/missing out=tab_var;
run;

data tab_var;
set tab_var;
where &var=1;
run;

data temp1;
length categorical $200.; format categorical $200.;
length value $200.; format value $200.;

set tab_var;
percent=percent/100;
  if _n_=1 then do;
  value=put(count, 8.)||"("||compress(put(percent, percent8.1))||")";
  order=1;
  categorical=propcase(vlabel(&var.));
  output;
end;
keep categorical value;
run;


proc append base=&dsetout data=temp1;
run;

*clean up;

proc datasets nodetails nolist;
delete tab_var temp1;
run; quit;

*Increment counter;
%let i=%eval(&i+1);
%end;*Binary;


%mend table_char;

%table_char(sample, height weight age, sex age, female, sample_table_char);

Ask a Question
Discussion stats
  • 1 reply
  • 199 views
  • 0 likes
  • 1 in conversation