BookmarkSubscribeRSS Feed
alexmathp
Fluorite | Level 6

Hi to all,

 

I want to ask how we can do sorting from proc format by second column and not first like it does by default.

To make it clear what i mean, i want to my formats to be sorted by second column (the red circle) of the below picture

Capture.PNG

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

I dont understand this. You want help to restructure the code or?

alexmathp
Fluorite | Level 6
when i load the formats in my data the sorting is from first column and not second like i want
Kurt_Bremser
Super User

Absolutely irrelevant. When the format is applied, later procedure steps will either order by formatted or unformatted values anyway, depending on the procedure.

If you want nicely ordered code, shuffle your code lines by selecting and moving.

PaigeMiller
Diamond | Level 26

Explain further. There is really no concept of sorting formats in SAS. Where should we see this proper sorting? In the output of some procedure? In a data set?

--
Paige Miller
alexmathp
Fluorite | Level 6
Yes i want to my data step to sort by the second column and not first like it does. Tell me if it clear now ok
Kurt_Bremser
Super User

The DATA step does not sort on its own. Certain procedures will order their output automatically.

If you want PROC SORT to sort by formatted values, you have to create a new variable containing the formatted values first.

In SQL, you can do that "on the fly":

proc format;
value mytest
  1 = "B"
  2 = "A"
;

data have;
input value;
format value mytest.;
datalines;
1
2
;

proc sql;
create table want as
  select put(value,mytest.) as value
  from have
  order by calculated value
;
quit;
PaigeMiller
Diamond | Level 26

@alexmathp wrote:
Yes i want to my data step to sort by the second column and not first like it does. Tell me if it clear now ok

You have not answered my questions: "Where should we see this proper sorting? In the output of some procedure? In a data set?"

--
Paige Miller
Tom
Super User Tom
Super User

Reordering the lines of the code used to generate the format will not have any effect on how the format is defined.

What is it that you really want to do?  

 

If you want something else to sort by the formatted values then store the formatted values into a another variable and sort by that.

If you are using PROC REPORT then use the ORDER=FORMATTED option. Note that is the default ordering for PROC REPORT.  https://support.sas.com/resources/papers/proceedings11/090-2011.pdf

 

alexmathp
Fluorite | Level 6

I want to make fac and bid file from the proc formats that i did already and the sorting to be done from the second column in some cases. If it is applicable of course?

ballardw
Super User

Please provide code by posting text in a code box.

I was going to show some affects of Formats and Procedure options but since you did not provide the code for the format as code I could execute I can't. I'm too lazy to retype someone else's work.

 

The PROCEDURE you use to display values will depend somewhat as to what you need to do get formatted values in a specific order (or not).

And since you have the formatted values '09' '07' and '08' appear more than once in the format it is not very clear what expected order you would expect.

 

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
  • 10 replies
  • 1945 views
  • 3 likes
  • 6 in conversation