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
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;
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;
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).
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".
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.
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.