SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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!!
4 REPLIES 4
Cynthia_sas
SAS Super FREQ
Is variable A a character variable or a numeric variable?

cynthia
deleted_user
Not applicable
I actually have a mixture of character and number variables I am trying to do this to, if it's possible on both.
Cynthia_sas
SAS Super FREQ
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]
chang_y_chung_hotmail_com
Obsidian | Level 7
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


   */

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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