BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Justin9
Obsidian | Level 7

Hi,

 

I currently have the following code:

 

data bands;

   length band $ 20;

   set test;

   if -30<=Number_difference<-20 then band="-30<=Number difference<-20";

       else if -20<=Number_difference<-10 then band="-20<=Number difference<-10";

       else if -10<=Number_difference<-0 then band="-10<=Number difference<0";

       else if 0<=Number_difference<10 then band="0<=Number difference<10";

       else if 10<=Number_difference<20 then band="10<=Number difference<20";

       else if 20<=Number_difference<30 then band="20<=Number difference<30";

       else band="Other";

run;



proc freq data=bands;

   tables band;

run;

 

The proc freq gives me the order as follows:

             band

-10<=Number difference<0

-20<=Number difference<-10

-30<=Number difference<-20

0<=Number difference<10

10<=Number difference<20

20<=Number difference<30

Other

 

However, I would like the proc freq table to be in the order as shown below. Can someone suggest how I could change my code so the output is shown like this please?

                 band

-30<=Number difference<-20

-20<=Number difference<-10

-10<=Number difference<0

0<=Number difference<10

10<=Number difference<20

20<=Number difference<30

Other

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Don't recode your value at all, use a format instead and it will sort correctly if you apply the format instead.

proc freq data=band;
tables number_difference;
format number_difference diff_band_fmt.;
run;

See the following example on how to create the format. Note that your PROC FORMAT code has to be before your PROC FREQ code.
https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf

View solution in original post

1 REPLY 1
Reeza
Super User
Don't recode your value at all, use a format instead and it will sort correctly if you apply the format instead.

proc freq data=band;
tables number_difference;
format number_difference diff_band_fmt.;
run;

See the following example on how to create the format. Note that your PROC FORMAT code has to be before your PROC FREQ code.
https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 800 views
  • 0 likes
  • 2 in conversation