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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1474 views
  • 0 likes
  • 2 in conversation