BookmarkSubscribeRSS Feed
Sean_OConnor
Fluorite | Level 6

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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