DATA Step, Macro, Functions and more

Refer To Column One Variable Without Knowing Variable Name

Accepted Solution Solved
Reply
Regular Contributor
Posts: 235
Accepted Solution

Refer To Column One Variable Without Knowing Variable Name

[ Edited ]

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

 


Accepted Solutions
Solution
‎11-24-2017 03:32 AM
Esteemed Advisor
Posts: 5,482

Re: Refer To Column One Variable Without Knowing Variable Name

Posted in reply to NicholasKormanik

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


All Replies
Super User
Posts: 23,296

Re: Refer To Column One Variable Without Knowing Variable Name

Posted in reply to NicholasKormanik

Not via PROC SORT but PROC SQL allows it. 

Trusted Advisor
Posts: 1,270

Re: Refer To Column One Variable Without Knowing Variable Name

Posted in reply to NicholasKormanik

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;

 

 

Solution
‎11-24-2017 03:32 AM
Esteemed Advisor
Posts: 5,482

Re: Refer To Column One Variable Without Knowing Variable Name

Posted in reply to NicholasKormanik

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
Regular Contributor
Posts: 235

Re: Refer To Column One Variable Without Knowing Variable Name

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.

 

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 157 views
  • 2 likes
  • 4 in conversation