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

851 views Asked by At

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

There are 1 answers

6
Reeza On

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;