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

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

Accepted Solutions
art297
Opal | Level 21

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

 

View solution in original post

7 REPLIES 7
Reeza
Super User

Can you generate some data we can work with to replicate and test your issue? 

benbuck
Obsidian | Level 7

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

art297
Opal | Level 21

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

 

benbuck
Obsidian | Level 7
Thank you for your help! This is exactly what I was looking for. I appreciate your effort and time.
Bedi
Calcite | Level 5

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.

Reeza
Super User

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.


 

Bedi
Calcite | Level 5

Hi..I tried it but than it is giving un-weighted response

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 1550 views
  • 3 likes
  • 4 in conversation