BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to sort by user defined order.

Why this code is not working?

I expect to get results 

Ronein_0-1643528912287.png

 

 

 


 Data have;
 input appidentity ID Var $ value Supplement; 
 cards;
888888 111 Inter . 100
888888 111 Age 50 200
888888 111 Wealth 5 300
888888 111 Educ 30 200
888888 111 Loan 65 500
888888 111 Cycle 120 100
888888 111 Total . 1400
888888 222 Inter . 200 300
888888 222 Age 40 200
888888 222 Wealth 300 500
888888 222 Educ 5 500
888888 222 Loan 40 200
888888 222 Cycle 10 100
888888 222 Total . 1800
999999 777 Inter . 100
999999 777 Age 60 200
999999 777 Wealth 500 700
999999 777 Educ 8 200
999999 777 Loan 0 300
999999 777 Cycle 300 1500
999999 777 Total . 3000
;
RUN;
 
Data Have2;
SET have;
appidentity_=put(appidentity,best.);
ID_=put(ID,best.);
RUN;

proc sql;
create table Want as
select *
from Have2
order by whichc(appidentity_,
                ID_,
				'Inter',
				'Educ',
				'Loan',
				'Cycle',
				'Wealth',
				'Age',
				'Total');
quit;

 

 

3 REPLIES 3
japelin
Rhodochrosite | Level 12

how about this

 

 

proc sql;
create table Want as
select *
from Have
order by appidentity
        ,ID
        ,whichc(VAR,
		       'Inter',
		       'Educ',
		       'Loan',
		       'Cycle',
		       'Wealth',
		       'Age',
		       'Total');
quit;

 

 

check whichc

 

Ksharp
Super User
 Data have;
 input appidentity ID Var $ value Supplement; 
 cards;
888888 111 Inter . 100
888888 111 Age 50 200
888888 111 Wealth 5 300
888888 111 Educ 30 200
888888 111 Loan 65 500
888888 111 Cycle 120 100
888888 111 Total . 1400
888888 222 Inter . 200 300
888888 222 Age 40 200
888888 222 Wealth 300 500
888888 222 Educ 5 500
888888 222 Loan 40 200
888888 222 Cycle 10 100
888888 222 Total . 1800
999999 777 Inter . 100
999999 777 Age 60 200
999999 777 Wealth 500 700
999999 777 Educ 8 200
999999 777 Loan 0 300
999999 777 Cycle 300 1500
999999 777 Total . 3000
;
RUN;


proc format;
invalue fmt
'Inter'=1
'Educ'=2
'Loan'=3
'Cycle'=4
'Wealth'=5
'Age'=6
'Total'=7
;
run;

proc sql;
create table want as
select *
 from have 
  order by appidentity ,ID ,input(var,fmt.);
quit;


PeterClemmensen
Tourmaline | Level 20

@Ronein, I see that you adapted my technique from the post here.

 

Please mark that as closed before you open a new thread 🙂

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1056 views
  • 3 likes
  • 4 in conversation