SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sylvanaw
Calcite | Level 5

Hi,

 

Is there a way to to print both the variable values AND the value labels in a frequency tabel?

When I use proc freq, at the moment only the value labels are displayed:

Age

up to 16 years: 12 %

17 to 30 years: 14 %

30 to 50 years: 36 %

50 to 65 years: 19 %

65 years and older: 18 %

missing: 18 %

 

I have defined them for my numeric variable Age with proc format and pplied them in a data step.

 

How can I cinfigure the system options, so that the output table looks like this:

1: up to 16 years: 12 %

2: 17 to 30 years: 14 %

3: 30 to 50 years: 36 %

4: 50 to 65 years: 19 %

5: 65 years and older: 18 %

9: missing: 1 %

(displaying both values and Labels)

 

I would appreciate any help.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

While you might need to do a little juggling, here is a way that is less complex and involves little  juggling.

 

For the same of discussion, let's say the variable AGE has a format AGECAT. that translates it into the formatted labels.  

 

Begin by creating a data set holding the percents:

 

proc freq data=have;

tables age / noprint out=age_counts (keep=age percent)

run;

 

The table doesn't print, but you can easily use the output data set:

 

data want;

set age_counts;

category = age;

run;

 

This creates a second variable with the same value.  But when printing, one gets formatted and one doesn't:

 

proc print data=want;

var category age percent;

format category;

format age agecat.;

run;

View solution in original post

7 REPLIES 7
Reeza
Super User
Any particular reason? To force a sort order? If so, there are other ways 🙂
sylvanaw
Calcite | Level 5

He Reeza,

 

No, I would rather like to check if all values and labels are assigned correctly as stated in the questionnaire and see if the frequencies of each category behave normal at the same time (in other words checking the correctness and plausibility of the data). Age is one of 181 variables that I would like to check.

So I would like to know how to change what is displayed in all the frequency tables by default if that exists.

Reeza
Super User

Ah, well in that case I don't think there's an easy way to check that. You can check the formats definitions if you wanted fairly easily, but it wouldn't have the counts.

 


@sylvanaw wrote:

He Reeza,

 

No, I would rather like to check if all values and labels are assigned correctly as stated in the questionnaire and see if the frequencies of each category behave normal at the same time (in other words checking the correctness and plausibility of the data). Age is one of 181 variables that I would like to check.

So I would like to know how to change what is displayed in all the frequency tables by default if that exists.


 

PaigeMiller
Diamond | Level 26

@sylvanaw wrote:

He Reeza,

 

No, I would rather like to check if all values and labels are assigned correctly as stated in the questionnaire and see if the frequencies of each category behave normal at the same time (in other words checking the correctness and plausibility of the data). Age is one of 181 variables that I would like to check.

So I would like to know how to change what is displayed in all the frequency tables by default if that exists.


Frequency tables is not the way to check this. PROC CONTENTS will provide the variable name and associated label.

--
Paige Miller
sylvanaw
Calcite | Level 5

I am not looking not for the variable lables, but for the value labels for each category (see example above).

andreas_lds
Jade | Level 19

Fancy things can be done with proc template. I have found some papers, but non explaining how to show formatted and unformatted values. Search-string used: "site:sas.com proc template freq".

 

You could clone the formats and add the value to the text displayed and use those formats in proc freq.

An example using sashelp.class:

 

proc format;
	value $Gender
		'M' = 'Male'
		'F' = 'Female'
	;
run;


proc freq data=sashelp.class;
	format Sex $Gender.;
	table Sex;
run;


proc format cntlout=work.GenderFmt;
	select $Gender;
run;


data work.GenderFmt;
	set work.GenderFmt(rename=(label = oldLabel));
	
	length Label $ 10;
	drop oldLabel length default;
	format Label;
	
	Label = cats(Start, ':', oldLabel);
	FmtName = cats(FmtName, '_Clone'); /* will cause trouble with long format names */

run;

proc format cntlin=work.GenderFmt;
run;

proc freq data=sashelp.class;
	format Sex $Gender_Clone.;
	table Sex;
run;

 

 

Astounding
PROC Star

While you might need to do a little juggling, here is a way that is less complex and involves little  juggling.

 

For the same of discussion, let's say the variable AGE has a format AGECAT. that translates it into the formatted labels.  

 

Begin by creating a data set holding the percents:

 

proc freq data=have;

tables age / noprint out=age_counts (keep=age percent)

run;

 

The table doesn't print, but you can easily use the output data set:

 

data want;

set age_counts;

category = age;

run;

 

This creates a second variable with the same value.  But when printing, one gets formatted and one doesn't:

 

proc print data=want;

var category age percent;

format category;

format age agecat.;

run;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 3033 views
  • 1 like
  • 5 in conversation