Date: Fri, 3 Oct 2003 13:03:08 -0700
Reply-To: "Huang, Ya" <yhuang@AMYLIN.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Huang, Ya" <yhuang@AMYLIN.COM>
Subject: Re: delete a column
Content-Type: text/plain; charset="iso-8859-1"
Jialu,
I'll try 'max()=min()' as the criteria to test
if all the values are same. I'll then try proc sql and
dictionary view to construct a list of the variables that
meet the criteria, then use that list in a data
step to drop those variables:
data one;
input x1 x2 x3 x4 c1 $ c2 $;
cards;
1 3 5 9 a a
1 2 5 9 a b
1 3 4 9 a c
1 5 9 . a d
1 1 6 9 a d
1 4 7 9 a d
;
options symbolgen;
proc sql noprint;
select 'case when max('||trim(name)||')=min('||trim(name)||') then "'
||compress(name)||'" else " " end' into :nlst separated by "||' '||"
from dictionary.columns
where libname='WORK' and memname='ONE'
;
select distinct &nlst
into :droplst
from one
;
data one;
set one (drop=&droplst);
run;
proc print;
run;
---------
Obs x2 x3 c2
1 3 5 a
2 2 5 b
3 3 4 c
4 5 9 d
5 1 6 d
6 4 7 d
Note that it even works for character variable! (x1 and c1
have been dropped). Note also that the since max() and min() do
not count missing values, therefore, a column with same unmissing
value and some missing value are also considered met the
criteria and dropped (x4).
Hope this helps.
Kind regards,
Ya Huang
-----Original Message-----
From: Jialu Zhang [mailto:jzhang8@GL.UMBC.EDU]
Sent: Friday, October 03, 2003 12:20 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: delete a column
Hello,
Does anyone know how to do this:
data one;
input x1 x2 x3;
cards;
1 3 5
1 2 5
1 3 4
1 5 9
1 1 6
1 4 7
;
run;
What I need to do is: if all the observations in one column have the same
value, then the column will be deleted. In this case, the x1 variable need
to be deleted from the dataset since the only value of x1 variable is 1.
The dataset will look like the following after deleting x1 column.
data one:
x2 x3
3 5
2 5
3 4
5 9
1 6
4 7
Many thanks in advance.
Jialu