如何提取SAS数据集中的列子集

问题描述 投票:0回答:1

可以说,我有一个SAS数据集,我想保留firt 2列和最后一个4列。换句话说,只有从头到尾的列。

data test;
    input a b c d e f g h i j;
cards;
1 2 3 4 5 6 7 8 9 10
;

初始输出:enter image description here

我想要的是以下内容-

所需输出:

enter image description here

我在网上检查过,人们正在尝试使用varnum,如图here所示,但我不知道。我不想使用here,而是想要一种自动的方法来解决此问题。

sas
1个回答
1
投票

如果您不知道变量数,则可以使用此宏(您应指定第一个变量数和最后一个变量数以保留在数据集,libname和数据集名称中:]

keep/drop

执行宏之前的数据集:

%macro drop_vars(num_first_vars,num_end_vars,lib,dataset); %macro d;%mend d;

proc sql noprint;;
    select sum(num_character,num_numeric) into:ncolumns 
    from dictionary.tables 
    where libname=upcase("&lib") and memname=upcase("&dataset");
    select name into: vars_to_drop separated by ',' 
    from dictionary.columns 
    where libname=upcase("&lib") and 
          memname=upcase("&dataset") and
          varnum between %eval(&num_first_vars.+1) and %eval(&ncolumns-&num_end_vars);
   alter table &lib..&dataset
   drop &vars_to_drop;
quit;

%mend drop_vars;

%drop_vars(2,3,work,test);

宏执行后的数据集:

+---+---+---+---+---+---+---+---+---+----+
| a | b | c | d | e | f | g | h | i | j  |
+---+---+---+---+---+---+---+---+---+----+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
+---+---+---+---+---+---+---+---+---+----+
© www.soinside.com 2019 - 2024. All rights reserved.