Help using Base SAS procedures

Proc freq output in descending order of variable values

Reply
N/A
Posts: 0

Proc freq output in descending order of variable values

This is probably a very simple solution, but I cannot remember how to code for my data to be displayed in descending order for my proc freq output. For instance, for variable A, the values are 0 1 2 3 4 11 22 33 44

Then in my proc output, variable A is ordered as follows
0
1
11
2
22
3
33
4
44

but I would like it to be ordered as follows
0
1
2
3
4
11
22
33
44

Any help is appreciated!!
SAS Super FREQ
Posts: 8,743

Re: Proc freq output in descending order of variable values

Is variable A a character variable or a numeric variable?

cynthia
N/A
Posts: 0

Re: Proc freq output in descending order of variable values

I actually have a mixture of character and number variables I am trying to do this to, if it's possible on both.
SAS Super FREQ
Posts: 8,743

Re: Proc freq output in descending order of variable values

Hi:
The SORTSEQ option of PROC SORT allows you to sort your data, using an option to tell SORT to sort by the numeric value of a field, instead of the character representation of the field. (The example in the doc explains that you would use NUMERIC_COLLATION=ON when you wanted to show "8 Main Street" sorted above "45 Main Street" (with NUMERIC_COLLATION=OFF, then the "45 Main Street" would come first)
http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a000146878.htm

Once you have the data sorted, then you need to tell PROC FREQ to use the sorted order, or DATA order, as shown in the output from the program below.

cynthia
[pre]
data myvars;
infile datalines;
input grp $ vara $ varb;
return;
datalines;
aaa 0 10
bbb 1 20
ccc 2 30
ddd 3 40
eee 11 40
fff 22 30
ggg 33 20
hhh 44 10
;
run;

ods listing;
proc freq data=myvars;
title '1) proc freq before sort';
table grp vara varb;
run;

proc sort data=myvars sortseq=linguistic(numeric_collation=on);
by grp vara varb;
run;

ods listing;
proc freq data=myvars order=data;
title '2) proc freq after sort with order=data';
table grp vara varb;
run;
[/pre]
Regular Contributor
Posts: 241

Re: Proc freq output in descending order of variable values

This is easy, since we have the right() function now.

By the way, I think you meant an ascending order. :-)



   /* test data */


   data one;


     length c $2 n 8;


     input c @@;


     n = input(c, best.);


   cards;


   0 1 11 2 22 3 33 4 44


   ;


   run;


 


   /* a view that right aligns character vars */


   data two/view=two;


      set one;


      array cs _character_;


      do over cs;


         cs = right(cs);


      end;


   run


 


   proc freq data=two;


   run;


   /* on lst


                                  Cumulative    Cumulative


   c     Frequency     Percent     Frequency      Percent


   -------------------------------------------------------


    0           1       11.11             1        11.11


    1           1       11.11             2        22.22


    2           1       11.11             3        33.33


    3           1       11.11             4        44.44


    4           1       11.11             5        55.56


   11           1       11.11             6        66.67


   22           1       11.11             7        77.78


   33           1       11.11             8        88.89


   44           1       11.11             9       100.00


 


 


                                  Cumulative    Cumulative


    n    Frequency     Percent     Frequency      Percent


   -------------------------------------------------------


    0           1       11.11             1        11.11


    1           1       11.11             2        22.22


    2           1       11.11             3        33.33


    3           1       11.11             4        44.44


    4           1       11.11             5        55.56


   11           1       11.11             6        66.67


   22           1       11.11             7        77.78


   33           1       11.11             8        88.89


   44           1       11.11             9       100.00


   */

Ask a Question
Discussion stats
  • 4 replies
  • 646 views
  • 0 likes
  • 3 in conversation