DATA Step, Macro, Functions and more

Control order of values displayed in Proc Freq output

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Control order of values displayed in Proc Freq output

[ Edited ]

I keep getting the output on racegrp, "Other" option displayed either in the middle or at the top of the result. see attached in details for sas code and output.

Thanks.


Accepted Solutions
Solution
‎02-20-2016 10:52 PM
Super User
Posts: 17,912

Re: sas output

Code white as 1, black as 2, other as 3, and apply proc format.

 

proc format;
value race_fmt
1 = 'White'
2 = 'Black'
3 = 'Other';
run;

data have;
set have;
if race='White' then cat=1;
else if race='Black' then cat=2;
else cat=3;
run;

proc freq data=have;
table cat;
format cat race_fmt.;
run;

View solution in original post


All Replies
Super User
Posts: 17,912

Re: sas output

[ Edited ]

The standard way around this is to code the variables as numeric and use formats to apply the displayed value. 

 

You also don't say what you want, so this is a bit of a guess. 

Occasional Contributor
Posts: 13

Re: sas output

Thank you Reeza for prompt response. To make clear, I want the the output table to produce as coded, White, Black, & Other in a sequence. 

 

Thanks

Solution
‎02-20-2016 10:52 PM
Super User
Posts: 17,912

Re: sas output

Code white as 1, black as 2, other as 3, and apply proc format.

 

proc format;
value race_fmt
1 = 'White'
2 = 'Black'
3 = 'Other';
run;

data have;
set have;
if race='White' then cat=1;
else if race='Black' then cat=2;
else cat=3;
run;

proc freq data=have;
table cat;
format cat race_fmt.;
run;
Occasional Contributor
Posts: 13

Re: sas output

Reeza,

 

As always, you have a solution to the question. I attached the solution output.

 

Thanks

Super User
Posts: 9,687

Re: sas output

[ Edited ]

Reeza's solution is a little bit of complicated. Create a new dummy variable  CAT and padding blanks before it .

 

data have;
set have;
length cat $ 40;
if race='White' then cat='  White';
else if race='Black' then cat=' Black' ;
else cat='Other';
run;

proc freq data=have;
table cat;
run;

 

Respected Advisor
Posts: 3,777

Re: sas output

@ReezaI would say the "standard" SAS way to do this is by using features of PRELOADFORMAT and NOTSORTED value label formats.

Trusted Advisor
Posts: 1,115

Re: Control order of values displayed in Proc Freq output

@abuanuazu: In your specific case you could have achieved the desired result even easier, without any additional steps:

proc freq data=have order=freq;
tables racegrp;
run;

The ORDER=FREQ option sorts the categories by descending frequency counts.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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