DATA Step, Macro, Functions and more

how to calculate stat values and create a dataset

Reply
Super Contributor
Posts: 272

how to calculate stat values and create a dataset

Dear,

 

I need help in my code to get the output i need to create a dataset for my table. Please help. Thank you.

 

I am calculating n, mean, median, range by variables test,visit,trt

 

data one;
input id trt $ visit $ test $ bas val;
datalines;
1 a wk1 pul 2 3
2 b wk1 pul 3 6
3 c wk1 pul 4 9
4 a wk1 pul 3 7
5 b wk1 pul 3 7
6 c wk1 pul 3 7
1 a wk1 bp 10 12
2 b wk1 bp 12 14
3 c wk1 bp 14 16
4 a wk1 bp 12 14
5 b wk1 bp 14 16
6 c wk1 bp 16 18
1 a wk2 pul 2 3
2 b wk2 pul 3 6
3 c wk2 pul 4 9
4 a wk2 pul 3 7
5 b wk2 pul 3 7
6 c wk2 pul 3 7
1 a wk2 bp 10 12
2 b wk2 bp 12 14
3 c wk2 bp 14 16
4 a wk2 bp 12 14
5 b wk2 bp 14 16
6 c wk2 bp 16 18
;
proc means data=one n mean sum range median min max;
var val;
class test visit trta;
output out=two(keep=_stat_ trt visi test val where=(test='pul' and trt ^='' and visi ^=''));
run;

dataset output needed:

 

visit   trt   n   mean median min    max   SD

wk1    a    2     5         5        3      7      2.82    

wk1    b    2     6.5     6.5       6      7      0.7

wk1     c    2      8       8          7      9     1.4

wk2    a    2     5         5        3      7       2.82

wk2    b    2     6.5     6.5       6      7      0.7

wk2     c    2      8       8          7      9     1.4

   

 

Table output needed;

wk1(pul)

                                           a                                     b                                    c

n                                          2                                     2                                   2

mean(SD)                              5(2.82)                            6.5(0.7)                           8(1.4)

median                                  5                                     6.5                                 8

range(min,max)                      3, 7                                 6, 7                                7, 9

 

Valued Guide
Posts: 797

Re: how to calculate stat values and create a dataset

[ Edited ]

You got started correctly using PROC MEANS (or its alias proc summary).

 

But ...

  1. You can use the NWAY option in the proc summary statement, which eliminates the need for
        where=(test='pul' and trt ^='' and visi ^='') on your output statement.
  2. Then sort the data by visi test _stat_.   This will provide proper data order for the proc transpose to follow.
  3. This is the magic.   A proc transpose ..    also with a "by visi test _stat_".   Don't forget to have an ID statement naming the variable whose values will become the new variable names.

 

Here's an analog using sashelp.shoes:

 

proc summary data=sashelp.shoes n mean sum range median min max  nway;
  class region subsidiary product;
  var sales;
  output out=two (drop=_type_ _freq_);
run;

proc sort;
  by region subsidiary _stat_ product;
run;

proc transpose data=two out=want (drop=_name_ _label_);
  by region subsidiary  _stat_;
  id product; /* Assign the product as the new variable name */
run;

 

Super Contributor
Posts: 272

Re: how to calculate stat values and create a dataset

Thank you very much for the code. But why the median and range values are not calculated with the code. Do you any suggestion

Super User
Posts: 17,820

Re: how to calculate stat values and create a dataset

STACKODS

 

proc means data=sashelp.shoes n mean sum range median min max stackods nway;
  class region subsidiary product;
  var sales;
  ods output summary=want;
run;

It didnt create the values because the statistics in the PROC MEANS statement control the display output, but you're capturing a different output when you use OUTPUT statement instead of ODS statement.

 

 

Super User
Posts: 17,820

Re: how to calculate stat values and create a dataset

Look at Cynthia Zender's paper on Creating Complex Reports. It has exact examples with code on how to accomplish this task. 

 

 

Super User
Posts: 17,820

Re: how to calculate stat values and create a dataset

Use STACKODS option for your table, rather than a means and then transpose. See the documentation in PROC MEANS for an example or this SAS Note.

 

http://support.sas.com/kb/46/427.html

 

 

Ask a Question
Discussion stats
  • 5 replies
  • 128 views
  • 4 likes
  • 3 in conversation