SAS反向列顺序

时间:2018-03-12 18:24:06

标签: sas transpose palindrome datastep

问题:

我希望颠倒sas数据集中所有列的顺序。我应该通过首先转置然后使用循环来反转列的顺序来实现这一点吗?这是我的逻辑......

第一步:

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

第一步输出:

pre-transpose

第二步:我在这里反转列吗?

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

第二步输出:

transposed

编辑:

我试过这个:

/* 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. 

1 个答案:

答案 0 :(得分:0)

您只需要获取变量名称列表。一种方法是使用元数据。如果您的数据集是libref HAVE中的成员WORK,那么您可以使用它来将变量名称列表转换为单个宏变量。

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;

然后,您可以在这样的数据步骤中使用宏变量。

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

请注意,DICTIONARY.COLUMNS中的libname和memname的值仅为大写。