## How to create mean and confidence interval macro + loop

Solved
Occasional Contributor
Posts: 18

# How to create mean and confidence interval macro + loop

Hi,

I have the dataset as below (at the bottom) - I have put the first 3 patients but my total dataset is over 1000 .

I would like to calculate the mean for STL0-STL24 and 95% CI for each subjid3. I would like to calculate mean for nlr0 to nlr24 and 95%CI for each subjid3.

How do I create a macro and loop to achieve this?

 subjid3 STL0 STL6 STL12 STL18 STL24 nlr0 nlr6 nlr12 nlr18 nlr24 TRT 1 113 137 155 . . 2 1.5 1.25 . . 0 2 225 194 188 205 . 6 3 3.4 7 . 1 3 336 306 . 260 . 2 1.7 . 1.9 . 1

Accepted Solutions
Solution
Wednesday
Super User
Posts: 23,714

## Re: How to create mean and confidence interval macro + loop

``````data want;
set have;

mean_stl = mean(of stl0--stl24);
std_STL = std(of stl0--stl24);
CILB_STL = mean_stl - 1.96*std_stl;
...etc.

run;``````

No loops or arrays. SAS loops through each line in the data set automatically anyways. You can use the inline functions such as MEAN, STD to get the values. See a quick (untested) example above. If you're still having issues please post back and include your full code and log.

@lavienrose1 wrote:

Hi,

I have the dataset as below (at the bottom) - I have put the first 3 patients but my total dataset is over 1000 .

I would like to calculate the mean for STL0-STL24 and 95% CI for each subjid3. I would like to calculate mean for nlr0 to nlr24 and 95%CI for each subjid3.

How do I create a macro and loop to achieve this?

 subjid3 STL0 STL6 STL12 STL18 STL24 nlr0 nlr6 nlr12 nlr18 nlr24 TRT 1 113 137 155 . . 2 1.5 1.25 . . 0 2 225 194 188 205 . 6 3 3.4 7 . 1 3 336 306 . 260 . 2 1.7 . 1.9 . 1

All Replies
Posts: 3,011

## Re: How to create mean and confidence interval macro + loop

No macros or loops needed. Look up the MEAN and STD functions in SAS help.

--
Paige Miller
Regular Contributor
Posts: 159

## Re: How to create mean and confidence interval macro + loop

the mean for each subject? assuming that's not what you meant, see eg proc univariate

--------------
blog: papersandprograms.com
Solution
Wednesday
Super User
Posts: 23,714

## Re: How to create mean and confidence interval macro + loop

``````data want;
set have;

mean_stl = mean(of stl0--stl24);
std_STL = std(of stl0--stl24);
CILB_STL = mean_stl - 1.96*std_stl;
...etc.

run;``````

No loops or arrays. SAS loops through each line in the data set automatically anyways. You can use the inline functions such as MEAN, STD to get the values. See a quick (untested) example above. If you're still having issues please post back and include your full code and log.

@lavienrose1 wrote:

Hi,

I have the dataset as below (at the bottom) - I have put the first 3 patients but my total dataset is over 1000 .

I would like to calculate the mean for STL0-STL24 and 95% CI for each subjid3. I would like to calculate mean for nlr0 to nlr24 and 95%CI for each subjid3.

How do I create a macro and loop to achieve this?

 subjid3 STL0 STL6 STL12 STL18 STL24 nlr0 nlr6 nlr12 nlr18 nlr24 TRT 1 113 137 155 . . 2 1.5 1.25 . . 0 2 225 194 188 205 . 6 3 3.4 7 . 1 3 336 306 . 260 . 2 1.7 . 1.9 . 1

Occasional Contributor
Posts: 18

## Re: How to create mean and confidence interval macro + loop

Thanks for your help - very much appreciated and solved my problem perfectly! (and more simply!)

Many many thanks!!

Occasional Contributor
Posts: 18

## Re: How to create mean and confidence interval macro + loop

Hi Reeza,

Can I ask, if I wanted to find the mean by column ie STL0 (113 + 225 + 336 /3) for each of the columns, stratified by TRT....how do I go about that??

Super User
Posts: 23,714

## Re: How to create mean and confidence interval macro + loop

That is best done using a summary procedure such as PROC MEANS.

You can run the example below, which summarizes the data using two different output formats.

``````*Create summary statistics for a dataset by a 'grouping' variable and store it in a dataset;

*Generate sample fake data;
data have;
input ID          feature1         feature2         feature3;
cards;
1               7.72               5.43              4.35
1               5.54               2.25              8.22
1               4.43               6.75              2.22
1               3.22               3.21              7.31
2               6.72               2.86              6.11
2               5.89               4.25              5.25
2               3.43               7.30              8.21
2               1.22               3.55              6.55

;
run;

*Create summary data;
proc means data=have noprint;
by id;
var feature1-feature3;
output out=want median= var= mean= /autoname;
run;

*Show for display;
proc print data=want;
run;

*First done here:https://communities.sas.com/t5/General-SAS-Programming/Getting-creating-new-summary-variables-longitudinal-data/m-p/347940/highlight/false#M44842;
*Another way to present data is as follows;

proc means data=have stackods nway n min max mean median std p5 p95;
by id;
var feature1-feature3;
ods output summary=want2;
run;

*Show for display;
proc print data=want2;
run;``````

@lavienrose1 wrote:

Hi Reeza,

Can I ask, if I wanted to find the mean by column ie STL0 (113 + 225 + 336 /3) for each of the columns, stratified by TRT....how do I go about that??

Occasional Contributor
Posts: 18

## Re: How to create mean and confidence interval macro + loop

Thanks very much Reeza - you've been wonderfully helpful. Much appreciated.

☑ This topic is solved.