BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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

 

5 REPLIES 5
mkeintz
PROC Star

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;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
knveraraju91
Barite | Level 11

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

Reeza
Super User

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.

 

 

Reeza
Super User

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

 

 

Reeza
Super User

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

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 933 views
  • 4 likes
  • 3 in conversation