Hi all,
I am running the below code, attempting to create a table using Proc Tabulate. I am using categorical variables and am attempting to weight observations using the variable "discwt". However, I can't get the weighting to work. Regardless of what I do, my code only produces an unweighted table.
I am running this on SAS 9.4 TS 1M1.
Thanks!
proc tabulate data = work.redo_t1;
title 'Table 1a';
class cat_flag race ;
table race, (cat_flag)*(N colpctn);
var discwt / weight = discwt;
weight discwt;
run;
You want to use the FREQ option .. NOT the WEIGHT option. e.g.:
data redo_t1; input ID discwt cat_flag race; datalines; 01 5 1 1 02 5 1 2 03 5 1 1 04 5 1 2 05 5 1 1 06 5 3 2 07 5 3 1 08 5 3 2 09 5 3 1 10 5 3 2 ; run; proc tabulate data = work.redo_t1; title 'Table 1a'; class cat_flag race ; table race, (cat_flag)*(N colpctn); var discwt / weight = discwt; freq discwt; run;
Art, CEO, AnalystFinder.com
Can you generate some data we can work with to replicate and test your issue?
Here is code to produce sample data:
data redo_t1;
input ID discwt cat_flag race;
datalines;
01 5 1 1
02 5 1 2
03 5 1 1
04 5 1 2
05 5 1 1
06 5 3 2
07 5 3 1
08 5 3 2
09 5 3 1
10 5 3 2
;
run;
proc tabulate data = work.redo_t1;
title 'Table 1a';
class cat_flag race ;
table race, (cat_flag)*(N colpctn);
var discwt / weight = discwt;
weight discwt;
run;
and i'd like my table to look something like this:
Cat 1 Cat 3
Region 1 15 10
Region 3 10 15
and not
Cat 1 Cat 3
Region 1 3 2
Region 3 2 3
You want to use the FREQ option .. NOT the WEIGHT option. e.g.:
data redo_t1; input ID discwt cat_flag race; datalines; 01 5 1 1 02 5 1 2 03 5 1 1 04 5 1 2 05 5 1 1 06 5 3 2 07 5 3 1 08 5 3 2 09 5 3 1 10 5 3 2 ; run; proc tabulate data = work.redo_t1; title 'Table 1a'; class cat_flag race ; table race, (cat_flag)*(N colpctn); var discwt / weight = discwt; freq discwt; run;
Art, CEO, AnalystFinder.com
Your code works fine when the weights are integers , however if the weight are non-integers ,it doesn't give correct results
Below is the sample code
data redo_t1;
input ID discwt 3.1 cat_flag race;
datalines;
01 0.2 1 1
02 0.4 1 2
03 0.4 1 1
04 0.4 1 2
05 0.4 1 1
06 0.1 3 2
07 1.2 3 1
08 0.4 3 2
09 1.0 3 1
10 0.4 3 2
;
run;
proc tabulate data = work.redo_t1;
title 'Table 1a';
class cat_flag race ;
table race, (cat_flag)*(N colpctn);
var discwt / weight = discwt;
freq discwt;
run;
When I run the attached syntax only the integer part in my weights is used for calculation.
Can you please suggest how to use the solution with the decimal weights.
Try the WEIGHT statement in that case instead of FREQ.
@Bedi wrote:
Your code works fine when the weights are integers , however if the weight are non-integers ,it doesn't give correct results
Below is the sample code
data redo_t1; input ID discwt 3.1 cat_flag race; datalines; 01 0.2 1 1 02 0.4 1 2 03 0.4 1 1 04 0.4 1 2 05 0.4 1 1 06 0.1 3 2 07 1.2 3 1 08 0.4 3 2 09 1.0 3 1 10 0.4 3 2 ; run; proc tabulate data = work.redo_t1; title 'Table 1a'; class cat_flag race ; table race, (cat_flag)*(N colpctn); var discwt / weight = discwt; freq discwt; run;
When I run the attached syntax only the integer part in my weights is used for calculation.
Can you please suggest how to use the solution with the decimal weights.
Hi..I tried it but than it is giving un-weighted response
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.