BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
StephanDup
Fluorite | Level 6

Good day. I have the following 2 data sets:

 

The first data set has 1000 variables with their values:

 

ID    Var1 ... Var1000

1        5             15

2        7             18

.

.

.

100   3              12

 

I also have the following data set with a list of variables:

 

Var_Name

Var1

Var5

Var6

Var101

Var159

.

.

.

Var987

 

Is there a way I can select the variables listed in the second data set from the first data set?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

This is a classic task for call execute():

data _null_;
set ds2 end=done;
if _n_ = 1 then call execute('data want; set ds1; keep ';
call execute(var_name !! ' ');
if done then call execute('; run;');
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

This is a classic task for call execute():

data _null_;
set ds2 end=done;
if _n_ = 1 then call execute('data want; set ds1; keep ';
call execute(var_name !! ' ');
if done then call execute('; run;');
run;
StephanDup
Fluorite | Level 6

Thank you very much. It worked! How do I keep other variables of ds1 as well (such as ID)?

Kurt_Bremser
Super User

@StephanDup wrote:

Thank you very much. It worked! How do I keep other variables of ds1 as well (such as ID)?


Either put them into ds2 when you create it, or insert them in the first call execute in the keep statement.

Or you could turn the logic around and create ds2 with variable names to be dropped, and create a drop statement with call execute.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1853 views
  • 0 likes
  • 2 in conversation