BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I am using proc transpose.

In the result  the order of columns  is :YYMM_1901, YYMM_1902, YYMM_1904, YYMM_1903.

But I want to have order : YYMM_1901, YYMM_1902, YYMM_1903, YYMM_1904.

What is the way to control the order of the columns?

I know that I can use retain statement in a separate data step.

Is there a way to control columns order within proc transpose?

Data aaa;
input ID  month sum_loan;
cards;
1 1901 10
1 1902 20
1 1904 30
2 1901 40
2 1903 50
;
run;
proc transpose data=aaa name=Metric prefix=YYMM_ out=bbb;
id month;
by ID ;
var sum_loan;
run;
2 REPLIES 2
Kurt_Bremser
Super User

The order of columns in a dataset is irrelevant, as columns are addressed by names.

Transpose adds the new columns whenever the first instance is encountered.

If you need a special order for display (eg in a report), you can prepare the order dynamically:

data aaa;
input ID month sum_loan;
cards;
1 1901 10
1 1902 20
1 1904 30
2 1901 40
2 1903 50
;

proc transpose data=aaa name=metric prefix=YYMM_ out=bbb;
id month;
by ID;
var sum_loan;
run;

proc sql noprint;
select name into :names separated by ' '
from dictionary.columns
where libname = 'WORK' and memname = 'BBB' and name like 'YYMM%'
order by name;
quit;

proc print data=bbb noobs;
var id &names.;
run;
Ksharp
Super User
Data aaa;
input ID  month sum_loan;
cards;
1 1901 10
1 1902 20
1 1904 30
2 1901 40
2 1903 50
;
run;
proc transpose data=aaa name=Metric prefix=YYMM_ out=bbb;
id month;
by ID ;
var sum_loan;
run;


proc transpose data=bbb(obs=0) out=temp;
var yymm_:;
run;
proc sql noprint;
select _NAME_ into : names separated by ' '
 from temp
  order by input(scan(_NAME_,-1,'_'),best.);
run;
data want;
 retain id metric &names;
 set bbb;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 826 views
  • 0 likes
  • 3 in conversation