BookmarkSubscribeRSS Feed
lhsumdalum
Obsidian | Level 7

I'm looking to reverse the order of all columns in a sas dataset. I believe the best way to do this would be to transpose the column data to be rows and then reorder the rows. 

 

Here is my code:

*Step One;
data pre_transpose; 
            set sashelp.class;
            *set &&dataset&i.. ;    
                _row_ + 1;              * Unique identifier ;
                length _charvar_ $20;   * Create 1 character variable ;
            run; 

*Step Two --> Is this where I would reverse columns? ;
proc transpose data = pre_transpose out = middle (where = (lowcase(_name_) ne '_row_'));
                by _row_;
                var _all_;
            quit; 

Here are pictures of my output: 

Step OneStep OneStep Two - Do I Reverse Column Order Here?Step Two - Do I Reverse Column Order Here?

 

So, would I reverse the column order in the second step or is there a better way of achieving this result?

 

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Do something like this

 

proc sql noprint;
   select name into :vars separated by ' '
   from dictionary.columns
   where libname="SASHELP" and memname='CLASS'
   order by varnum descending;
quit;

%put &vars.;

data want;
   format &vars.;
   set sashelp.class;
run;
lhsumdalum
Obsidian | Level 7
Thanks for your reply, I like the proc sql macro method you have suggested. I'm curious what I would replace libname = "SASHELP" and memname = 'CLASS' with if I choose to run this on any dataset from a specified directory?
art297
Opal | Level 21

I agree with @PeterClemmensen in principle, but suggest using retain rather than format. Using format, as such, you'd lose any formats that you had assigned. So, instead, I'd recommend using:

proc sql noprint;
   select name into :vars separated by ' '
   from dictionary.columns
   where libname="SASHELP" and memname='CLASS'
   order by varnum descending;
quit;

data want;
   retain &vars.;
   set sashelp.class;
run;

That way you don't risk losing anything,

Art, CEO, AnalystFinder.com

 

 

 

lhsumdalum
Obsidian | Level 7
This did not reverse the order of the columns for me. My log states this: "NOTE: The query as specified involves ordering by an item that doesn't appear in its select clause."
art297
Opal | Level 21

Show the code you submitted. The libname and memname have to be in upper case. Were they?

 

Art, CEO, AnalystFinder.com

 

lhsumdalum
Obsidian | Level 7

Art. thank you for clarifying that the libname and memname statements have to be in upper case. This has resolved my issue! 

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
  • 6 replies
  • 2821 views
  • 2 likes
  • 3 in conversation