BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NKormanik
Barite | Level 11

I would like to sort a dataset, by the first column.

 

proc sort data=have;
by Col#1;
run;

 

Suppose I don't have the actual name of the Col#1 variable.

 

(Actually, I do, of course, but it varies among datasets.  And I'm seeking a generic approach.)

 

Can this be done?

 

Thanks!

 

Nicholas Kormanik

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Use SQL, as @Reeza suggested, or sort by everything (it will sort on the first variable first)

 

proc sql; 
create table cars as select * from sashelp.cars 
order by 1; 
quit;

/* Or */

proc sort data=sashelp.cars out=cars; by _all_; run;
PG

View solution in original post

4 REPLIES 4
Reeza
Super User

Not via PROC SORT but PROC SQL allows it. 

stat_sas
Ammonite | Level 13

Hi,

 

A couple of steps before proc sort may produce the desired output.

 

proc contents data=sashelp.cars order=varnum out=vars(keep=name varnum) ;
run;

proc sql;
select name into :sort_var from vars
where varnum=1;
quit;

proc sort data=cars;
by &sort_var;
run;

 

 

PGStats
Opal | Level 21

Use SQL, as @Reeza suggested, or sort by everything (it will sort on the first variable first)

 

proc sql; 
create table cars as select * from sashelp.cars 
order by 1; 
quit;

/* Or */

proc sort data=sashelp.cars out=cars; by _all_; run;
PG
NKormanik
Barite | Level 11

Thank you all for your help.

 

Since these datasets only contain two columns, it is easiest to sort by everything.  Straightforward and easy.  Wonderful.  Glad it can be done this way.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 2764 views
  • 2 likes
  • 4 in conversation