BookmarkSubscribeRSS Feed
dupp99
Calcite | Level 5

For example, the unsorted data set looks like:

 

x            z

21         AL

22         ZA

9           BC

19         AL

21         BC

23         AL

9           ZA

11         BC

 

 

I did 

 

proc sort data=data out=sorted ;
by z x;
run;

 

 

and tried this too

proc sort data=data out=sorted ;
by z;
run;

But what I got looks like:

 

x            z

19         AL

23         AL

9           BC

11         BC

9           ZA

22         ZA

21         AL

21         BC

 

 

Why is the bottom two lines not sorted properly? Help!

 

6 REPLIES 6
ballardw
Super User

@dupp99 wrote:

For example, the unsorted data set looks like:

 

x            z

21         AL

22         ZA

9           BC

19         AL

21         BC

23         AL

9           ZA

11         BC

 

 

I did 

 

proc sort data=data out=sorted ;
by z x;
run;

 

 

and tried this too

proc sort data=data out=sorted ;
by z;
run;

But what I got looks like:

 

x            z

19         AL

23         AL

9           BC

11         BC

9           ZA

22         ZA

21         AL

21         BC

 

 

Why is the bottom two lines not sorted properly? Help!

 


Most likely cause in a character that you can't see.

Also note that posts in the main message window here will remove lots of white space or blank type information so what you posted is likely not what you actually have in your data set.

 

Try using a data step with:

Put z=   z $hex16. ;

And see  what the values following the Z=AL look like. I suspect that you will see one of them has different starting value than 414C

 

 

You might try posting a data step created from your data set using Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

dupp99
Calcite | Level 5

I tried the Put z= z $hex16. ;

 

But that doesn't change anything in the dataset.

 

and the proc sort still got the same output.

ballardw
Super User

@dupp99 wrote:

I tried the Put z= z $hex16. ;

 

But that doesn't change anything in the dataset.

 

and the proc sort still got the same output.


I did not imply that it would "fix" anything. I said you could use it to examine the values. Did you see different results for the ones that didn't sort properly? You need to remove something from the data value but depending on what it might be the code needed may differ.

You might have an encoding issue where some of your data is encoded using a different language scheme, you may just have not printable character (or 3). Show the results of the PUT statement. Paste the result from the LOG into a code box opened using the forum's {I} icon.

Astounding
PROC Star
Another possibility:

Run a PROC CONTENTS on your data and see what you find for the variable Z. It may be that you are looking at formatted values for Z and the data is actually in order by the true (unformatted) value of Z.
Kurt_Bremser
Super User

When I create data out of what you posted, and run the sort:

data have;
input x z $;
datalines;
21         AL
22         ZA
9           BC
19         AL
21         BC
23         AL
9           ZA
11         BC
;

proc sort data=have out=want;
by z;
run;

proc print data=want noobs;
run;

I get this:

 x    z

21    AL
19    AL
23    AL
 9    BC
21    BC
11    BC
22    ZA
 9    ZA

So it is obvious that what you posted is not your real data, but only what you are shown. Leading blanks, invisible characters, or formats disguise the raw values in your dataset.

So you need to follow Maxim 3 and get to know your data. Run a proc contents to see if any formats are involved, convert the strings to hex display (use the double length of the variable when specifiying the $HEX format) as suggested by @ballardw if no format is in play.

Ksharp
Super User

Try to remove some unprintable characters .

 

data data;

 set data;

z=compress(z,' ','s');

/*

z=compress(z, ,'ka');

*/

run;

proc sort ..........

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 2255 views
  • 1 like
  • 5 in conversation