BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lavienrose1
Calcite | Level 5

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
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
pau13rown
Lapis Lazuli | Level 10

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

Reeza
Super User
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

 

lavienrose1
Calcite | Level 5

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

 

Many many thanks!!

lavienrose1
Calcite | Level 5

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

Reeza
Super User

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


 

lavienrose1
Calcite | Level 5

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

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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