Fluorite | Level 6

## turn proc univariate results into variable

Hello,

I am calculating confidence intervals for the mean of a few variables and I want to find a way to turn the CI results in to a variable on my final dataset.

For example:

I have a variable HAV_Complete, with a mean of 0.90817. I calculate the 95% CI using proc univariate:

proc univariate data=standard cibasic;

var hib_complete;

run;

In my output, the 95% CI is 0.893-0.922. Is there any way to dynamically turn this in to a variable in my final dataset? What I would like to see is:

record_id     hav_complete   hav_complete_mean    lower_limit    upper_limit

1                       0.903                    0.90817                 0.893              0.922

2                       1.00                      0.90817                 0.893              0.922

3                       0.87                      0.90817                 0.893              0.922

Any insight would be helpful. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: turn proc univariate results into variable

Switch to PROC SUMMARY

``````proc summary data=sashelp.class;
var height weight;
output out=ci mean= lclm= uclm=/autoname;
run;

data class;
if _n_=1 then set ci(drop=_type_ _freq_);
set sashelp.class;
run;``````
--
Paige Miller
10 REPLIES 10
Diamond | Level 26

## Re: turn proc univariate results into variable

You can obtain the confidence intervals as in this example:

``````ods output basicintervals=ci;
proc univariate data=sashelp.class cibasic;
var height;
run;``````

If you want them appended in columns to the original data

``````data class;
if _n_=1 then set ci(where=(parameter='Mean'));
set sashelp.class;
run;``````
--
Paige Miller
Fluorite | Level 6

## Re: turn proc univariate results into variable

Hello!

That definitely worked- thanks so much! Is there a way to include multiple variables (when I add more, it only calculates for the first variable)

example:

ods output basicintervals=ci;
proc univariate data=DATA.STANDARD_NO cibasic;
var HIB_COMPLETE THREEDOSECOMP FOURDOSECOMP RTV_COMPLETE

run;

This only gives HIB_Complete responses

Diamond | Level 26

## Re: turn proc univariate results into variable

Switch to PROC SUMMARY

``````proc summary data=sashelp.class;
var height weight;
output out=ci mean= lclm= uclm=/autoname;
run;

data class;
if _n_=1 then set ci(drop=_type_ _freq_);
set sashelp.class;
run;``````
--
Paige Miller
Tourmaline | Level 20

## Re: turn proc univariate results into variable

Sir @PaigeMiller  Cool solutions. I learned something new from OP and you in this thread. I had so much respect for Proc Univariate until this thread. Hmm, so it can't handle multiple vars?? Oh well what a shame!

So if i wanted to get all the statistics that Univariate could provide which perhaps not quite there in proc means/summary, would the only way be is fill in macro var with a var list and call one by one? I hope this is not the case? sighs

Diamond | Level 26

## Re: turn proc univariate results into variable

@novinosrin wrote:

Sir @PaigeMiller  Cool solutions. I learned something new from OP and you in this thread. I had so much respect for Proc Univariate until this thread. Hmm, so it can't handle multiple vars?? Oh well what a shame!

Completely unwarranted assumption. Of course UNIVARIATE can handle multiple variables. But to get the output table that the OP wanted, it was easier and faster (less programming time and less execution time) in PROC SUMMARY.

--
Paige Miller
Tourmaline | Level 20

## Re: turn proc univariate results into variable

Ah Okay. Hmm Thank you!

Super User

## Re: turn proc univariate results into variable

Whereas I always default to PROC MEANS that can generate at least two different ways of presenting data...still better than figuring out which R package to use...and which will work with the versions of your other packages 😞
Tourmaline | Level 20

## Re: turn proc univariate results into variable

What intrigued me to ask that was, I honestly wouldn't expect @PaigeMiller  to say "switch to" unless something really is a showstopper. In this thread that recommendation was pretty quick from the master though I found it rather unusual.

Diamond | Level 26

## Re: turn proc univariate results into variable

Actually I recommend people use a different PROC or different approach all the time. Use the right tool. It's a maxim!

--
Paige Miller
Super User

## Re: turn proc univariate results into variable

You have to process the data after the fact within a data step. I have a macro you can use to develop a table of characteristics if you'd like. Should get you close to what you want.

1. Run the following code with no changes. Should not see any output or any errors.

``````/*
This macro creates a table of charateristics for the variables listed.
It handles categorical, binary and continuous variables and produces and output dataset that can be further customized. No statistical information is
included in this analysis
*/

/*Parameters to be set:
dsetin - name of dataset to be analyzed
cont = macro variable list of variable names, ie cont=age weight height
cat=list of categorical variables ie cat=sex grade
bin=binary variables, such as smoking now, smoking ever
dsetout=name of output dataset
Run example at the end for a sample output dataset call sample_table_char in the work directory
*/

*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;``````

2. Call the macro:

`````` *Example of macro usage;
data sample;
set sashelp.class;
female=ifn( sex='F',1,0);
run;

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

@TPayne wrote:

Hello,

I am calculating confidence intervals for the mean of a few variables and I want to find a way to turn the CI results in to a variable on my final dataset.

For example:

I have a variable HAV_Complete, with a mean of 0.90817. I calculate the 95% CI using proc univariate:

proc univariate data=standard cibasic;

var hib_complete;

run;

In my output, the 95% CI is 0.893-0.922. Is there any way to dynamically turn this in to a variable in my final dataset? What I would like to see is:

record_id     hav_complete   hav_complete_mean    lower_limit    upper_limit

1                       0.903                    0.90817                 0.893              0.922

2                       1.00                      0.90817                 0.893              0.922

3                       0.87                      0.90817                 0.893              0.922

Any insight would be helpful. Thank you!

Discussion stats
• 10 replies
• 2229 views
• 5 likes
• 4 in conversation