I have produced a range of PROC FREQ tables, and I would like them ordered in a particular way. For example, in the screenshot attached, I would like them ordered as: 'Agree, Somewhat Agree, Neither Agree nor Disagree, Somewhat Disagree, Disagree'.
I know that I am able to order the observations based on alphabetical order, or frequency etc. but is there a way to order them completely customized?
Personally what I do with such data is to read the original data into a numeric value in the order I want. Then use another format to display the text as desired. By default Proc Freq will use the numeric value of the variable for display order. Other procedures you may need to use another option to control order.
One of the big advantages of this approach is a simple different format can create different analysis grouped values which are honored by analysis, reporting and graphing procedures generally.
Here is an example of creating a custom informat (the invalue statement), using it to read a couple of variables and creating output with proc freq with two different display formats.
proc format; invalue scale (upcase) 'AGREE' = 1 'SOMEWHAT AGREE' = 2 'NEITHER AGREE NOR DISAGREE' = 3 'SOMEWHAT DISAGREE' = 4 'DISAGREE' = 5 other = . ; value scale 1 = 'Agree' 2 = 'Somewhat agree' 3 = 'Neither agree nor disagree' 4 = 'Somewhat disagree' 5 = 'Disagree' ; value scale_3level 1,2 = 'Agree/somewhat agree' 3 = 'Neither agree nor disagree' 4,5 = 'Somewhat disagree/disagree' ; data example; infile datalines dlm=','; informat x y scale.; input x y; datalines; Agree,Agree Somewhat agree,Disagree Somewhat disagree,Somewhat disagree Neither agree nor disagree,agree Disagree,Disagree Somewhat agree,Somewhat agree ; proc freq data=example; table x y; format x y scale.; run; proc freq data=example; table x y; format x y scale_3level.; run;
If you don't want to reread your base data you could add a numeric version of your existing variable(s) with an input function call in a data step;
data want; set have; numvar = input(var, scale.); run;
and use the format desired.
If you have many similar variables then use arrays to do the same to all of them. The more variables involved the more likely I would be to reread the raw data file.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.