通过根据 SAS 名称中的关键字选择变量来对数据集进行子集化 [英] Subsetting a dataset by selecting variables based on keywords in their name in SAS

查看:23
本文介绍了通过根据 SAS 名称中的关键字选择变量来对数据集进行子集化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以提供帮助.我有一个导入 SAS 的大型数据集,其中包含数千个变量.我想通过提取名称中包含特定关键字的变量来创建一个新数据集.例如,以下变量在我的数据集中:

I hope someone can help. I have a large dataset imported to SAS with thousands of variables. I want to create a new dataset by extracting variables that have a specific keyword in their name. For example, the following variables are in my dataset:

AAYAN_KK_Equity_Ask
AAYAN_KK_Equity_Bid
AAYAN_KK_Equity_Close
AAYAN_KK_Equity_Date
AAYAN_KK_Equity_Volume
AAYANRE_KK_Equity_Ask
AAYANRE_KK_Equity_Bid
AAYANRE_KK_Equity_Close
AAYANRE_KK_Equity_Date

AAYAN_KK_Equity_Ask
AAYAN_KK_Equity_Bid
AAYAN_KK_Equity_Close
AAYAN_KK_Equity_Date
AAYAN_KK_Equity_Volume
AAYANRE_KK_Equity_Ask
AAYANRE_KK_Equity_Bid
AAYANRE_KK_Equity_Close
AAYANRE_KK_Equity_Date

我想提取以 _Ask 和 _Bid 结尾的变量,而不知道变量名称的其余部分.有没有办法做到这一点?我想尝试使用 do 循环,但不知道如何指示 SAS 将每个变量名称的最后一部分与 _Ask 或 _Bid 进行比较.

I want to extract variables that end with _Ask and _Bid without knowing the rest of the variable's name. Is there a way to do that? I want to try using a do loop but don't know how to instruct SAS to compare each variable's last part of the name with _Ask or _Bid.

后记.我想为除最后一部分(_Ask 或 _Bid)之外的以变量全名开头的每个集合创建一个新变量.我可以在使用赋值语句时做到这一点吗?

Afterwords. I want to create a new variable for each set that starts with full name of the variable except the last part (Which is _Ask or _Bid). Can I do that in using an assignment statement?

推荐答案

您可能想要查询 sashelp.vtable,其中包含有关您的数据集的元数据.假设您的数据在库 WORK 中并称为 TABLE,下面会创建一个以 ASK 结尾的变量列表.

You probably want to query sashelp.vtable which holds the metadata about your data set. Assuming your data is in the library WORK and called TABLE the following creates a list of the variables that end in ASK.

proc sql;
select name into :varlist separated by " "
from sashelp.vcolumn
where libname="WORK" and memname="TABLE" and upcase(name) like '%_ASK';
quit;

*To rename the variables with MID generate a rename statement;
proc sql;
    select catx("=", name, tranwrd(upcase(name), "_ASK", "_MID"))
    into :rename_list separated by " "
    from sashelp.vcolumn
    where libname="WORK" and memname="TABLE" and upcase(name) like '%_ASK';
quit;

%put &rename_list;


data want_ask;
set work.table
 (keep = &varlist);
 rename &rename_list;
run;

这篇关于通过根据 SAS 名称中的关键字选择变量来对数据集进行子集化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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