SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Selecting variables from another data set

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Selecting variables from another data set

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?


Accepted Solutions
Solution
‎11-01-2017 12:52 AM
Super User
Posts: 9,932

Re: Selecting variables from another data set

Posted in reply to StephanDup

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎11-01-2017 12:52 AM
Super User
Posts: 9,932

Re: Selecting variables from another data set

Posted in reply to StephanDup

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 8

Re: Selecting variables from another data set

Posted in reply to KurtBremser

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

Super User
Posts: 9,932

Re: Selecting variables from another data set

Posted in reply to StephanDup

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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