BookmarkSubscribeRSS Feed
tennytivvytutu
Fluorite | Level 6

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

 

  1. WHERE City LIKE "New%"; will return values like New York, Newport, New
  2. WHERE City LIKE "Sant_  %"; will return values like Santa Clara, Santo Domingo

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! 

1 REPLY 1
sbxkoenk
SAS Super FREQ

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1252 views
  • 1 like
  • 2 in conversation