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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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