0

Issue:

I'm looking to reverse the order of all columns in a sas dataset. Should I achieve this by first transposing and then using a loop to reverse the order of the columns? This is my logic...

Step One:

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

Step One Output:

pre-transpose

Step Two: Do I Reverse Columns Here?

proc transpose data = pre_transpose out = middle (where = (lowcase(_name_) ne '_row_'));
                by _row_;
                var _all_;
            quit; 

Step Two Output:

transposed

EDIT:

I have attempted this:

/* use proc sql to create a macro variable for column names */ 
            proc sql noprint; 
                select varnum, nliteral(name) 
                into :varlist, :varlist separated by ' '
                from dictionary.columns
                where libname = 'WORK' and memname = 'all_character'
                order by varnum desc;
            quit;

            /* Use retain to maintain format */ 
            data reverse_columns;
            retain &varlist.;
            set all_character;
            run;        

But I did not achieve the results I was looking for - the column order is not reversed. 
datanalyst
  • 351
  • 1
  • 3
  • 15
  • 1
    @tom code worked for me and it works. Please change memname ='ALL_CHARACTER', your code is not picking anything into macrovariable because where clause is not picking working. All the tablenames (memname) are stored in Upcase and you have it in lowcase – Kiran Mar 12 '18 at 21:03

1 Answers1

0

You just need to get the list of variable names. One way is to use the metadata. Do if your dataset is member HAVE in libref WORK then you could use this to get the list of variable names into a single macro variable.

proc sql noprint;
  select varnum , nliteral(name)
    into :varlist, :varlist separated by ' ' 
  from dictionary.columns
  where libname='WORK' and memname='HAVE'
  order by varnum desc 
  ;
quit;

You could then use the macro variable in a data step like this.

data want ;
   retain &varlist ;
   set have ;
run;

Note that the value of libname and memname in DICTIONARY.COLUMNS is in uppercase only.

Tom
  • 47,574
  • 2
  • 16
  • 29
  • this didn't reverse the column order for me. See edited post for code. The formats look identical before running that code and after. – datanalyst Mar 12 '18 at 19:47
  • 1
    Make sure that the SQL query finds the variable names. The LIBNAME and MEMNAME values in the metadata tables are stored in uppercase only. – Tom Mar 13 '18 at 01:50