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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 264 views
  • 4 likes
  • 4 in conversation