BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
CathyVI
Pyrite | Level 9

Hi,

I am trying to achieve a specific sort order using custom sort for the variables Names and Codes. I have a sample variable with this order below:

Names    Codes
Boys        2795
                 2796
                 2900
                 PHA
                 Total
Girls        2795
                 2796
                 2900
                 PHA
                Total
Dads      2795
                2796
                2900
                PHA
               Total
Moms   2795
               2796
               2900
               PHA
               Total

 

This is the order I wanted

Names  Codes
Dads      PHA
                2795
                2796
                2900
                Total
Moms    PHA
                2795
                2796
                2900
               Total
Boys      PHA
               2795
               2796
               2900
              Total
Girls     PHA
              2795
              2796
              2900
              Total

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

Try this:

proc format;
  value $Name_Order
  'Dads' = '1'
  'Moms' = '2'
  'Boys' = '3'
  'Girls' = '4'
  ;
  value $Code_Order
  'PHA' = '1'
  '2795' = '2'
  '2796' = '3'
  '2900' = '4'
  'Total' = '5'
  ;
run;

data Want;
  set Have;
  Name_Order = put(Names, $Name_Order.);
  Code_Order = put(Codes, $Code_Order.);
run;

proc sort data = Want;
  by Name_Order Code_Order;
run; 

View solution in original post

3 REPLIES 3
SASKiwi
PROC Star

Try this:

proc format;
  value $Name_Order
  'Dads' = '1'
  'Moms' = '2'
  'Boys' = '3'
  'Girls' = '4'
  ;
  value $Code_Order
  'PHA' = '1'
  '2795' = '2'
  '2796' = '3'
  '2900' = '4'
  'Total' = '5'
  ;
run;

data Want;
  set Have;
  Name_Order = put(Names, $Name_Order.);
  Code_Order = put(Codes, $Code_Order.);
run;

proc sort data = Want;
  by Name_Order Code_Order;
run; 
Patrick
Opal | Level 21

This looks more like a sort order you want for some report. Which SAS Proc are you using? And how does the source data look like that you feed this Proc (ideally share sample data and your code).

ballardw
Super User

I have no clue what a PHA might be but if TOTAL is easily derived using the values in PHA and those other codes then it maybe should not be in this set. SAS Reporting tools such as Proc Tabulate, Report and even Print can sum values and Tabulate and Report can do other summaries usually considered as "Total".

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!

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