6
votes

I currently have a dataset with 200 variables. From those variables, I created 100 new variables. Now I would like to drop the original 200 variables. How can I do that?

Slightly better would be, how I can drop variables 3-200 in the new dataset.

sorry if I was vague in my question but basically I figured out I need to use --. If my first variable is called first and my last variable is called last, I can drop all the variables inbetween with (drop= first--last);

Thanks for all the responses.

5

5 Answers

5
votes

As with most SAS tasks, there are several alternatives. The easiest and safest way to drop variables from a SAS data set is with PROC SQL. Just list the variables by name, separated by a comma:

proc sql;
   alter table MYSASDATA
      drop name, age, address;
quit;

Altering the table with PROC SQL removes the variables from the data set in place.

Another technique is to recreate the data set using a DROP option:

data have;
   set have(drop=name age address);
run;

And yet another way is using a DROP statement:

data have;
   set have;
   drop name age address;
run;
4
votes

Lots of options - some 'safer', some less safe but easier to code. Let's imagine you have a dataset with variables ID, PLNT, and x1-x200 to start with.

data have;
id=0;
plnt=0;
array x[200];
do _t = 1 to dim(x);
x[_t]=0;
end;
run;

data want;
set have;
*... create new 100 variables ... ;
*option 1:
drop x1-x200; *this works when x1-x200 are numerically consecutive;
*option 2:
drop x1--x200; *this works when they are physically in order on the dataset - 
                only the first and last matter;
run;

*Or, do it this way. This would also work with SQL ALTER TABLE. This is the safest way to do it.;

proc sql;
select name into :droplist separated by ' ' from dictionary.columns
where libname='WORK' and memname='HAVE' and name not in ('ID','PRNT');
quit;

proc datasets lib=work;
modify want;
drop &droplist.;
quit;
2
votes

If all of the variables you want to drop are named so they all start the same (like old_var_1, old_var_2, ..., old_var_n), you could do this (note the colon in drop option):

data have;
set have(drop= old_var:);
run;
0
votes
data want;
    set have;
    drop VAR1--VARx;
run;

Would love to know if you can do this by position. Definitely works with variable names separated by double dash (--).

0
votes

I have some macros that would allow this here You could run that whole set of macros, or just run list_vars(), is_blank(), num_words, find_word, remove_word, remove_words , nth_word().

Using these it would be:

%let keep_vars = keep_this and_this also_this;
%let drop_vars = %list_vars(old_dataset);
%let drop_vars = %remove_words(&drop_vars  , &keep_vars);

data new_dataset (drop = &drop_vars );
  set old_dataset;
 /*stuff happens*/
run;

This will keep the three variables keep_this and_this also_this but drop everything else in the old dataset.