03-22-2017 07:53 AM
I'm attempting to merge a number of datasets together, however, prior to this I need to do some cleaning up. I only want to keep certain variables so I'm using the (keep= ) option.
I've create a macro variable to desginate what variables I want to keep across all my different datasets. Unforunately however, not all variables are in all datasets. Thus when I run the data step, which includes a macro I get a number of errors as the variables I tell sas to keep are not in the dataset (they are in a number of different datasets).
Is there a a soloution to this. For reference here is a sample of the code I'm using.
%let year = 2014p /*(Points to the new folder)*/; %let yeara = 2014 /*(Points to the correct year within the folder)*/ ; %let vars = (keep= profit sales gender region); data DETAILS&yeara; set itsd.DETAILS&yeara &vars; data director&yeara; set itsd.director&yeara;&vars;run;
Initially I thought the
Might work before I ran the data step but it doesn't. Any feedback welcome.
03-22-2017 07:59 AM
right before the relevant steps; don't forget to reset DKRICOND to its initial value afterwards, as you don't want SAS to ignore all such errors.
03-22-2017 11:41 AM
On the one hand, the solution that @KurtBremser provided will work perfectly, and is by far the easiest.
Another option is to actually build a variable list of the variables that you want that are in your target dataset. Here's an example:
/* This is the list of variable names we want */
/* Note that "MadeUpColumn" won't be in the dataset */
length VarName $32;
/* This will be the table that we want to keep certain variables from */
/* Get the list of variables in the table */
create table GoodColNames as select name as VarName from dictionary.columns
where libname = "WORK" and memname = "FILEWITHCOLUMNS";
/* Put the list of variables that we want that are also in the dataset into a macro variable */
proc sql noprint;
select n.VarName into :JoinedCols separated by " "
from GoodColNames n join VarsToKeep v
on n.VarName = v.Varname;
/* Set up the rest of the keep clause syntax */
%let vars =(keep= &JoinedCols.)%str(;