BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Crystal_F
Quartz | Level 8

Hi everyone,

 

I'm having a hard time editing the following code to generate weighted N and column percentage using proc tabulate. My dataset is survey design but only has weights and no strata nor cluster. My expected result is to have weighted frequency of lab test outcome by condition and race group. 

Using survey frequency is less flexible compared to tabulate in term of table format.

Any help on rewriting the code is greatly appreciated!

proc tabulate data=test(where=(cohort=1)) noseps  ;
class flag_A1C9_lst  Cat_raceeth obesity Hyperlipidemia Hypertension ;
table flag_A1C9_lst,(all  obesity*Cat_raceeth 
					 Hyperlipidemia*Cat_raceeth 
					 Hypertension*Cat_raceeth)*(n*f=comma8. colPctN*f=4.1);				 
run; 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

What do you get when you add a Weight statement to your proc tabulate:

 

proc tabulate data=test(where=(cohort=1)) noseps  ;
class flag_A1C9_lst  Cat_raceeth obesity Hyperlipidemia Hypertension ;
weight FWGT_2019; table flag_A1C9_lst,(all obesity*Cat_raceeth Hyperlipidemia*Cat_raceeth Hypertension*Cat_raceeth)*(n*f=comma8. colPctN*f=4.1); run;

If your variable FWGT_2019 is actually a count variable, such as from summarizing data, you may actually want a FREQ statement instead of weight.

View solution in original post

6 REPLIES 6
mkeintz
PROC Star

And what is the weight variable?

 

Please show some sample data, in the form of a working data step.

And the expected result.

--------------------------
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

--------------------------
Crystal_F
Quartz | Level 8
data sample;
  input uniqueid $ flag_A1C9_lst Cat_raceeth obesity Hyperlipidemia Hypertension cohort FWGT_2019
        ;
datalines;
1 1 1 1 1 0 1 1473
2 0 2 0 1 1 0 2296
3 . 3 1 0 1 1 7484
;
run;

The expected output looks like below:

table example.png

Thank you for your help!

ballardw
Super User

What do you get when you add a Weight statement to your proc tabulate:

 

proc tabulate data=test(where=(cohort=1)) noseps  ;
class flag_A1C9_lst  Cat_raceeth obesity Hyperlipidemia Hypertension ;
weight FWGT_2019; table flag_A1C9_lst,(all obesity*Cat_raceeth Hyperlipidemia*Cat_raceeth Hypertension*Cat_raceeth)*(n*f=comma8. colPctN*f=4.1); run;

If your variable FWGT_2019 is actually a count variable, such as from summarizing data, you may actually want a FREQ statement instead of weight.

Crystal_F
Quartz | Level 8
Thank you for your reply! Adding weight statement alone couldn't generate weighted N and weighted column percentage from 'n*f=comma8. colPctN*f=4.1' in SAS Studio.
Reeza
Super User

FREQ seems to work?

data sample;
  input uniqueid $ flag_A1C9_lst Cat_raceeth obesity Hyperlipidemia Hypertension cohort FWGT_2019
        ;
datalines;
1 1 1 1 1 0 1 1473
2 0 2 0 1 1 0 2296
3 . 3 1 0 1 1 7484
;
run;

 

proc tabulate data=sample(where=(cohort=1)) noseps  ;
class flag_A1C9_lst  Cat_raceeth obesity Hyperlipidemia Hypertension / missing ;
freq FWGT_2019;
table flag_A1C9_lst,(all  obesity*Cat_raceeth 
					 Hyperlipidemia*Cat_raceeth 
					 Hypertension*Cat_raceeth)*(n*f=comma8. colPctN*f=4.1);				 
run; 

 

 

 

Crystal_F
Quartz | Level 8

Yes, using freq statement instead really works. Thank you @ballardw @Reeza for your great suggestions!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2295 views
  • 4 likes
  • 4 in conversation