BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
AthiraThulasi
Fluorite | Level 6

Hi,

 

I'm a new SAS user. Pls help me understand the difference in the following code.

 

In the first code - rbc and wbc written right after "BY" statement are sorted in the ascending order.

In the second code - rbc and cholesterol written after wbc is not sorted in ascending order. Why is that?

As per my understanding, variables in "BY" statement are default sorted in ascending order.

 

PROC SORT DATA = blood OUT = sorted_blood;
BY rbc cholesterol DESCENDING wbc;
run;

IMG_8954.png

 

PROC SORT DATA = blood OUT = sorted_blood;
BY DESCENDING wbc rbc cholesterol;
run;

 

IMG_0089.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

As per my understanding, variables in "BY" statement are default sorted in ascending order.

That's correct.

 

In the second code - rbc and cholesterol written after wbc is not sorted in ascending order. Why is that?

Your sort is: BY DESCENDING wbc rbc cholesterol;

The first level of sorting is: DESCENDING wbc

Within the same value for WBC rows then get sorted by ascending rbc and then within the same value of rbc they get sorted by ascending cholesterol.

To "see" that the sorting also works for 3rd level cholesterol you would need to find multiple rows in your source data with identical values for wbc and rbc.

 

If you look at your data then you'll see that it's sorted as requested.

Patrick_0-1707180198945.png

 

 

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

As per my understanding, variables in "BY" statement are default sorted in ascending order.

That's correct.

 

In the second code - rbc and cholesterol written after wbc is not sorted in ascending order. Why is that?

Your sort is: BY DESCENDING wbc rbc cholesterol;

The first level of sorting is: DESCENDING wbc

Within the same value for WBC rows then get sorted by ascending rbc and then within the same value of rbc they get sorted by ascending cholesterol.

To "see" that the sorting also works for 3rd level cholesterol you would need to find multiple rows in your source data with identical values for wbc and rbc.

 

If you look at your data then you'll see that it's sorted as requested.

Patrick_0-1707180198945.png

 

 

AthiraThulasi
Fluorite | Level 6
Got it..
Thankyou....
rvikram
Fluorite | Level 6

All the by variables are sorted in ascending order by default. Variables to be sorted by descending order should be preceded by the word descending, which is what you are doing in your code.

You are sorting the whole table by:
RBC in ascending order
Then, by Cholesterol in Ascending Order.

And finally, by WBC in Descending order due to the keyword 'descending' placed before WBC. 

 

Sort applies to variables in the order listed under BY variable. If you study the rows with identical data points, you will understand.

 

Second example:

rvikram_0-1707184330024.png

 

AthiraThulasi
Fluorite | Level 6
Got it , I checked the identical points...
Thanks much...

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 3234 views
  • 2 likes
  • 3 in conversation