Hello everyone, I have a question regarding proc sort. Anyone can help? Thanks in advance!
For example I have a dataset "survey", and a character variable "question" and want to sort it.
I hope to get the ideal sequence order, but I got the sorted sequence in the middle column. I wonder whether it is possible to get the ideal sequence in SAS? Thanks!!!
proc sort data=survey;
by question;
run;
original sequence | after sorting | ideal sequence |
| q2 | q1 | q1 |
| q3 | q10 | q2 |
| q4 | q2 | q3 |
| q5 | q3 | q4 |
| q1 | q4 | q5 |
| q7 | q5 | q6 |
| q10 | q6 | q7 |
| q8 | q7 | q8 |
| q9 | q8 | q9 |
| q6 | q9 | q10 |
I think you are looking for the following:
data survey;
input question $;
cards;
q2
q3
q4
q5
q1
q7
q10
q8
q9
q6
;
proc sort data=survey SORTSEQ=linguistic
(NUMERIC_COLLATION=ON);
by question;
run;
I think you are looking for the following:
data survey;
input question $;
cards;
q2
q3
q4
q5
q1
q7
q10
q8
q9
q6
;
proc sort data=survey SORTSEQ=linguistic
(NUMERIC_COLLATION=ON);
by question;
run;
Hi Authur,
This is exactly what I wanted. Thank you so much!!!
Yanmin
Or a SQL way which has more power.
data survey; input question $; cards; q2 q3 q4 q5 q1 q7 q10 q8 q9 q6 ; run; proc sql; create table want as select * from survey order by input(compress(question, ,'kd'),best8.); quit;
Ksharp
Thank you so much, Ksharp!
Your Sql code works very well. It is great to know both methods work.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.