multiple variables not properly sorted in sort procedure

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

multiple variables not properly sorted in sort procedure

I have a data set with variables including gender, biology_score1, biology_score2, and biology_score3. I want to sort these variables by gender in an alphabetical order, and by biology_score1 in a descending order and by biology_score2 and biology_score3 in ascending orders, all in one sort procedure. The code I have is: 

 

proc sort data= A02_scores1 out=sorted_scores;
by gender descending biology_score1 biology_score2 biology_score3;
run;
proc print data= sorted_scores;
var gender biology_score1 biology_score2 biology_score3;
run;

 

When I run this, the gender and descending biology_score1 sort correctly, but the other two variables are not sorting in ascending orders, or seemingly any order. I tried putting in "ascending" ahead of the variable name in the "by" line even though it is default, but still no luck.


Accepted Solutions
Solution
‎11-07-2016 11:59 PM
Grand Advisor
Posts: 17,316

Re: multiple variables not properly sorted in sort procedure

It sounds like you're following the instructions. Your idea of how the output should look is incorrect though. 

 

All vars are sorted by groups within the previous variables restrictions. 

 

 

 

As mentioned the output you want isn't possible in a single sort AFAIK. 

View solution in original post


All Replies
Grand Advisor
Posts: 17,316

Re: multiple variables not properly sorted in sort procedure

Data is sorted by row so a row is moved with all elements together when I suspect you want the values to be displayed in a different order. 

 

Post some sample input/output data if you want confirmation. I'm not actually sure how to get around this easily...

Occasional Contributor
Posts: 13

Re: multiple variables not properly sorted in sort procedure

Perhaps I'm misinterpreting the instructions I have? Those are: "Sort the temporary SAS data set by gender in an alphabetical order, and by biology_score1 in a descending order, and by biology_score1 and biology_score3 in ascending orders, using a single SORT procedure."  With the current code and data that I have, what I get looks like this:

SAS Output


 gender biology_score1 biology_score2 biology_score3
F858888
F768185
F716676
F697374
F667178
M879091
M787679
M748185
M728889
M697881
I would think that the biology_score2 column should read from top to bottom: 66, 71, 73, 81, 88, 76, 78, 81, 88, 90 and the biology_score3 column from top to bottom: 74, 76, 78, 85, 88, 79, 81, 85, 89, 91? Is this even possible to do, since it sorts from one variable to the next?
Solution
‎11-07-2016 11:59 PM
Grand Advisor
Posts: 17,316

Re: multiple variables not properly sorted in sort procedure

It sounds like you're following the instructions. Your idea of how the output should look is incorrect though. 

 

All vars are sorted by groups within the previous variables restrictions. 

 

 

 

As mentioned the output you want isn't possible in a single sort AFAIK. 

Occasional Contributor
Posts: 13

Re: multiple variables not properly sorted in sort procedure

It seems I'm probably just getting confused as to how they want it to be sorted based on the wording of the question, which made it sound to me like they wanted all scores sorted separately relative to gender in the manner I described above. Thanks for your assistance!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 213 views
  • 1 like
  • 2 in conversation