簡體   English   中英

如何從 SAS 中的 SAS 表中刪除具有特定后綴的列?

[英]How to drop columns with specific suffixes from a SAS table in SAS?

我在 SAS Enterprise Guide 中有如下表:

COL_DT    | COL_5  | ...  | COL_n
----------|--------|------|--------
10MAY2021 | 1      | ...  | xxx
15DEC2021 | 0.5    | ...  | xxx
09APR2020 | 12     | ...  | xxx
...       | ...    | ...  | ...

我需要從上面的 SAS 表中刪除以以下結尾的列: _DT, _ID, _CP如果存在帶有提到的后綴的列

預期輸出:

COL_5  | ...  | COL_n
-------|------|-------
1      | ...  | xxx
0.5    | ...  | xxx
12     | ...  | xxx
...    | ...  | ...  

我如何在 SAS Enterprise Guide 中執行此操作?

創建示例數據集

data have;
    input col1-col5 col_dt col_id col_cp;
    cards;
1 2 3 4 5 6 7 8 
9 10 11 12 13 14 15 16
;
run;

使用與上一個問題相同的技術創建要刪除的列列表

proc sql noprint;
    select distinct name into :cols separated by ' '
    from sashelp.vcolumn 
    where upcase(memname)='HAVE' and 
        (upcase(name) like '%^_DT' escape '^' 
        or upcase(name) like '%^_ID' escape '^' 
        or upcase(name) like '%^_CP' escape '^');
quit;

刪除選定的列

data want;
    set have(drop=&cols.);
run;

因此

col1 col2 col3 col4 col5
 1     2    3    4    5
 9    10   11   12   13

或者,您可以使用proc contents而不是 SAS 列視圖

data have;
    input col1-col5 col_dt col_id col_cp;
    cards;
1 2 3 4 5 6 7 8 
9 10 11 12 13 14 15 16
;
run;

proc contents data=have out=content noprint nodetails;run;

proc sql noprint;
    select distinct name into :cols separated by ' '
    from content 
    where upcase(memname)='HAVE' and 
        (upcase(name) like '%^_DT' escape '^' 
        or upcase(name) like '%^_ID' escape '^' 
        or upcase(name) like '%^_CP' escape '^');
quit;

data want;
    set have(drop=&cols.);
run;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM