SA,仅保留包含特定字符的列 [英] SAS, keeping only columns that contain a certain character

查看:16
本文介绍了SA,仅保留包含特定字符的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以执行仅保留包含特定字符的列的操作。

例如,假设我有几列:姓名、姓氏、性别、年龄。 我只想保留以字母"s"(姓氏和性别)开头的列。 我该怎么做?

推荐答案

如何用过滤发出姓名有几种变体。

对于前缀或变量列表,这非常容易。对于后缀或更复杂的模式,它保持更复杂。一般来说,您可以按如下方式创建快捷列表:

_numeric_ : all numeric variables
_character_ : all character variables
_all_  : all variables
prefix1 - prefix# : all variables with the same prefix assuming they're numbered
prefix:  : all variables that start with prefix
firstVar -- lastVar : variables based on location between first and last variable, including the first and last. 
first-numeric-lastVar : variables that are numeric based on location between first and last variable

任何更复杂的事情都需要您通过元数据列表对其进行过滤操作。SAS基本上保留了有关每个数据集的一些元数据,以便您可以查询这些信息来构建列表。有关列和类型的数据位于sashelp.vcolumndictionary.column数据集中。

过滤所有包含单词mpg的列,例如:

*generate variable list;
proc sql noprint;
select name into :var_list separated by " "
from sashelp.vcolumn

where libname = 'SASHELP' and memname = 'CARS' 
and lowcase(name) like '%mpg%';
quit;

*check log for results;
%put &var_list;

*verification from original table;
proc contents data=sashelp.cars;
run;

*example of usage;
data want;
set sashelp.cars;
keep &var_list;
run;

blog posthere (documentation)中提供了更多详细信息。

这篇关于SA,仅保留包含特定字符的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆