Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop a range of variables in SAS

Tags:

sas

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.

like image 705
sujinge9 Avatar asked Mar 23 '13 19:03

sujinge9


3 Answers

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;
like image 192
BellevueBob Avatar answered Oct 03 '22 04:10

BellevueBob


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;
like image 45
Joe Avatar answered Oct 03 '22 04:10

Joe


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;
like image 36
Dejan Peretin Avatar answered Oct 03 '22 03:10

Dejan Peretin