BookmarkSubscribeRSS Feed
Sean_OConnor
Obsidian | Level 7

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 

2 REPLIES 2
Kurt_Bremser
Super User

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.

TomKari
Onyx | Level 15

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 7548 views
  • 0 likes
  • 3 in conversation