Hi there,
I know that we can use the LIKE operator to filter rows of data; below are my notes on this:
LIKE operator for pattern matching
WHERE col-name LIKE "value";
% is a wild card for any number of characters
_ is a wild card for a single character
*A wild card is a character that can be substituted for zero or more characters in a string
My question is, is it possible to apply this same logic to subset data by column names?
More specifically, I have a table with over 100 columns; I want to create a new table that includes only the columns related to Math. Instead of copy/paste or listing all the columns with the word "Math" in the name, can I apply the LIKE operator using the wildcards to create a table including all columns whose name contains the word "Math"?
Moreover, would this subsetting need to happen in a DATA step or PROC SQL?
DATA want; SET have; WHERE *column name; LIKE "%math%"; RUN;
PROC SQL; CREATE TABLE want AS
SELECT *column name LIKE '%math'; FROM have;
QUIT;
Thanks in advance!
Use the dictionary tables, like here :
%LET olnames=;
PROC SQL noprint;
select name into :olnames separated by ' '
from dictionary.columns
where libname='SASHELP' and memname='HEART' and name LIKE "%ol%";
QUIT;
%PUT &=olnames;
data work.have; set sashelp.heart(keep=&olnames.); run;
/* end of program */
Koen
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.