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

Hi ,

I have two datasets B1 and B2 and i want to sort both datasets by first column can i do it dyanmically without indicating the name of teh column ?

 

B1

NUMBERITEMSCODES
22222232222
7141111545454
315151515454545
18888888

484888

 

 

b2

 

IDITEMSCODES
21151515746467
6155154748784
57884878234454
175157874864487

 

 

%macro xc;

proc sort data=B_&i out= c_&i;

by ;

run;

%mend;

%xc;

 

i want to order by first column for B1 and B2 . Can anyone suggest me what should i do ?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

How about that:

%macro xc(i);

data _null_;
set sashelp.vcolumn;
where upcase(libname) = 'WORK' and upcase(memname) = upcase("B_&i");
if _n_ = 1
then do;
  call symput('varname',name);
  stop;
end;
run;

proc sort data=B_&i out=c_&i;
by &varname;
run;

%mend;

%xc(1);

Not tested, of course. Therefore no warranty or liability.

View solution in original post

10 REPLIES 10
Reeza
Super User

Not via proc sort but Proc SQL does allow you to reference columns using a column number. 

 

Proc sql;

create table class as

select *

from sashelp.class

order by 1;

quit;

hexx18
Quartz | Level 8

 

 

i did

 

proc sql;

create table c_&i. as

  select * from b_&i.

order by 1;

quit;

 

but its not working its because the first column is character ?

Reeza
Super User

No, the first column can be character. What does your log say, please include the error as well

 

 

And does this work?

proc sql;
create table class as
select *
from sashelp.class
order by 1;
quit;
ChrisNZ
Tourmaline | Level 20

If the sort order is incorrect because you have numbers in strings and you want to sort by the number in the string, do this:

 

data HAVE;
  x='10'; output;
  x='5';  output;
run;

proc sort sortseq=linguistic(numeric_collation=on);
  by _ALL_;
run;

proc print; 
run;
Obs x
1 5
2 10

 

 

hexx18
Quartz | Level 8

its not working probably my ID nad number coloumns are character is there a way that i can change ID and number coloumns from both B1 and B2 datasets to numberic while its getting imported ? so that when there is a sas dataset i will have id and number coloumns numeric

 

 

Thanks

PGStats
Opal | Level 21

Only works for the first column:

 

proc sort data=sashelp.class out=sortedClass; 
by _all_; 
run;
PG
hexx18
Quartz | Level 8

what if i want to order by only first column ?

PGStats
Opal | Level 21

A dataset ordered by _ALL_ is ordered by the first column. In any case, you are better off with SQL, as @Reeza suggested.

PG
Kurt_Bremser
Super User

How about that:

%macro xc(i);

data _null_;
set sashelp.vcolumn;
where upcase(libname) = 'WORK' and upcase(memname) = upcase("B_&i");
if _n_ = 1
then do;
  call symput('varname',name);
  stop;
end;
run;

proc sort data=B_&i out=c_&i;
by &varname;
run;

%mend;

%xc(1);

Not tested, of course. Therefore no warranty or liability.

hexx18
Quartz | Level 8

Thanks a lot  it worked

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 10 replies
  • 1454 views
  • 5 likes
  • 5 in conversation