Discussion:
SQL - Excluding columns from Select
(too old to reply)
Ali G
2005-09-22 14:29:02 UTC
Permalink
Is there a way of excluding columns in an SQL select construct? I want
to write a statement that would be synonomous with this :

data table1(keep=var1-var1000);
--data--;
run;

proc sql;
select *
except var1, var2, var3
from table1
;
quit;

I know I could do it with the datastep and DROP= but it's SQL I'm
interested in.

Thanks.
Toby
2005-09-22 14:36:01 UTC
Permalink
ALI ,

data one ;
infile cards dlm = ' ' ;
input var1 var2 var3 var4 var5 ;
cards ;
1 2 3 4 5
;
run ;

proc sql ;
select *
from one (drop = var4 var5) ;
quit ;


Is there a way of excluding columns in an SQL select construct? I want
to write a statement that would be synonomous with this :


data table1(keep=var1-var1000);
--data--;
run;


proc sql;
select *
except var1, var2, var3
from table1
;
quit;


I know I could do it with the datastep and DROP= but it's SQL I'm
interested in.


Thanks.
Ali G
2005-09-22 15:23:48 UTC
Permalink
Thanks Toby,

Simple as that...

I tried to extend your code to:

proc sql ;
select *
from one (drop = var4 var5)
where var4 = 4;
quit ;

But it didn't like that.
Toby
2005-09-22 15:32:38 UTC
Permalink
ALI ,

The Drop statement written like the code below works the same in SQL as
it does in a Data step. It basically stops SAS from reading the
variable into the PDV, thus you can't use a where statement that
references a var in the drop statement.

You can't do the following:

Data one ;
set two (drop = var4) ;
where var4 = 'Alpha' ;
run ;

nor can you do:

proc sql ;
select *
from one (drop = var4)
where var4 = 'Alpha' ;
quit ;

and you can't do the following either:

proc print
data = one (drop = var4) ;
where var4 = 'Alpha' ;
run ;

it looks like you want the printing capabilities of SQL select clause
but also want to mix the var selection capabilities of proc print. If
it were me I would either use proc print or if I needed to do something
that I couldn't do in proc print and could do easily in SQL I would do
something like the following:

proc sql ;
create table _temp as
select *
from one
where var4 = 4 ;

select *
from _temp (drop = Var4 var5) ;
quit ;


Toby Dunn

Thanks Toby,


Simple as that...


I tried to extend your code to:


proc sql ;
select *
from one (drop = var4 var5)
where var4 = 4;
quit ;


But it didn't like that.
Ken Borowiak
2005-09-22 16:39:18 UTC
Permalink
A warning is generated that data set options will be ignored when used
in the CREATE VIEW statement, except for READ, WRITE, ALTER, PW &
LABEL. Most data set options will work in the CREATE TABLE statement.

Ken

-----Original Message-----
From: Howard Schreier <hs AT dc-sug DOT org> <***@HOWLES.COM>
To: SAS-***@LISTSERV.UGA.EDU
Sent: Thu, 22 Sep 2005 11:30:14 -0400
Subject: Re: SQL - Excluding columns from Select

You'll need two statements, as in (untested);

create view no45 (drop = var4 var5) as
select *
from one
where var4 = 4;

select * from no45;
Post by Ali G
Thanks Toby,
Simple as that...
proc sql ;
select *
from one (drop = var4 var5)
where var4 = 4;
quit ;
But it didn't like that.
John Hendrickx
2005-09-23 06:46:28 UTC
Permalink
Here's yet another alternative, using macros available at
http://www.datasavantconsulting.com/roland/sasautos.html

proc sql ;
select %remove(%varlist(one),var4,var5)
from one;
quit ;

You can add up to 30 arguments to the %remove macro.

HTH,
John Hendrickx
Ali G
2005-09-23 08:37:12 UTC
Permalink
That's perfect. A neat solution that does exactly what I want.

Thanks to all who replied.
s***@gmail.com
2012-06-14 12:16:02 UTC
Permalink
Post by Ali G
That's perfect. A neat solution that does exactly what I want.
Thanks to all who replied.
HI can u give some small example for this .

Loading...