BookmarkSubscribeRSS Feed
sashelp123
Calcite | Level 5

Hello,

 

I am trying to sort my output and am not sure how to insert a few lines in the right place, without re-ordering everything (if possible).

 

Basically it looks like this, as an example:

 

blue - 01

red-02

purple- 03

orange- 04

grey-05 

 

These fields are numbered up to 100, with a code that already existed and are in the correct order. However between lets say blue 01 and red 02, I would like to add an additional field (for example green) so it would come after blue and before red. Is there a number I can assign to green, so that it will be listed after red and before purple. For example would 02.1 work, or is there a way to insert without messing up the other fields? There are basically 100 fields that are assigned codes in order, but I need to insert about 5 in certain places. Thanks! 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Sorting of character strings works alphabetically. The way you have set this up (for the five shown) blue is always first and grey is always second and changing the number to the right of the color will not affect sorting.

 

Sorting of numeric variables works numerically. What you need to do is to create the proper numeric variable containing the sort order for each row, and then your sorts will work the way you want.

--
Paige Miller
sashelp123
Calcite | Level 5

Ok thank you! these were just examples that happened to be alphabetical unfortunately. But basically they each already have a number code assigned (over 100 varabiles) and are in the proper order, so I was hoping for the five I had to fit in, there was a way to add a number between whole numbers. Like if 04 is followed by 05, would there be a number I could assign to stick it between 04 and 05 and avoid re-ordering all 100? Sorry if I am not making sense, I appreciate the help! 

PaigeMiller
Diamond | Level 26

Like if 04 is followed by 05

04 is a character string. 

 

Assuming you have a numeric variable (do you?) then if you have numbers 4 and 5, then 4.5 will sort between them.

 

--
Paige Miller
ballardw
Super User

How many values are involved? Are the values constant or do you have lots of changes?

 

It might be that you could make a custom INFORMAT to create a numeric value that could use to sort with.

A brief example:

data example;
   input value $;
datalines  ;
abc
451
8rtq 
blue3
;

proc format;
  invalue sortord
'451' = 1
'blue3' = 2
'abc'   = 3
'8rtq'  = 4
;
run;

data need;
   set example;
   order = input (value,sortord.);
run;

proc sort data=need;
  by order;
run;

But I'm not sure how "100 fields" applies. Are you saying that you are going to sort on 100 variables???

 

I think you need to provide a much more complete example of data and what the final result would be. Probably only 5 or so variables and enough records to demonstrate typical needed behavior.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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