简体   繁体   中英

SAS proc tabulate

I am trying to create a table like this: Demog

Here's my code which is not working:

proc tabulate data=temp out = t1;
  class  age gender ethnic height TRT TREATGR;
  table  ethnic * (N) gender  * (N) age * (n mean median min max) height * (n mean median min max),  
      TREATGR*TRT*N;
run;

Here's the log:

127 proc tabulate data=temp out = t1; 128 class age gender ethnic height TRT TREATGR; 129 table ethnic * (N) gender * (N) age * (n mean median min max) height * (n mean median min 129! max), 130 TREATGR TRT N; 131 run;

ERROR: There are multiple statistics associated with a single table cell in the following nesting : ETHNIC * N * TREATGR * TRT * N. ERROR: There are multiple statistics associated with a single table cell in the following nesting : GENDER * N * TREATGR * TRT * N. ERROR: There are multiple statistics associated with a single table cell in the following nesting : AGE * N * TREATGR * TRT * N. ERROR: Statistic other than N was requested without analysis variable in the following nesting : AGE * Mean * TREATGR * TRT * N. ERROR: Statistic other than N was requested without analysis variable in the following nesting : AGE * Median * TREATGR * TRT * N. ERROR: Statistic other than N was requested without analysis variable in the following nesting : AGE * Min * TREATGR * TRT * N. ERROR: Statistic other than N was requested without analysis variable in the following nesting : AGE * Max * TREATGR * TRT * N. ERROR: There are multiple statistics associated with a single table cell in the following n esting : HEIGHT * N * TREATGR * TRT * N. ERROR: Statistic other than N was requested without analysis variable in the following nesting : HEIGHT * Mean * TREATGR * TRT * N. ERROR: Statistic other than N was requested without analysis variable in the following nesting : HEIGHT * Median * TREATGR * TRT * N. ERROR: Statistic other than N was requested without analysis variable in the following nesting : HEIGHT * Min * TREATGR * TRT * N. ERROR: Statistic other than N was requested without analysis variable in the following nesting : HEIGHT * Max * TREATGR * TRT * N. NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.T1 may be incomplete. When this step was stopped there were 0 observations and 0 variables. WARNING: Data set WORK.T1 was not replaced because this step was stopped. NOTE: PROCEDURE TABULATE used (Total process time): real time 0.01 seconds cpu time 0.01 seconds

But this works

proc tabulate data=temp out = t1;
  class  age gender ethnic height TRT TREATGR;
  table  ethnic  gender  age  height ,  
  TREATGR*TRT*N;
run;

But it lits all the age and heights.

CLASS variables are only used for 'cuts' of the data, ie, what defines the rows/columns. If you want mean/median/etc., ie the contents of the 'middle' of the table, then you have two choices:

  • Use n or pctn (or similar). Then you get effectively a 'dummy' variable that is just 1 for every row to add.
  • Add a var variable, which is an analysis variable and can be used for mean/median/etc.

Class variables can also be analysis variables, but they have to be declared as such (and it often doesn't do exactly what you want due to the interaction between class and analysis variables).

In your case, age and height are clearly not intended to be classification variables; they're analysis variables. You're not getting counts for each unique value, but summary statistics.

To your larger problem, you are missing something fundamental about PROC TABULATE tables that's too long to go into here; go read some tutorials. At minimum, you're confused about how rows, columns, and interactions work; all those * lead to a very different table than you're looking for. Space separates things concatenated together on the same axis, while comma separates rows from columns, and asterisk nests inside a dimension. So leaving aside the other issues, you need something like

table (race gender age height)*(n pctn),treatgr;

Order is (table/page),(row),(col).

To get the mean/median, I don't think you can do exactly that; but if you could, it would be something like

table (age*mean age*median age*n age*min age*max),treatgr;

An example of a table not too far from yours:

proc tabulate data=sashelp.class;
   var height weight;
   class sex age;
   table  age,sex*(n colpctn);
   table (height*n height*mean height*median height*min height*max)
        (weight*n weight*mean weight*median weight*min weight*max),sex;
run;

That's not perfect, and I suspect it's not possible to do exactly what you want in one TABULATE table (or two like above); you'd have to use PROC REPORT likely to get it to look exactly like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM