BookmarkSubscribeRSS Feed
markc
Obsidian | Level 7

Hello,

 

How can I use a PROC SQL to select only variables from a SAS table which start with a prefix?

 

For example:

 

PROC SQL;

create table1 as

select a.prefix:,

from table2 as a;

QUIT;

3 REPLIES 3
novinosrin
Tourmaline | Level 20

Hi @markc 

 

I would be surprised if somebody like PGstats might come up with something as I don't think Proc sql supports variable lists. This means you would have to query dictionary tables and load the varlist in macro separated by ',' and call in a select clause.

 

Ok that being said you could use keep= dataset option like-->

 

data w;
array t(10)(10*35);
j=57665;
run;

proc sql;
create table want as
select *
from w(keep=t:);
quit;

 

 

Astounding
PROC Star

You have to spell out each variable when using SQL.  There are ways to get macro language to save you some of the typing, but macro language would have to generate a program that lists each variable.

 

Of course, if you switch to a DATA step, the coding is easy:

 

data want;
   set have (keep=prefix: ) ;
run;
PGStats
Opal | Level 21

You could use sashelp.vcolumns :

 

proc sql noprint;
select name into :cols separated by ","
from sashelp.vcolumn 
where libname="SASHELP" and upcase(memname)="HEART" and
    prxmatch("/^s/i", name) ;
create table subset as
select &cols. 
from sashelp.heart;
quit;
PG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 3 replies
  • 4862 views
  • 6 likes
  • 4 in conversation