Hello SAS experts,
I have a huge dataset that need to delete variables for example 2-20 and need to know how.
Thanks
Hi,
Check out 'drop' or 'keep' statements or options.
data want;
set sashelp.class;
drop sex--weight;
run;
Regards,
Haikuo
this is a dataset with 500 variables, It would be very hard to do this one by one, I need to drop let say variables 10 through variable 20. I have droped the ones with same prefix but there are more to drop.
thanks
if they are in order like you said, you can use '--' to do the job. just edit my post.
Take a look at datanull's suggested method at: http://communities.sas.com/message/38407#38407
Thanks Art, but this looks a lot more complicated than it should be. Is there any simple code for droping sequntial variables?
thanks
The method is easier than it appears. Here is an example dropping variable 2 thru 3 from sashelp.class:
proc sql noprint;
select name
into :vlist separated by " "
from dictionary.columns
where libname eq 'SASHELP' and
memname = "CLASS" and
varnum between 2 and 3
;
quit;
data want;
set sashelp.class (drop=&vlist.);
run;
I've never worked with SQL syntax, so I actually do not know what any of these lines mean. Is there any other way to do this
Thank you
While I agree with Tom that it is dangerous to drop variable by number, the only lines that have to be changed in the proc sql code are:
where libname eq 'SASHELP' and
SASHELP has to be changed to the library where your data is located, but typed in UPPER CASE
memname = "CLASS" and
CLASS has to be changed to the name of your data file, but typed in UPPER CASE
varnum between 2 and 3
and, where I used 2 and 3 in the above statement, if you wanted to drop variables 10 thru 20 you would replace that line with:
varnum between 10 and 20
I am not sure what is easier than using a variable name range list as suggested above. Read the documentation:
http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695105.htm
Using name range lists (such as sex -- weight) is dangerous enough without being able drop variables by position without even knowing the variables names.
It sounds like you would be more comfortable with a low-tech approach. If that's true ...
Run a PROC CONTENTS on the data set, adding the POSITION option. That way, you can see a list of all variables in order. You will more easily be able to select ranges of variables to remove, and apply that using the syntax that has already been shown here:
data new;
set old (drop=first_variable_to_drop -- last_variable_to_drop first_variable_in_another_range -- last_variable_in_another_range);
run;
You'll need to know the names of the first and last variable being dropped.
Or even lower tech just open the dataset or proc print it and you will see the variables in order.
Thank you All,
Low tech sounds great to me.
Thanks
If you are worried about the order of the variables then how about trying this:
%let varlist1= ;*Define your variable list in the order you would like to keep;
%let varlist2= ; * Define you variables that you would like to drop;
data a;
set dsin;
drop &varlist2;
run;
Data dsout;
retail &varlist1;
set a;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.