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?
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;
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;
Thank you very much. It worked! How do I keep other variables of ds1 as well (such as ID)?
@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.
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.
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.