BookmarkSubscribeRSS Feed
choi
Calcite | Level 5
I have a variable (e.g., VARS) in Table_A that contains a list of variables to be selected from Table_B.

VARS
====
"Age"
"Gender"
"Height"
"Weight"
...

Table B contains variables as named above among many other variables. I would like to select only those variables in VARS from Table B. Here's a code I used:

proc sql;
select (select VARS from Table_A)
from Table_B;
run;

Of course, I got an error message: "ERROR: Subquery evaluated to more than one row."

Is there a way to achieve this in proc sql? I know how to do this by creating %macro variables.

Many thanks,
2 REPLIES 2
art297
Opal | Level 21
There is probably a more direct way, but the following example should work:


data a;
input a b c d e f;
cards;
1 2 3 4 5 6
7 8 9 10 11 12
;
data b;
input a b c;
cards;
1 2 3
4 5 6
;
proc sql noprint;
select name into :names
separated by ","
from dictionary.columns
where libname eq "WORK" and
memname eq "B"
;

create table want as
select &names.
from A
;
quit;

HTH,
Art
DBailey
Lapis Lazuli | Level 10
to follow up on the post...

VARS
"Age"
"Gender"
"Height"
"Weight"

proc sql;
select vars into :VarStmt separated by ', '
from VARS;

select &VarStmt from Table_B;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 1074 views
  • 0 likes
  • 3 in conversation