Hi everyone,
I'm relatively new to SAS and I'm struggling with this problem.
I've been given two datasets, one big table with hundreds of variables, and the other with one column containing certain variable names from the first dataset.
I need to select all the variables in the first dataset whose names appear in the second dataset.
Is there any proc or sql technique to do this?
Thanks
Yes, typo there:
data _null_; set small end=last; if _n_=1 then call execute('data new_big; set big (keep='); call execute(varname||" "); if last then call execute('); run;'); run;
Hi, yes one way could be using the dictionnary tables:
proc sql ;
select b.name into :names separated by ' '
from sashelp.vcolumn a
inner join
sashelp.vcolumn b
on a.name eq b.name
where a.libname eq 'SASHELP'
and a.memname eq 'AARFM'
and b.libname eq 'SASHELP'
and b.memname eq 'ADSMSG'
;
quit;
%put &=names.;
data adsmsg;
set sashelp.adsmsg;
keep &names.;
run;
- Cheers -
There are several methods. The one I tend to go with is:
data _null_; set small end=last; if _n_=1 then call execute('data new_big; set big (keep='); call execute(varname," "); if last then call execute('); run;'); run;
This will generate the datastep to create new_big, using the big dataset, and for each row in small will add the variable to the keep list.
Hi RW9,
This looks good but I'm getting an error on the second execute routine. It says it has too many arguments.
Any suggestions on a fix for this?
Thanks again
Yes, typo there:
data _null_; set small end=last; if _n_=1 then call execute('data new_big; set big (keep='); call execute(varname||" "); if last then call execute('); run;'); run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.