BookmarkSubscribeRSS Feed
AlexCamden
Calcite | Level 5

Is there a way to keep variables from one dataset if I am setting multiple datasets? There is an excel file of the variables that is wanted, it is only variables no data. Then I have my raw data that has the variables but has the data. I want to have a new dataset where I can set my “wanted variables” with my “raw data variables” and just keep those variables, because the raw data will have more variables than wanted.

I know I can do a simple keep statement, I am wanting it to be dynamic, so if the excel file with the desired variable list changes the new dataset will catch it and keep the new added variable. Rather than checking all the variables each time.

2 REPLIES 2
Kurt_Bremser
Super User

How to automate this:

/* this simulates your "list of variables" */
data vars_to_keep;
input varname :$32.;
datalines;
name
age
weight
;

/* create a macro variable from this */
proc sql noprint;
select varname into :keepnames separated by ' '
from vars_to_keep;
quit;

/* for demonstration purposes, I use a dataset from SASHELP that is available everywhere */
data want;
set sashelp.class;
keep &keepnames.;
run;

/* or */

data want;
set sashelp.class (keep=&keepnames.);
run;
Astounding
PROC Star

Alex,

 

It is possible that the solution from @Kurt_Bremser is exactly what you need.  But my ears pick up when I hear "Excel" and just want to make sure you are heading in the right direction.

 

When data come in from Excel, it is likely that you run into other types of problems.  Do character variables have the same lengths every time?  Could a variable be character in one data set but numeric in another data set?  For either of these cases, keeping the list of variables will hide your problem rather than uncover it.  

 

One way to approach the problem would be to construct a LENGTH statement as well as a KEEP statement.  That way you can set the lengths and have SAS give you some warning if a data set is not compatible with those lengths.  Besides a LENGTH statement, another method would be to create a data set with all the needed variables  You don't have to populate the variables, just define them.  Then you could use:

 

data want;

   if 0 then set correct_lengths;

   keep _all_;

   set one_data_filled_dataset;

run;

 

We don't really know the situations you will encounter, so this is really just a word of warning about what you might encounter.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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