Subsetting a dataset by selecting variables based on keywords in their name in SAS

帅比萌擦擦* 提交于 2019-12-29 08:21:34

问题


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

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.

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?


回答1:


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;


来源:https://stackoverflow.com/questions/26855371/subsetting-a-dataset-by-selecting-variables-based-on-keywords-in-their-name-in-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!