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