Hello
I want to sort by user defined order.
Why this code is not working?
I expect to get results
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;
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
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.