Help using Base SAS procedures

How to create mean and confidence interval macro + loop

Accepted Solution Solved
Reply
Highlighted
Occasional Contributor
Posts: 18
Accepted Solution

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?

 

Thanks in advance,

 

subjid3STL0STL6STL12STL18STL24nlr0nlr6nlr12nlr18nlr24TRT
1113137155..21.51.25..0
2225194188205.633.47.1
3336306.260.21.7.1.9.1

Accepted Solutions
Solution
a week ago
Super User
Posts: 23,776

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

Posted in reply to lavienrose1
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?

 

Thanks in advance,

 

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

 

View solution in original post


All Replies
Respected Advisor
Posts: 3,061

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

Posted in reply to lavienrose1

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

--
Paige Miller
Regular Contributor
Posts: 164

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

Posted in reply to lavienrose1

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

--------------
blog: papersandprograms.com
Solution
a week ago
Super User
Posts: 23,776

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

Posted in reply to lavienrose1
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?

 

Thanks in advance,

 

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??

 

 

Thanks in advance

Super User
Posts: 23,776

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

Posted in reply to lavienrose1

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??

 

 

Thanks in advance


 

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 165 views
  • 2 likes
  • 4 in conversation