Hi zetter, The order of a character variable depends on what you put in the character string description and it actually depends on the operating system. This is also true about the self defined format. So, if you want the displayed order to correspond to a numeric order, your first two letters should reflect the desired order. How about: data library; format id 3. behaviour $4. lib_visits 3. lib_vis_cat $7.; input id behaviour $ lib_visits; if lib_visits < 10 then lib_vis_cat = "1-9"; else if lib_visits < 21 then lib_vis_cat = "10-20"; else if lib_visits < 30 then lib_vis_cat = "21-30"; else if lib_visits >= 21 then lib_vis_cat = "31+"; label lib_vis_cat = "Library visits count"; cards; 110 good 2 111 good 15 113 bad 10 114 bad 1 115 nice 21 116 nice 24 117 good 6 119 bad 3 120 good 31 121 good 15 123 bad 10 124 bad 1 125 nice 2 126 nice 24 127 good 6 128 bad 35 ; run; proc freq data=library; tables behaviour*lib_vis_cat /missing; title "Behaviour by Library visit count"; run; A blank precedes any character in all operating systems I worked on. In Windows OS any digit precedes letters, unlike on the mainframe Z/OS. The reason for the format statement in the input data step is that if you don't assign the length of a character variable, the assigned length will be taken from the first data row, so if it happens to be "bad," then your other values will be truncated to three characters. I added the format for id to have it listed as the first variable, since the frmat statement dictates the order here. If you only put the character variables in the format statement, then the position of the variables will be different. Try it on your own. The /missing option on the tables statement in proc freq makes sure that all categories are listed, in particular the missing values. Since you are clearly a novice, I did not suggest proc tabulate. It gets really tricky when there are missing values. I do not advise a SAS novice to use it before mastering basic stuff. Proc freq is a staple of SAS and it provides a lot more information with minimal code. The user defined format is a nice approach, but if you associate the variable with the format in a data step, rather than each time in a procedure, only the formatted values get saved. When you make a permanent data set with such formats and you later lose the format definition, you will not be able to retrieve the values. So, be careful with user defined formats. Good luck. Dorota Edited to add a comment on user defined formats and the format statement.
... View more