Folks,
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
OPTIONS NOSYNTAXCHECK;
Might work before I ran the data step but it doesn't. Any feedback welcome.
Kind regards,
Sean
Use
options dkricond=nowarn;
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.
On the one hand, the solution that @Kurt_Bremser 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:
Tom
/* This is the list of variable names we want */
/* Note that "MadeUpColumn" won't be in the dataset */
data VarsToKeep;
length VarName $32;
input VarName;
cards;
Product
Stores
MadeUpColumn
Inventory
run;
/* This will be the table that we want to keep certain variables from */
data FileWithColumns;
set sashelp.shoes;
run;
/* Get the list of variables in the table */
proc sql;
create table GoodColNames as select name as VarName from dictionary.columns
where libname = "WORK" and memname = "FILEWITHCOLUMNS";
quit;
/* 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;
quit;
/* Set up the rest of the keep clause syntax */
%let vars =(keep= &JoinedCols.)%str(;);
%put &vars.;
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!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.