Help using Base SAS procedures

deleting variables in order

Reply
Regular Contributor
Posts: 161

deleting variables in order

Hello SAS experts,

I have a huge dataset that need to delete variables for example 2-20 and need to know how.

Thanks

Respected Advisor
Posts: 3,156

Re: deleting variables in order

Hi,

Check out 'drop' or 'keep' statements or options.

data want;

set sashelp.class;

drop sex--weight;

run;

Regards,

Haikuo

Regular Contributor
Posts: 161

deleting variables in order

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

Respected Advisor
Posts: 3,156

deleting variables in order

if they are in order like you said, you can use '--' to do the job. just edit my post.

PROC Star
Posts: 7,468

deleting variables in order

Take a look at datanull's suggested method at: http://communities.sas.com/message/38407#38407

Regular Contributor
Posts: 161

deleting variables in order

Thanks Art, but this looks a lot more complicated than it should be. Is there any simple code for droping sequntial variables?

thanks

PROC Star
Posts: 7,468

deleting variables in order

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;

Regular Contributor
Posts: 161

deleting variables in order

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

PROC Star
Posts: 7,468

deleting variables in order

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

Super User
Super User
Posts: 7,039

deleting variables in order

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.

Super User
Posts: 5,498

deleting variables in order

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.

Super User
Super User
Posts: 7,039

deleting variables in order

Posted in reply to Astounding

Or even lower tech just open the dataset or proc print it and you will see the variables in order.

Regular Contributor
Posts: 161

deleting variables in order

Thank you All,

Low tech sounds great to me.

Thanks

Frequent Contributor
Frequent Contributor
Posts: 83

deleting variables in order

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;

Ask a Question
Discussion stats
  • 13 replies
  • 1087 views
  • 0 likes
  • 6 in conversation