BookmarkSubscribeRSS Feed
R_A_G_
Calcite | Level 5

Hello SAS experts,

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

Thanks

13 REPLIES 13
Haikuo
Onyx | Level 15

Hi,

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

data want;

set sashelp.class;

drop sex--weight;

run;

Regards,

Haikuo

R_A_G_
Calcite | Level 5

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

Haikuo
Onyx | Level 15

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

art297
Opal | Level 21

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

R_A_G_
Calcite | Level 5

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

thanks

art297
Opal | Level 21

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;

R_A_G_
Calcite | Level 5

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

art297
Opal | Level 21

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

Tom
Super User Tom
Super User

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.

Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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

R_A_G_
Calcite | Level 5

Thank you All,

Low tech sounds great to me.

Thanks

Mit
Calcite | Level 5 Mit
Calcite | Level 5

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 2168 views
  • 0 likes
  • 6 in conversation